![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Servo controlled by soil moisture
Dependencies: mbed C12832 Servo
main.cpp@0:8badbe8e934c, 2021-10-17 (annotated)
- Committer:
- tollidal
- Date:
- Sun Oct 17 10:26:17 2021 +0000
- Revision:
- 0:8badbe8e934c
Strawberry Forever Soil Moisture Sensor & Servo
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tollidal | 0:8badbe8e934c | 1 | #include "mbed.h" |
tollidal | 0:8badbe8e934c | 2 | #include "Servo.h" |
tollidal | 0:8badbe8e934c | 3 | #include "C12832.h" |
tollidal | 0:8badbe8e934c | 4 | |
tollidal | 0:8badbe8e934c | 5 | C12832 lcd(p5, p7, p6, p8, p11); //LCD screen |
tollidal | 0:8badbe8e934c | 6 | AnalogIn soil_moisture(p20); //soil moisture sensor |
tollidal | 0:8badbe8e934c | 7 | Servo myservo(p21); //servo |
tollidal | 0:8badbe8e934c | 8 | |
tollidal | 0:8badbe8e934c | 9 | int main() |
tollidal | 0:8badbe8e934c | 10 | { |
tollidal | 0:8badbe8e934c | 11 | |
tollidal | 0:8badbe8e934c | 12 | |
tollidal | 0:8badbe8e934c | 13 | /*lcd.printf("Servo Calibration Controls:\n"); |
tollidal | 0:8badbe8e934c | 14 | lcd.printf("1,2,3 - Position Servo (full left, middle, full right)\n"); |
tollidal | 0:8badbe8e934c | 15 | lcd.printf("4,5 - Decrease or Increase range\n");*/ |
tollidal | 0:8badbe8e934c | 16 | |
tollidal | 0:8badbe8e934c | 17 | float range = 0.0005; |
tollidal | 0:8badbe8e934c | 18 | float position = 0.5; |
tollidal | 0:8badbe8e934c | 19 | |
tollidal | 0:8badbe8e934c | 20 | float moisture_value = 0.0f; |
tollidal | 0:8badbe8e934c | 21 | |
tollidal | 0:8badbe8e934c | 22 | |
tollidal | 0:8badbe8e934c | 23 | while(1) { |
tollidal | 0:8badbe8e934c | 24 | |
tollidal | 0:8badbe8e934c | 25 | moisture_value = soil_moisture; //soil moisture value |
tollidal | 0:8badbe8e934c | 26 | lcd.locate(0,0); //text location on screen |
tollidal | 0:8badbe8e934c | 27 | lcd.cls(); |
tollidal | 0:8badbe8e934c | 28 | lcd.printf("Moisture reading is: %2.2f\n", moisture_value); //display soil moisture value as a percentage |
tollidal | 0:8badbe8e934c | 29 | wait(1.0f); |
tollidal | 0:8badbe8e934c | 30 | // lcd.printf("Moisture reading is: %2.2f\n", value); |
tollidal | 0:8badbe8e934c | 31 | |
tollidal | 0:8badbe8e934c | 32 | if (moisture_value > 30&& moisture_value <= 65) { //if soil moisture level is between 30 and 60 moisture level okay |
tollidal | 0:8badbe8e934c | 33 | lcd.printf("Moisture level okay"); |
tollidal | 0:8badbe8e934c | 34 | position = 0.0; |
tollidal | 0:8badbe8e934c | 35 | } |
tollidal | 0:8badbe8e934c | 36 | |
tollidal | 0:8badbe8e934c | 37 | else if (moisture_value <= 30) { //if soil moisture level is below 30, soil is dry |
tollidal | 0:8badbe8e934c | 38 | position = 1.0; //move servo to open position to water plant |
tollidal | 0:8badbe8e934c | 39 | |
tollidal | 0:8badbe8e934c | 40 | |
tollidal | 0:8badbe8e934c | 41 | } else { |
tollidal | 0:8badbe8e934c | 42 | position = 0.0; //if soil moisture is above 65 soil is wet, do not water |
tollidal | 0:8badbe8e934c | 43 | } |
tollidal | 0:8badbe8e934c | 44 | |
tollidal | 0:8badbe8e934c | 45 | |
tollidal | 0:8badbe8e934c | 46 | |
tollidal | 0:8badbe8e934c | 47 | lcd.printf("position = %.1f, range = +/-%0.4f\n", position, range); |
tollidal | 0:8badbe8e934c | 48 | myservo.calibrate(range, 45.0); |
tollidal | 0:8badbe8e934c | 49 | myservo = position; |
tollidal | 0:8badbe8e934c | 50 | } |
tollidal | 0:8badbe8e934c | 51 | } |