Advertisement
Guest User

Garduino

a guest
Apr 21st, 2020
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.17 KB | None | 0 0
  1. // Garduino with LCD, Soil Sensor and LDR for sunlight detection
  2. // LDR connects to AO
  3. // Soil Sensor on Digital 8
  4. // Pump connects via mosfet/relay to pin 13
  5. // Backlight Anode to pin 6 to enable backlight control
  6. // LCD rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2
  7.  
  8. #include <LiquidCrystal.h>
  9. const int lightsensor = A0;   // the number of the sun sensor pin
  10. const int soilsensor = 8;  // the number of the soil sensor pin
  11. const int pumpPin = 13;      // the number of the water pump pin
  12. const int BLPin = 6;
  13. const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
  14. LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
  15.  
  16. // variables will change:
  17. int soilstate = 0;         // variable for reading the sensor status
  18. int lightstate = 0;        // variable for sun light
  19. void setup() {
  20.   pinMode(BLPin, OUTPUT); // initialize lcd backlight pin
  21.   pinMode(pumpPin, OUTPUT);   // initialize the pump pin as an output:
  22.   pinMode(soilsensor, INPUT);   // initialize the soil sensor pin as an input:
  23.   lcd.begin(16, 2);   // set up the LCD's number of columns and rows:
  24.   Serial.begin(9600); //configure serial to talk to computer
  25.    }
  26.  
  27. void loop() {
  28.   soilstate = digitalRead(soilsensor);   // read the state of the soil sensor value:
  29.   lightstate = analogRead(lightsensor);   // read the state of the light sensor value:
  30.   Serial.println(lightstate);
  31.   Serial.println(soilstate);  
  32.     if (soilstate == HIGH && lightstate < 450 ) {// turn pump on
  33.         digitalWrite(pumpPin, HIGH);
  34.         digitalWrite(BLPin, HIGH);
  35.         lcd.display();
  36.         lcd.print("Soil Dry");
  37.         lcd.clear();
  38.   } else if (soilstate == LOW && lightstate < 450) {// turn pump off
  39.         digitalWrite(pumpPin, LOW);
  40.         digitalWrite(BLPin, HIGH);
  41.         lcd.display();
  42.         lcd.print("Soil Wet");
  43.         lcd.clear();
  44.   } else if (soilstate == HIGH && lightstate > 450) {// turn pump and lcd off
  45.         digitalWrite(pumpPin, LOW);
  46.         digitalWrite(BLPin, LOW);
  47.         lcd.noDisplay();
  48.         lcd.clear();
  49.   } else if (soilstate == LOW && lightstate > 450) {//turn pump and lcd off
  50.         digitalWrite(pumpPin, LOW);
  51.         digitalWrite(BLPin, LOW);
  52.         lcd.noDisplay();
  53.         lcd.clear();
  54.         }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement