Strawberry Greenhouse V3

Dependencies:   mbed C12832 Servo HCSR04 DHT DHT11

Files at this revision

API Documentation at this revision

Comitter:
tollidal
Date:
Tue Oct 19 20:21:02 2021 +0000
Parent:
2:2621304bdec1
Commit message:
Strawberry Greenhouse complete V3;

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 2621304bdec1 -r fdbd4ffb9ca6 main.cpp
--- a/main.cpp	Tue Oct 19 18:55:00 2021 +0000
+++ b/main.cpp	Tue Oct 19 20:21:02 2021 +0000
@@ -19,262 +19,166 @@
 
 
 int main()
-
 {
-    while(1)
-    {
-
-
-//water tank level system
+    int error = 0;
+    float h = 0.0f, c = 0.0f, f = 0.0f, k = 0.0f, dp = 0.0f, dpf = 0.0f;
+    lcd.cls();
+    char tmpString[16];
+    int temp_format = 0;
+    int humid_format = 0;
+    float ldrout = 0.0;
+    float range = 0.0005;
+    float position = 0.5;
+    float moisture_value = 0;
 
-//welcome message on LCD
-
-        lcd.cls();
-
-    lcd.locate(25,5);
-
-    lcd.printf("Tank Water Level");
+    while(true) {
 
-    lcd.locate(2,15);
-
-    lcd.printf("Greenhouse Control System");
-
-    wait(2.5);
-
-    lcd.cls();      //clear LCD
+                lcd.cls();
+                lcd.locate(25,5);
+                lcd.printf("Tank Water Level");
+                lcd.locate(2,15);
+                lcd.printf("Greenhouse Control System");
+                wait(2.5);
 
-
-    Timer timer;
+                lcd.cls();      //clear LCD
+                Timer timer;
+                {
 
-    {
-
-        sensor.setRanges(0, 500);       // water tank level 500mm
-
+                    sensor.setRanges(0, 500);       // water tank level 500mm
+                    lcd.print_bm(bitmEmptyTank,80,0);     // print Empty Tank at location x=80, y=0
+                    lcd.copy_to_lcd();
 
 
-        lcd.print_bm(bitmEmptyTank,80,0);     // print Empty Tank at location x=80, y=0
-        lcd.copy_to_lcd();
-        //if needed:   lcd.locate(0,10);
-        //if needed:   lcd.printf("Min. range = %g cm\n\rMax. range = %g cm\n\r",
-        //If needed:   sensor.getMinRange(), sensor.getMaxRange());
+                    timer.reset();
+                    timer.start();
+                    sensor.startMeasurement();
 
-        
-            timer.reset();
-            timer.start();
-            sensor.startMeasurement();
-            /*while(!sensor.isNewDataReady()) {*/
-                // wait for new data
-                // waiting time depends on the distance
-            }
-            lcd.locate(20,5);
-            lcd.printf("Distance:");
-            lcd.locate(20,17);
-            lcd.printf ("%5.1f mm\r", sensor.getDistance_mm());
-            timer.stop();
-            wait_ms(500 - timer.read_ms()); // time the loop
+                }
+                lcd.locate(20,5);
+                lcd.printf("Distance:");
+                lcd.locate(20,17);
+                lcd.printf ("%5.1f mm\r", sensor.getDistance_mm());
+                timer.stop();
+                wait_ms(500 - timer.read_ms()); // time the loop
 
-            //turn RGB LED red if distance >400mm
+                //turn RGB LED red if distance >400mm
 
-            if( sensor.getDistance_mm()< 400) r= 1; //red light on
-            else r = 0;
+                if( sensor.getDistance_mm()< 400) r= 1; //red light on
+                else r = 0;
 
-            if( sensor.getDistance_mm()> 400) g=1; //green light on
-            else g=0;         //green light off
+                if( sensor.getDistance_mm()> 400) g=1; //green light on
+                else g=0;         //green light off
 
 
-            if( sensor.getDistance_mm() < 40 )  lcd.print_bm(bitmTank,80,0); //full tank
-            lcd.copy_to_lcd();
-            if( sensor.getDistance_mm() > 80 )  lcd.print_bm(bitmNinetyTank,80,0); //90% full tank
-            lcd.copy_to_lcd();
-            if( sensor.getDistance_mm() > 120  )  lcd.print_bm(bitmEightyTank,80,0); //80% full tank
-            lcd.copy_to_lcd();
-            if( sensor.getDistance_mm() > 160 )  lcd.print_bm(bitmSeventyTank,80,0); //70% full tank
-            lcd.copy_to_lcd();
-            if( sensor.getDistance_mm() > 200 )  lcd.print_bm(bitmSixtyTank,80,0); //60% full tank
-            lcd.copy_to_lcd();
-            if( sensor.getDistance_mm() > 240 )  lcd.print_bm(bitmFiftyTank,80,0); //50% full tank
-            lcd.copy_to_lcd();
-            if( sensor.getDistance_mm() > 280 )  lcd.print_bm(bitmFourtyTank,80,0); //40% full tank
-            lcd.copy_to_lcd();
-            if( sensor.getDistance_mm() > 320 )  lcd.print_bm(bitmThirtyTank,80,0); //30% full tank
-            lcd.copy_to_lcd();
-            if( sensor.getDistance_mm() > 360 )  lcd.print_bm(bitmTwentyTank,80,0); //20% full tank
-            lcd.copy_to_lcd();
-            if( sensor.getDistance_mm() > 400 )  lcd.print_bm(bitmTenTank,80,0); //10% full tank
-            lcd.copy_to_lcd();
-            if( sensor.getDistance_mm() > 440 )  lcd.print_bm(bitmEmptyTank,80,0); //empty tank
-            lcd.copy_to_lcd();
-
-
-
-            wait (10.0f);
-
-
-//Temperature Control System
-
-
-            //system welcome message on LCD
-
-            lcd.cls();
-
-            lcd.locate(15,5);
-
-            lcd.printf("Temp & Humidi Level");
-
-            lcd.locate(2,15);
-
-            lcd.printf("Greenhouse Control System");
-
-            wait(2.5);
-
-            lcd.cls();      //clear LCD
+                if( sensor.getDistance_mm() < 40 )  lcd.print_bm(bitmTank,80,0); //full tank
+                lcd.copy_to_lcd();
+                if( sensor.getDistance_mm() > 80 )  lcd.print_bm(bitmNinetyTank,80,0); //90% full tank
+                lcd.copy_to_lcd();
+                if( sensor.getDistance_mm() > 120  )  lcd.print_bm(bitmEightyTank,80,0); //80% full tank
+                lcd.copy_to_lcd();
+                if( sensor.getDistance_mm() > 160 )  lcd.print_bm(bitmSeventyTank,80,0); //70% full tank
+                lcd.copy_to_lcd();
+                if( sensor.getDistance_mm() > 200 )  lcd.print_bm(bitmSixtyTank,80,0); //60% full tank
+                lcd.copy_to_lcd();
+                if( sensor.getDistance_mm() > 240 )  lcd.print_bm(bitmFiftyTank,80,0); //50% full tank
+                lcd.copy_to_lcd();
+                if( sensor.getDistance_mm() > 280 )  lcd.print_bm(bitmFourtyTank,80,0); //40% full tank
+                lcd.copy_to_lcd();
+                if( sensor.getDistance_mm() > 320 )  lcd.print_bm(bitmThirtyTank,80,0); //30% full tank
+                lcd.copy_to_lcd();
+                if( sensor.getDistance_mm() > 360 )  lcd.print_bm(bitmTwentyTank,80,0); //20% full tank
+                lcd.copy_to_lcd();
+                if( sensor.getDistance_mm() > 400 )  lcd.print_bm(bitmTenTank,80,0); //10% full tank
+                lcd.copy_to_lcd();
+                if( sensor.getDistance_mm() > 440 )  lcd.print_bm(bitmEmptyTank,80,0); //empty tank
+                lcd.copy_to_lcd();
+                wait (2.5f);
 
 
-            int error = 0;
-            float h = 0.0f, c = 0.0f, f = 0.0f, k = 0.0f, dp = 0.0f, dpf = 0.0f;
-            lcd.cls();
-            char tmpString[16];
-            int temp_format = 0;
-            int humid_format = 0;
+                lcd.cls();
+                lcd.locate(15,5);
+                lcd.printf("Temp & Humidi Level");
+                lcd.locate(2,15);
+                lcd.printf("Greenhouse Control System");
+                wait(2.5);
 
-            //while(1)
-            
+                lcd.cls();      //clear LCD
+        
+
+        wait(0.5f);
+        error = sensorB.readData();
+
+        if (0 == error) {
 
-                wait(0.5f);
-                error = sensorB.readData();
-                if (0 == error) {
-                    c   = sensorB.ReadTemperature(CELCIUS);
-                    f   = sensorB.ReadTemperature(FARENHEIT);
-                    k   = sensorB.ReadTemperature(KELVIN);
-                    h   = sensorB.ReadHumidity();
-                    dp  = sensorB.CalcdewPoint(c, h);
-                    dpf = sensorB.CalcdewPointFast(c, h);
-                    printf("Temperature in Kelvin: %4.2f, Celcius: %4.2f, Farenheit %4.2f\n", k, c, f);
-                    printf("Humidity is %4.2f, Dewpoint: %4.2f, Dewpoint fast: %4.2f\n", h, dp, dpf);
-                    printf("Tempformat: %d HumidFormat: %d\n", temp_format, humid_format);
-                    switch(temp_format) {
-                        case 0 :
-                            sprintf(tmpString, " Temp: %4.2fC", c);
-                            break;
-                        case 1 :
-                            sprintf(tmpString, " Temp: %4.2fF", f);
-                            break;
-                        case 2 :
-                            sprintf(tmpString, " Temp: %4.2fK", k);
-                            break;
-                    
-
-                    //display the results onto the LCD
-                    lcd.locate(0,0);
-                    lcd.printf("Temperature: %4.2fC", c);
-                    lcd.locate(0,10);
-                    lcd.printf("Humidity: %4.2f", h);
-                    wait(2);
-
-                    //turn fan on if temperature is above 26
-                    if(c > 26) fan=1;   //fan is on
-                    else fan=0;         //Fan is off
-
-
-                    //wait 2 seconds and check temp again
-                    wait(10.0);
-
-
+            c   = sensorB.ReadTemperature(CELCIUS);
+            f   = sensorB.ReadTemperature(FARENHEIT);
+            k   = sensorB.ReadTemperature(KELVIN);
+            h   = sensorB.ReadHumidity();
+            dp  = sensorB.CalcdewPoint(c, h);
+            dpf = sensorB.CalcdewPointFast(c, h);
+        }
+        lcd.cls();
+        lcd.locate(0,0);
+        lcd.printf("K: %4.2f, C: %4.2f, F %4.2f\n", k, c, f);
+        lcd.locate(0,10);
+        lcd.printf("H: %4.2f, D: %4.2f, DF: %4.2f\n", h, dp, dpf);
+        lcd.locate(0,20);
+        lcd.printf("Tempf: %d HumidF: %d\n", temp_format, humid_format);
+        
+        //turn fan on if temperature is above 26
+        if(c > 20) fan=1;   //fan is on
+        else fan=0;         //Fan is off
+        wait(2.5);
+        //wait 2 seconds and check temp again
 
 
-//Light Sensor System
-
-                    lcd.cls();
+        //Light Sensor System
 
-                    lcd.locate(25,5);
+        lcd.cls();
+        lcd.locate(25,5);
+        lcd.printf("Light Level");
+        lcd.locate(2,15);
+        lcd.printf("Greenhouse Control System");
+        wait(2.5);
 
-                    lcd.printf("Light Level");
+        ldrout=ldr.read();                        //read ldr value
 
-                    lcd.locate(2,15);
-
-                    lcd.printf("Greenhouse Control System");
+        lcd.cls();
+        lcd.locate(10,10);
+        lcd.printf("Light Level is: %f\n :",ldrout);       //print ldr value on LCD
+        if(ldrout < 0.6) {                                //if ldr value is smaller than 0.6 green LED turns on
 
-                    wait(2.5);
-
-                    lcd.cls();
+            myled = 1;                                  //LED on
+        } else {
+            myled=0;                                    //LED off
+        }
+        wait(2.5);
 
-                    //while(1) 
-                    float ldrout=ldr.read();                        //read ldr value
-                    lcd.locate(10,10);
-                    lcd.printf("Light Level is: %f\n :",ldrout);       //print ldr value on LCD
-                    if(ldrout<0.6) {                                //if ldr value is smaller than 0.6 green LED turns on
+        //Soil Moisture System
+        lcd.cls();
+        lcd.locate(25,5);
+        lcd.printf("Soil Moisture Level");
+        lcd.locate(2,15);
+        lcd.printf("Greenhouse Control System");
+        wait(2.5);
 
-                        myled = 1;                                  //LED on
-                    } else {
-                        myled=0;                                    //LED off
+        moisture_value = soil_moisture.read () *3.3;                                               //soil moisture value
+       
+        lcd.cls();
+        lcd.locate(0,0);                                                               //text location on screen
+        lcd.printf("Moisture reading is: %1.2f\n", moisture_value );                     //display soil moisture value as a percentage
 
-                        wait(10.0);
-                        lcd.cls();
-                    
-                }
-
-            }
+        if (moisture_value < 1)  {                                 //if soil moisture level is between 30 and 60 moisture level okay
+            lcd.locate(0,10);
+            lcd.printf("Moisture level not okay");
+            position = 0.0;
         }
+        else {
+            lcd.locate(0,10);
+            lcd.printf("Moisture level is okay");
+            position = 1.0;
+        }  
+        myservo.write(position);
+        wait(2.5);
     }
 }
-
-/*
-
-//Soil Moisture System
-
-//lcd.cls();
-
-                    lcd.locate(25,5);
-
-                    lcd.printf("Soil Moisture Level");
-
-                    lcd.locate(2,15);
-
-                    lcd.printf("Greenhouse Control System");
-
-                    wait(2.5);
-
-
-float range = 0.0005;
-    float position = 0.5;
-
-    float moisture_value = 0.0f;
-
-
-    while(1) {
-
-        moisture_value = soil_moisture*100;                                               //soil moisture value
-        lcd.locate(0,0);                                                               //text location on screen
-        lcd.cls();
-        lcd.printf("Moisture reading is: %2.2f\n", moisture_value);                     //display soil moisture value as a percentage
-        wait(1.0f);
-        //  lcd.printf("Moisture reading is: %2.2f\n", value);
-
-        if (moisture_value > 30&& moisture_value <= 50) {                                 //if soil moisture level is between 30 and 60 moisture level okay
-            lcd.printf("Moisture level okay");
-            position = 0.0;
-        }
-
-        else if (moisture_value <= 30) {                                        //if soil moisture level is below 30, soil is dry
-            position = 1.0;                                             //move servo to open position to water plant
-
-
-        } else {
-            position = 0.0;                                             //if soil moisture is above 65 soil is wet, do not water
-        }
-
-
-
-        lcd.printf("position = %.1f, range = +/-%0.4f\n", position, range);
-        myservo.calibrate(range, 45.0);
-        myservo = position;
-
-        wait (10);
-
-    }
-}
-
-
-*/
-
-