Advertisement
pleasedontcode

Moisture Management rev_01

Aug 18th, 2024
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: Moisture Management
  13.     - Source Code NOT compiled for: Arduino Mega
  14.     - Source Code created on: 2024-08-18 14:36:40
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* The Arduino-based irrigation system shall */
  21.     /* continuously read soil moisture data from an */
  22.     /* analog input, process the data, and trigger */
  23.     /* irrigation actions based on predefined moisture */
  24.     /* thresholds. */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28.  
  29. /****** FUNCTION PROTOTYPES *****/
  30. void setup(void);
  31. void loop(void);
  32. void updateInputs();
  33. void convertInputsFromRawToPhyData();
  34. void triggerIrrigation(bool state);
  35.  
  36. /***** DEFINITION OF ANALOG INPUT PINS *****/
  37. const uint8_t Soil1_PIN_A0 = A0; // Pin for soil moisture sensor
  38. const uint8_t IRRIGATION_PIN = 9; // Define a digital pin for irrigation control
  39.  
  40. /****** DEFINITION OF ANALOG INPUTS CHARACTERISTIC CURVES *****/
  41. const uint8_t SEGMENT_POINTS_voltage_Moisture_PIN_A0 = 2;
  42. const float voltage_Moisture_PIN_A0_lookup[2][SEGMENT_POINTS_voltage_Moisture_PIN_A0] =
  43. {
  44.     {0.0, 5.0},   // Voltage [V]
  45.     {0.0, 100.0}  // Moisture [M]
  46. };
  47.  
  48. /***** DEFINITION OF INPUT RAW VARIABLES *****/
  49. /***** used to store raw data *****/
  50. unsigned int Soil1_PIN_A0_rawData = 0; // Analog Input
  51.  
  52. /***** DEFINITION OF INPUT PHYSICAL VARIABLES *****/
  53. /***** used to store data after characteristic curve transformation *****/
  54. float Soil1_PIN_A0_phyData = 0.0; // Moisture [M]
  55.  
  56. /***** DEFINITION OF THRESHOLDS *****/
  57. const float MOISTURE_THRESHOLD_LOW = 30.0; // Lower threshold for moisture
  58. const float MOISTURE_THRESHOLD_HIGH = 60.0; // Upper threshold for moisture
  59.  
  60. void setup(void)
  61. {
  62.     // Initialize the analog input pin
  63.     pinMode(Soil1_PIN_A0, INPUT);
  64.     // Initialize the irrigation control pin
  65.     pinMode(IRRIGATION_PIN, OUTPUT);
  66.     // Ensure irrigation is off at the start
  67.     digitalWrite(IRRIGATION_PIN, LOW);
  68. }
  69.  
  70. void loop(void)
  71. {
  72.     // Refresh input data
  73.     updateInputs();
  74.     // Convert raw data to physical data
  75.     convertInputsFromRawToPhyData();
  76.     // Trigger irrigation based on moisture levels
  77.     triggerIrrigation(Soil1_PIN_A0_phyData < MOISTURE_THRESHOLD_LOW);
  78. }
  79.  
  80. void updateInputs()
  81. {
  82.     Soil1_PIN_A0_rawData = analogRead(Soil1_PIN_A0); // Read raw soil moisture data
  83. }
  84.  
  85. /* BLOCK lookup_phyData_from_voltage */
  86. float lookup_phyData_from_voltage(float voltage, int segment_points, const float* voltage_phyData_lookup)
  87. {
  88.     // Search table for appropriate value.
  89.     uint8_t index = 0;
  90.  
  91.     const float *voltagePointer = &voltage_phyData_lookup[0];
  92.     const float *phyDataPointer = &voltage_phyData_lookup[segment_points];
  93.  
  94.     // Perform minimum and maximum voltage saturation based on characteristic curve
  95.     voltage = min(voltage, voltagePointer[segment_points-1]);
  96.     voltage = max(voltage, voltagePointer[0]);
  97.  
  98.     while( voltagePointer[index] <= voltage && index < segment_points )
  99.         index++;
  100.  
  101.     // If index is zero, physical value is smaller than our table range
  102.     if( index==0 )
  103.     {
  104.         return map_f( voltage,
  105.             voltagePointer[0],   // X1
  106.             voltagePointer[1],   // X2
  107.             phyDataPointer[0],   // Y1
  108.             phyDataPointer[1] ); // Y2
  109.     }
  110.     // If index is maxed out, phyisical value is larger than our range.
  111.     else if( index==segment_points )
  112.     {
  113.         return map_f( voltage,
  114.             voltagePointer[segment_points-2],   // X1
  115.             voltagePointer[segment_points-1],   // X2
  116.             phyDataPointer[segment_points-2],   // Y1
  117.             phyDataPointer[segment_points-1] ); // Y2
  118.     }
  119.     // index is between 0 and max, just right
  120.     else
  121.     {
  122.         return map_f( voltage,
  123.             voltagePointer[index-1], // X1
  124.             voltagePointer[index],    // X2
  125.             phyDataPointer[index-1], // Y1
  126.             phyDataPointer[index] );  // Y2
  127.     }
  128. }
  129. /* END BLOCK lookup_phyData_from_voltage */
  130.  
  131. /* BLOCK map_f */
  132. float map_f(float x, float in_min, float in_max, float out_min, float out_max)
  133. {
  134.     return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
  135. }
  136. /* END BLOCK map_f */
  137.  
  138. /* BLOCK convertInputsFromRawToPhyData */
  139. void convertInputsFromRawToPhyData()
  140. {
  141.     float voltage = 0.0;
  142.  
  143.     voltage = Soil1_PIN_A0_rawData * (5.0 / 1023.0);
  144.     Soil1_PIN_A0_phyData = lookup_phyData_from_voltage(voltage, SEGMENT_POINTS_voltage_Moisture_PIN_A0, &(voltage_Moisture_PIN_A0_lookup[0][0]));
  145.  
  146. }
  147. /* END BLOCK convertInputsFromRawToPhyData */
  148.  
  149. /* BLOCK triggerIrrigation */
  150. void triggerIrrigation(bool state)
  151. {
  152.     if (state)
  153.     {
  154.         digitalWrite(IRRIGATION_PIN, HIGH); // Turn on irrigation
  155.     }
  156.     else
  157.     {
  158.         digitalWrite(IRRIGATION_PIN, LOW); // Turn off irrigation
  159.     }
  160. }
  161. /* END BLOCK triggerIrrigation */
  162.  
  163. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement