Strawberry_Greenhouse_V1.0

Dependencies:   mbed C12832 Servo HCSR04 DHT DHT11

main.cpp

Committer:
tollidal
Date:
2021-10-19
Revision:
2:2621304bdec1
Parent:
1:81cec44cec2e
Child:
3:fdbd4ffb9ca6

File content as of revision 2:2621304bdec1:

#include "mbed.h"
#include "C12832.h"
#include "tank.h"
#include "HCSR04.h"
#include "DHT.h"
#include "Servo.h"


C12832 lcd(p5, p7, p6, p8, p11);        // LCD screen
PwmOut r (p23);                         // r = red LED
PwmOut g (p24);                         // g = green LED
HCSR04 sensor(p9, p20);                 // ultrasonic distance sensor
DHT sensorB(p16,DHT11);                 // DHT11 temp & humi sensor
DigitalOut fan(p18);                     //grove relay control DC fan
DigitalOut myled(p14);                  //assigning greenled as digital output pin14
AnalogIn ldr(p15);                      //assigning ldr as analog input p15
AnalogIn soil_moisture(p19);             //soil moisture sensor
Servo myservo(p21);                      //servo


int main()

{
    while(1)
    {


//water tank level system

//welcome message on LCD

        lcd.cls();

    lcd.locate(25,5);

    lcd.printf("Tank Water Level");

    lcd.locate(2,15);

    lcd.printf("Greenhouse Control System");

    wait(2.5);

    lcd.cls();      //clear LCD


    Timer timer;

    {

        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();
        //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();
            /*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

            //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) 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


            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;

            //while(1)
            

                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);




//Light Sensor System

                    lcd.cls();

                    lcd.locate(25,5);

                    lcd.printf("Light Level");

                    lcd.locate(2,15);

                    lcd.printf("Greenhouse Control System");

                    wait(2.5);

                    lcd.cls();

                    //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

                        myled = 1;                                  //LED on
                    } else {
                        myled=0;                                    //LED off

                        wait(10.0);
                        lcd.cls();
                    
                }

            }
        }
    }
}

/*

//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);

    }
}


*/