Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed C12832 Servo HCSR04 DHT DHT11
main.cpp
00001 #include "mbed.h" 00002 #include "C12832.h" 00003 #include "tank.h" 00004 #include "HCSR04.h" 00005 #include "DHT.h" 00006 #include "Servo.h" 00007 00008 00009 C12832 lcd(p5, p7, p6, p8, p11); // LCD screen 00010 PwmOut r (p23); // r = red LED 00011 PwmOut g (p24); // g = green LED 00012 HCSR04 sensor(p9, p20); // ultrasonic distance sensor 00013 DHT sensorB(p16,DHT11); // DHT11 temp & humi sensor 00014 DigitalOut fan(p18); //grove relay control DC fan 00015 DigitalOut myled(p14); //assigning greenled as digital output pin14 00016 AnalogIn ldr(p15); //assigning ldr as analog input p15 00017 AnalogIn soil_moisture(p19); //soil moisture sensor 00018 Servo myservo(p21); //servo 00019 00020 00021 int main() 00022 { 00023 int error = 0; 00024 float h = 0.0f, c = 0.0f, f = 0.0f, k = 0.0f, dp = 0.0f, dpf = 0.0f; 00025 lcd.cls(); 00026 char tmpString[16]; 00027 int temp_format = 0; 00028 int humid_format = 0; 00029 float ldrout = 0.0; 00030 float range = 0.0005; 00031 float position = 0.5; 00032 float moisture_value = 0; 00033 00034 while(true) { 00035 00036 lcd.cls(); 00037 lcd.locate(25,5); 00038 lcd.printf("Tank Water Level"); 00039 lcd.locate(2,15); 00040 lcd.printf("Greenhouse Control System"); 00041 wait(2.5); 00042 00043 lcd.cls(); //clear LCD 00044 Timer timer; 00045 { 00046 00047 sensor.setRanges(0, 500); // water tank level 500mm 00048 lcd.print_bm(bitmEmptyTank,80,0); // print Empty Tank at location x=80, y=0 00049 lcd.copy_to_lcd(); 00050 00051 00052 timer.reset(); 00053 timer.start(); 00054 sensor.startMeasurement(); 00055 00056 } 00057 lcd.locate(20,5); 00058 lcd.printf("Distance:"); 00059 lcd.locate(20,17); 00060 lcd.printf ("%5.1f mm\r", sensor.getDistance_mm()); 00061 timer.stop(); 00062 wait_ms(500 - timer.read_ms()); // time the loop 00063 00064 //turn RGB LED red if distance >400mm 00065 00066 if( sensor.getDistance_mm()< 400) r= 1; //red light on 00067 else r = 0; 00068 00069 if( sensor.getDistance_mm()> 400) g=1; //green light on 00070 else g=0; //green light off 00071 00072 00073 if( sensor.getDistance_mm() < 40 ) lcd.print_bm(bitmTank,80,0); //full tank 00074 lcd.copy_to_lcd(); 00075 if( sensor.getDistance_mm() > 80 ) lcd.print_bm(bitmNinetyTank,80,0); //90% full tank 00076 lcd.copy_to_lcd(); 00077 if( sensor.getDistance_mm() > 120 ) lcd.print_bm(bitmEightyTank,80,0); //80% full tank 00078 lcd.copy_to_lcd(); 00079 if( sensor.getDistance_mm() > 160 ) lcd.print_bm(bitmSeventyTank,80,0); //70% full tank 00080 lcd.copy_to_lcd(); 00081 if( sensor.getDistance_mm() > 200 ) lcd.print_bm(bitmSixtyTank,80,0); //60% full tank 00082 lcd.copy_to_lcd(); 00083 if( sensor.getDistance_mm() > 240 ) lcd.print_bm(bitmFiftyTank,80,0); //50% full tank 00084 lcd.copy_to_lcd(); 00085 if( sensor.getDistance_mm() > 280 ) lcd.print_bm(bitmFourtyTank,80,0); //40% full tank 00086 lcd.copy_to_lcd(); 00087 if( sensor.getDistance_mm() > 320 ) lcd.print_bm(bitmThirtyTank,80,0); //30% full tank 00088 lcd.copy_to_lcd(); 00089 if( sensor.getDistance_mm() > 360 ) lcd.print_bm(bitmTwentyTank,80,0); //20% full tank 00090 lcd.copy_to_lcd(); 00091 if( sensor.getDistance_mm() > 400 ) lcd.print_bm(bitmTenTank,80,0); //10% full tank 00092 lcd.copy_to_lcd(); 00093 if( sensor.getDistance_mm() > 440 ) lcd.print_bm(bitmEmptyTank,80,0); //empty tank 00094 lcd.copy_to_lcd(); 00095 wait (2.5f); 00096 00097 00098 lcd.cls(); 00099 lcd.locate(15,5); 00100 lcd.printf("Temp & Humidi Level"); 00101 lcd.locate(2,15); 00102 lcd.printf("Greenhouse Control System"); 00103 wait(2.5); 00104 00105 lcd.cls(); //clear LCD 00106 00107 00108 wait(0.5f); 00109 error = sensorB.readData(); 00110 00111 if (0 == error) { 00112 00113 c = sensorB.ReadTemperature(CELCIUS); 00114 f = sensorB.ReadTemperature(FARENHEIT); 00115 k = sensorB.ReadTemperature(KELVIN); 00116 h = sensorB.ReadHumidity(); 00117 dp = sensorB.CalcdewPoint(c, h); 00118 dpf = sensorB.CalcdewPointFast(c, h); 00119 } 00120 lcd.cls(); 00121 lcd.locate(0,0); 00122 lcd.printf("K: %4.2f, C: %4.2f, F %4.2f\n", k, c, f); 00123 lcd.locate(0,10); 00124 lcd.printf("H: %4.2f, D: %4.2f, DF: %4.2f\n", h, dp, dpf); 00125 lcd.locate(0,20); 00126 lcd.printf("Tempf: %d HumidF: %d\n", temp_format, humid_format); 00127 00128 //turn fan on if temperature is above 26 00129 if(c > 20) fan=1; //fan is on 00130 else fan=0; //Fan is off 00131 wait(2.5); 00132 //wait 2 seconds and check temp again 00133 00134 00135 //Light Sensor System 00136 00137 lcd.cls(); 00138 lcd.locate(25,5); 00139 lcd.printf("Light Level"); 00140 lcd.locate(2,15); 00141 lcd.printf("Greenhouse Control System"); 00142 wait(2.5); 00143 00144 ldrout=ldr.read(); //read ldr value 00145 00146 lcd.cls(); 00147 lcd.locate(10,10); 00148 lcd.printf("Light Level is: %f\n :",ldrout); //print ldr value on LCD 00149 if(ldrout < 0.6) { //if ldr value is smaller than 0.6 green LED turns on 00150 00151 myled = 1; //LED on 00152 } else { 00153 myled=0; //LED off 00154 } 00155 wait(2.5); 00156 00157 //Soil Moisture System 00158 lcd.cls(); 00159 lcd.locate(25,5); 00160 lcd.printf("Soil Moisture Level"); 00161 lcd.locate(2,15); 00162 lcd.printf("Greenhouse Control System"); 00163 wait(2.5); 00164 00165 moisture_value = soil_moisture.read () *3.3; //soil moisture value 00166 00167 lcd.cls(); 00168 lcd.locate(0,0); //text location on screen 00169 lcd.printf("Moisture reading is: %1.2f\n", moisture_value ); //display soil moisture value as a percentage 00170 00171 if (moisture_value < 1) { //if soil moisture level is between 30 and 60 moisture level okay 00172 lcd.locate(0,10); 00173 lcd.printf("Moisture level not okay"); 00174 position = 0.0; 00175 } 00176 else { 00177 lcd.locate(0,10); 00178 lcd.printf("Moisture level is okay"); 00179 position = 1.0; 00180 } 00181 myservo.write(position); 00182 wait(2.5); 00183 } 00184 }
Generated on Wed Jul 27 2022 14:52:40 by
1.7.2