Strawberry_Greenhouse_V1.0
Dependencies: mbed C12832 Servo HCSR04 DHT DHT11
main.cpp@3:fdbd4ffb9ca6, 2021-10-19 (annotated)
- Committer:
- tollidal
- Date:
- Tue Oct 19 20:21:02 2021 +0000
- Revision:
- 3:fdbd4ffb9ca6
- Parent:
- 2:2621304bdec1
Strawberry Greenhouse complete V3;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jbrown57 | 0:c48b28944702 | 1 | #include "mbed.h" |
jbrown57 | 0:c48b28944702 | 2 | #include "C12832.h" |
jbrown57 | 0:c48b28944702 | 3 | #include "tank.h" |
jbrown57 | 0:c48b28944702 | 4 | #include "HCSR04.h" |
tollidal | 2:2621304bdec1 | 5 | #include "DHT.h" |
tollidal | 2:2621304bdec1 | 6 | #include "Servo.h" |
jbrown57 | 0:c48b28944702 | 7 | |
jbrown57 | 0:c48b28944702 | 8 | |
jbrown57 | 0:c48b28944702 | 9 | C12832 lcd(p5, p7, p6, p8, p11); // LCD screen |
jbrown57 | 0:c48b28944702 | 10 | PwmOut r (p23); // r = red LED |
jbrown57 | 0:c48b28944702 | 11 | PwmOut g (p24); // g = green LED |
tollidal | 2:2621304bdec1 | 12 | HCSR04 sensor(p9, p20); // ultrasonic distance sensor |
tollidal | 2:2621304bdec1 | 13 | DHT sensorB(p16,DHT11); // DHT11 temp & humi sensor |
tollidal | 2:2621304bdec1 | 14 | DigitalOut fan(p18); //grove relay control DC fan |
tollidal | 2:2621304bdec1 | 15 | DigitalOut myled(p14); //assigning greenled as digital output pin14 |
tollidal | 2:2621304bdec1 | 16 | AnalogIn ldr(p15); //assigning ldr as analog input p15 |
tollidal | 2:2621304bdec1 | 17 | AnalogIn soil_moisture(p19); //soil moisture sensor |
tollidal | 2:2621304bdec1 | 18 | Servo myservo(p21); //servo |
tollidal | 2:2621304bdec1 | 19 | |
jbrown57 | 0:c48b28944702 | 20 | |
jbrown57 | 0:c48b28944702 | 21 | int main() |
jbrown57 | 0:c48b28944702 | 22 | { |
tollidal | 3:fdbd4ffb9ca6 | 23 | int error = 0; |
tollidal | 3:fdbd4ffb9ca6 | 24 | float h = 0.0f, c = 0.0f, f = 0.0f, k = 0.0f, dp = 0.0f, dpf = 0.0f; |
tollidal | 3:fdbd4ffb9ca6 | 25 | lcd.cls(); |
tollidal | 3:fdbd4ffb9ca6 | 26 | char tmpString[16]; |
tollidal | 3:fdbd4ffb9ca6 | 27 | int temp_format = 0; |
tollidal | 3:fdbd4ffb9ca6 | 28 | int humid_format = 0; |
tollidal | 3:fdbd4ffb9ca6 | 29 | float ldrout = 0.0; |
tollidal | 3:fdbd4ffb9ca6 | 30 | float range = 0.0005; |
tollidal | 3:fdbd4ffb9ca6 | 31 | float position = 0.5; |
tollidal | 3:fdbd4ffb9ca6 | 32 | float moisture_value = 0; |
jbrown57 | 0:c48b28944702 | 33 | |
tollidal | 3:fdbd4ffb9ca6 | 34 | while(true) { |
jbrown57 | 0:c48b28944702 | 35 | |
tollidal | 3:fdbd4ffb9ca6 | 36 | lcd.cls(); |
tollidal | 3:fdbd4ffb9ca6 | 37 | lcd.locate(25,5); |
tollidal | 3:fdbd4ffb9ca6 | 38 | lcd.printf("Tank Water Level"); |
tollidal | 3:fdbd4ffb9ca6 | 39 | lcd.locate(2,15); |
tollidal | 3:fdbd4ffb9ca6 | 40 | lcd.printf("Greenhouse Control System"); |
tollidal | 3:fdbd4ffb9ca6 | 41 | wait(2.5); |
jbrown57 | 0:c48b28944702 | 42 | |
tollidal | 3:fdbd4ffb9ca6 | 43 | lcd.cls(); //clear LCD |
tollidal | 3:fdbd4ffb9ca6 | 44 | Timer timer; |
tollidal | 3:fdbd4ffb9ca6 | 45 | { |
jbrown57 | 0:c48b28944702 | 46 | |
tollidal | 3:fdbd4ffb9ca6 | 47 | sensor.setRanges(0, 500); // water tank level 500mm |
tollidal | 3:fdbd4ffb9ca6 | 48 | lcd.print_bm(bitmEmptyTank,80,0); // print Empty Tank at location x=80, y=0 |
tollidal | 3:fdbd4ffb9ca6 | 49 | lcd.copy_to_lcd(); |
jbrown57 | 0:c48b28944702 | 50 | |
jbrown57 | 0:c48b28944702 | 51 | |
tollidal | 3:fdbd4ffb9ca6 | 52 | timer.reset(); |
tollidal | 3:fdbd4ffb9ca6 | 53 | timer.start(); |
tollidal | 3:fdbd4ffb9ca6 | 54 | sensor.startMeasurement(); |
jbrown57 | 0:c48b28944702 | 55 | |
tollidal | 3:fdbd4ffb9ca6 | 56 | } |
tollidal | 3:fdbd4ffb9ca6 | 57 | lcd.locate(20,5); |
tollidal | 3:fdbd4ffb9ca6 | 58 | lcd.printf("Distance:"); |
tollidal | 3:fdbd4ffb9ca6 | 59 | lcd.locate(20,17); |
tollidal | 3:fdbd4ffb9ca6 | 60 | lcd.printf ("%5.1f mm\r", sensor.getDistance_mm()); |
tollidal | 3:fdbd4ffb9ca6 | 61 | timer.stop(); |
tollidal | 3:fdbd4ffb9ca6 | 62 | wait_ms(500 - timer.read_ms()); // time the loop |
jbrown57 | 0:c48b28944702 | 63 | |
tollidal | 3:fdbd4ffb9ca6 | 64 | //turn RGB LED red if distance >400mm |
tollidal | 2:2621304bdec1 | 65 | |
tollidal | 3:fdbd4ffb9ca6 | 66 | if( sensor.getDistance_mm()< 400) r= 1; //red light on |
tollidal | 3:fdbd4ffb9ca6 | 67 | else r = 0; |
jbrown57 | 0:c48b28944702 | 68 | |
tollidal | 3:fdbd4ffb9ca6 | 69 | if( sensor.getDistance_mm()> 400) g=1; //green light on |
tollidal | 3:fdbd4ffb9ca6 | 70 | else g=0; //green light off |
jbrown57 | 0:c48b28944702 | 71 | |
jbrown57 | 0:c48b28944702 | 72 | |
tollidal | 3:fdbd4ffb9ca6 | 73 | if( sensor.getDistance_mm() < 40 ) lcd.print_bm(bitmTank,80,0); //full tank |
tollidal | 3:fdbd4ffb9ca6 | 74 | lcd.copy_to_lcd(); |
tollidal | 3:fdbd4ffb9ca6 | 75 | if( sensor.getDistance_mm() > 80 ) lcd.print_bm(bitmNinetyTank,80,0); //90% full tank |
tollidal | 3:fdbd4ffb9ca6 | 76 | lcd.copy_to_lcd(); |
tollidal | 3:fdbd4ffb9ca6 | 77 | if( sensor.getDistance_mm() > 120 ) lcd.print_bm(bitmEightyTank,80,0); //80% full tank |
tollidal | 3:fdbd4ffb9ca6 | 78 | lcd.copy_to_lcd(); |
tollidal | 3:fdbd4ffb9ca6 | 79 | if( sensor.getDistance_mm() > 160 ) lcd.print_bm(bitmSeventyTank,80,0); //70% full tank |
tollidal | 3:fdbd4ffb9ca6 | 80 | lcd.copy_to_lcd(); |
tollidal | 3:fdbd4ffb9ca6 | 81 | if( sensor.getDistance_mm() > 200 ) lcd.print_bm(bitmSixtyTank,80,0); //60% full tank |
tollidal | 3:fdbd4ffb9ca6 | 82 | lcd.copy_to_lcd(); |
tollidal | 3:fdbd4ffb9ca6 | 83 | if( sensor.getDistance_mm() > 240 ) lcd.print_bm(bitmFiftyTank,80,0); //50% full tank |
tollidal | 3:fdbd4ffb9ca6 | 84 | lcd.copy_to_lcd(); |
tollidal | 3:fdbd4ffb9ca6 | 85 | if( sensor.getDistance_mm() > 280 ) lcd.print_bm(bitmFourtyTank,80,0); //40% full tank |
tollidal | 3:fdbd4ffb9ca6 | 86 | lcd.copy_to_lcd(); |
tollidal | 3:fdbd4ffb9ca6 | 87 | if( sensor.getDistance_mm() > 320 ) lcd.print_bm(bitmThirtyTank,80,0); //30% full tank |
tollidal | 3:fdbd4ffb9ca6 | 88 | lcd.copy_to_lcd(); |
tollidal | 3:fdbd4ffb9ca6 | 89 | if( sensor.getDistance_mm() > 360 ) lcd.print_bm(bitmTwentyTank,80,0); //20% full tank |
tollidal | 3:fdbd4ffb9ca6 | 90 | lcd.copy_to_lcd(); |
tollidal | 3:fdbd4ffb9ca6 | 91 | if( sensor.getDistance_mm() > 400 ) lcd.print_bm(bitmTenTank,80,0); //10% full tank |
tollidal | 3:fdbd4ffb9ca6 | 92 | lcd.copy_to_lcd(); |
tollidal | 3:fdbd4ffb9ca6 | 93 | if( sensor.getDistance_mm() > 440 ) lcd.print_bm(bitmEmptyTank,80,0); //empty tank |
tollidal | 3:fdbd4ffb9ca6 | 94 | lcd.copy_to_lcd(); |
tollidal | 3:fdbd4ffb9ca6 | 95 | wait (2.5f); |
jbrown57 | 0:c48b28944702 | 96 | |
jbrown57 | 0:c48b28944702 | 97 | |
tollidal | 3:fdbd4ffb9ca6 | 98 | lcd.cls(); |
tollidal | 3:fdbd4ffb9ca6 | 99 | lcd.locate(15,5); |
tollidal | 3:fdbd4ffb9ca6 | 100 | lcd.printf("Temp & Humidi Level"); |
tollidal | 3:fdbd4ffb9ca6 | 101 | lcd.locate(2,15); |
tollidal | 3:fdbd4ffb9ca6 | 102 | lcd.printf("Greenhouse Control System"); |
tollidal | 3:fdbd4ffb9ca6 | 103 | wait(2.5); |
tollidal | 2:2621304bdec1 | 104 | |
tollidal | 3:fdbd4ffb9ca6 | 105 | lcd.cls(); //clear LCD |
tollidal | 3:fdbd4ffb9ca6 | 106 | |
tollidal | 3:fdbd4ffb9ca6 | 107 | |
tollidal | 3:fdbd4ffb9ca6 | 108 | wait(0.5f); |
tollidal | 3:fdbd4ffb9ca6 | 109 | error = sensorB.readData(); |
tollidal | 3:fdbd4ffb9ca6 | 110 | |
tollidal | 3:fdbd4ffb9ca6 | 111 | if (0 == error) { |
tollidal | 2:2621304bdec1 | 112 | |
tollidal | 3:fdbd4ffb9ca6 | 113 | c = sensorB.ReadTemperature(CELCIUS); |
tollidal | 3:fdbd4ffb9ca6 | 114 | f = sensorB.ReadTemperature(FARENHEIT); |
tollidal | 3:fdbd4ffb9ca6 | 115 | k = sensorB.ReadTemperature(KELVIN); |
tollidal | 3:fdbd4ffb9ca6 | 116 | h = sensorB.ReadHumidity(); |
tollidal | 3:fdbd4ffb9ca6 | 117 | dp = sensorB.CalcdewPoint(c, h); |
tollidal | 3:fdbd4ffb9ca6 | 118 | dpf = sensorB.CalcdewPointFast(c, h); |
tollidal | 3:fdbd4ffb9ca6 | 119 | } |
tollidal | 3:fdbd4ffb9ca6 | 120 | lcd.cls(); |
tollidal | 3:fdbd4ffb9ca6 | 121 | lcd.locate(0,0); |
tollidal | 3:fdbd4ffb9ca6 | 122 | lcd.printf("K: %4.2f, C: %4.2f, F %4.2f\n", k, c, f); |
tollidal | 3:fdbd4ffb9ca6 | 123 | lcd.locate(0,10); |
tollidal | 3:fdbd4ffb9ca6 | 124 | lcd.printf("H: %4.2f, D: %4.2f, DF: %4.2f\n", h, dp, dpf); |
tollidal | 3:fdbd4ffb9ca6 | 125 | lcd.locate(0,20); |
tollidal | 3:fdbd4ffb9ca6 | 126 | lcd.printf("Tempf: %d HumidF: %d\n", temp_format, humid_format); |
tollidal | 3:fdbd4ffb9ca6 | 127 | |
tollidal | 3:fdbd4ffb9ca6 | 128 | //turn fan on if temperature is above 26 |
tollidal | 3:fdbd4ffb9ca6 | 129 | if(c > 20) fan=1; //fan is on |
tollidal | 3:fdbd4ffb9ca6 | 130 | else fan=0; //Fan is off |
tollidal | 3:fdbd4ffb9ca6 | 131 | wait(2.5); |
tollidal | 3:fdbd4ffb9ca6 | 132 | //wait 2 seconds and check temp again |
jbrown57 | 0:c48b28944702 | 133 | |
jbrown57 | 0:c48b28944702 | 134 | |
tollidal | 3:fdbd4ffb9ca6 | 135 | //Light Sensor System |
tollidal | 2:2621304bdec1 | 136 | |
tollidal | 3:fdbd4ffb9ca6 | 137 | lcd.cls(); |
tollidal | 3:fdbd4ffb9ca6 | 138 | lcd.locate(25,5); |
tollidal | 3:fdbd4ffb9ca6 | 139 | lcd.printf("Light Level"); |
tollidal | 3:fdbd4ffb9ca6 | 140 | lcd.locate(2,15); |
tollidal | 3:fdbd4ffb9ca6 | 141 | lcd.printf("Greenhouse Control System"); |
tollidal | 3:fdbd4ffb9ca6 | 142 | wait(2.5); |
tollidal | 2:2621304bdec1 | 143 | |
tollidal | 3:fdbd4ffb9ca6 | 144 | ldrout=ldr.read(); //read ldr value |
tollidal | 2:2621304bdec1 | 145 | |
tollidal | 3:fdbd4ffb9ca6 | 146 | lcd.cls(); |
tollidal | 3:fdbd4ffb9ca6 | 147 | lcd.locate(10,10); |
tollidal | 3:fdbd4ffb9ca6 | 148 | lcd.printf("Light Level is: %f\n :",ldrout); //print ldr value on LCD |
tollidal | 3:fdbd4ffb9ca6 | 149 | if(ldrout < 0.6) { //if ldr value is smaller than 0.6 green LED turns on |
tollidal | 2:2621304bdec1 | 150 | |
tollidal | 3:fdbd4ffb9ca6 | 151 | myled = 1; //LED on |
tollidal | 3:fdbd4ffb9ca6 | 152 | } else { |
tollidal | 3:fdbd4ffb9ca6 | 153 | myled=0; //LED off |
tollidal | 3:fdbd4ffb9ca6 | 154 | } |
tollidal | 3:fdbd4ffb9ca6 | 155 | wait(2.5); |
tollidal | 2:2621304bdec1 | 156 | |
tollidal | 3:fdbd4ffb9ca6 | 157 | //Soil Moisture System |
tollidal | 3:fdbd4ffb9ca6 | 158 | lcd.cls(); |
tollidal | 3:fdbd4ffb9ca6 | 159 | lcd.locate(25,5); |
tollidal | 3:fdbd4ffb9ca6 | 160 | lcd.printf("Soil Moisture Level"); |
tollidal | 3:fdbd4ffb9ca6 | 161 | lcd.locate(2,15); |
tollidal | 3:fdbd4ffb9ca6 | 162 | lcd.printf("Greenhouse Control System"); |
tollidal | 3:fdbd4ffb9ca6 | 163 | wait(2.5); |
tollidal | 2:2621304bdec1 | 164 | |
tollidal | 3:fdbd4ffb9ca6 | 165 | moisture_value = soil_moisture.read () *3.3; //soil moisture value |
tollidal | 3:fdbd4ffb9ca6 | 166 | |
tollidal | 3:fdbd4ffb9ca6 | 167 | lcd.cls(); |
tollidal | 3:fdbd4ffb9ca6 | 168 | lcd.locate(0,0); //text location on screen |
tollidal | 3:fdbd4ffb9ca6 | 169 | lcd.printf("Moisture reading is: %1.2f\n", moisture_value ); //display soil moisture value as a percentage |
tollidal | 2:2621304bdec1 | 170 | |
tollidal | 3:fdbd4ffb9ca6 | 171 | if (moisture_value < 1) { //if soil moisture level is between 30 and 60 moisture level okay |
tollidal | 3:fdbd4ffb9ca6 | 172 | lcd.locate(0,10); |
tollidal | 3:fdbd4ffb9ca6 | 173 | lcd.printf("Moisture level not okay"); |
tollidal | 3:fdbd4ffb9ca6 | 174 | position = 0.0; |
tollidal | 2:2621304bdec1 | 175 | } |
tollidal | 3:fdbd4ffb9ca6 | 176 | else { |
tollidal | 3:fdbd4ffb9ca6 | 177 | lcd.locate(0,10); |
tollidal | 3:fdbd4ffb9ca6 | 178 | lcd.printf("Moisture level is okay"); |
tollidal | 3:fdbd4ffb9ca6 | 179 | position = 1.0; |
tollidal | 3:fdbd4ffb9ca6 | 180 | } |
tollidal | 3:fdbd4ffb9ca6 | 181 | myservo.write(position); |
tollidal | 3:fdbd4ffb9ca6 | 182 | wait(2.5); |
tollidal | 2:2621304bdec1 | 183 | } |
jbrown57 | 0:c48b28944702 | 184 | } |