Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: Moisture Management
- - Source Code NOT compiled for: Arduino Mega
- - Source Code created on: 2024-08-18 14:36:40
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* The Arduino-based irrigation system shall */
- /* continuously read soil moisture data from an */
- /* analog input, process the data, and trigger */
- /* irrigation actions based on predefined moisture */
- /* thresholds. */
- /****** END SYSTEM REQUIREMENTS *****/
- /****** DEFINITION OF LIBRARIES *****/
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- void updateInputs();
- void convertInputsFromRawToPhyData();
- void triggerIrrigation(bool state);
- /***** DEFINITION OF ANALOG INPUT PINS *****/
- const uint8_t Soil1_PIN_A0 = A0; // Pin for soil moisture sensor
- const uint8_t IRRIGATION_PIN = 9; // Define a digital pin for irrigation control
- /****** DEFINITION OF ANALOG INPUTS CHARACTERISTIC CURVES *****/
- const uint8_t SEGMENT_POINTS_voltage_Moisture_PIN_A0 = 2;
- const float voltage_Moisture_PIN_A0_lookup[2][SEGMENT_POINTS_voltage_Moisture_PIN_A0] =
- {
- {0.0, 5.0}, // Voltage [V]
- {0.0, 100.0} // Moisture [M]
- };
- /***** DEFINITION OF INPUT RAW VARIABLES *****/
- /***** used to store raw data *****/
- unsigned int Soil1_PIN_A0_rawData = 0; // Analog Input
- /***** DEFINITION OF INPUT PHYSICAL VARIABLES *****/
- /***** used to store data after characteristic curve transformation *****/
- float Soil1_PIN_A0_phyData = 0.0; // Moisture [M]
- /***** DEFINITION OF THRESHOLDS *****/
- const float MOISTURE_THRESHOLD_LOW = 30.0; // Lower threshold for moisture
- const float MOISTURE_THRESHOLD_HIGH = 60.0; // Upper threshold for moisture
- void setup(void)
- {
- // Initialize the analog input pin
- pinMode(Soil1_PIN_A0, INPUT);
- // Initialize the irrigation control pin
- pinMode(IRRIGATION_PIN, OUTPUT);
- // Ensure irrigation is off at the start
- digitalWrite(IRRIGATION_PIN, LOW);
- }
- void loop(void)
- {
- // Refresh input data
- updateInputs();
- // Convert raw data to physical data
- convertInputsFromRawToPhyData();
- // Trigger irrigation based on moisture levels
- triggerIrrigation(Soil1_PIN_A0_phyData < MOISTURE_THRESHOLD_LOW);
- }
- void updateInputs()
- {
- Soil1_PIN_A0_rawData = analogRead(Soil1_PIN_A0); // Read raw soil moisture data
- }
- /* BLOCK lookup_phyData_from_voltage */
- float lookup_phyData_from_voltage(float voltage, int segment_points, const float* voltage_phyData_lookup)
- {
- // Search table for appropriate value.
- uint8_t index = 0;
- const float *voltagePointer = &voltage_phyData_lookup[0];
- const float *phyDataPointer = &voltage_phyData_lookup[segment_points];
- // Perform minimum and maximum voltage saturation based on characteristic curve
- voltage = min(voltage, voltagePointer[segment_points-1]);
- voltage = max(voltage, voltagePointer[0]);
- while( voltagePointer[index] <= voltage && index < segment_points )
- index++;
- // If index is zero, physical value is smaller than our table range
- if( index==0 )
- {
- return map_f( voltage,
- voltagePointer[0], // X1
- voltagePointer[1], // X2
- phyDataPointer[0], // Y1
- phyDataPointer[1] ); // Y2
- }
- // If index is maxed out, phyisical value is larger than our range.
- else if( index==segment_points )
- {
- return map_f( voltage,
- voltagePointer[segment_points-2], // X1
- voltagePointer[segment_points-1], // X2
- phyDataPointer[segment_points-2], // Y1
- phyDataPointer[segment_points-1] ); // Y2
- }
- // index is between 0 and max, just right
- else
- {
- return map_f( voltage,
- voltagePointer[index-1], // X1
- voltagePointer[index], // X2
- phyDataPointer[index-1], // Y1
- phyDataPointer[index] ); // Y2
- }
- }
- /* END BLOCK lookup_phyData_from_voltage */
- /* BLOCK map_f */
- float map_f(float x, float in_min, float in_max, float out_min, float out_max)
- {
- return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
- }
- /* END BLOCK map_f */
- /* BLOCK convertInputsFromRawToPhyData */
- void convertInputsFromRawToPhyData()
- {
- float voltage = 0.0;
- voltage = Soil1_PIN_A0_rawData * (5.0 / 1023.0);
- Soil1_PIN_A0_phyData = lookup_phyData_from_voltage(voltage, SEGMENT_POINTS_voltage_Moisture_PIN_A0, &(voltage_Moisture_PIN_A0_lookup[0][0]));
- }
- /* END BLOCK convertInputsFromRawToPhyData */
- /* BLOCK triggerIrrigation */
- void triggerIrrigation(bool state)
- {
- if (state)
- {
- digitalWrite(IRRIGATION_PIN, HIGH); // Turn on irrigation
- }
- else
- {
- digitalWrite(IRRIGATION_PIN, LOW); // Turn off irrigation
- }
- }
- /* END BLOCK triggerIrrigation */
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement