Here is the code that I wrote to make the Arduino turn on/off the little water pump based on the soil humidity:
/* * This is a sketch for turning on a water pump * when a dryness threshold is met. */ // analog pin 0 int moistureSensor = 0; // digital pin 12 int redLed = 12; // digital pin 11 int greenLed = 11; // digital pin 7 int pumpPin = 7; // variable to hold moisture value int moistureVal; void setup() { Serial.begin(9600); pinMode(greenLed, OUTPUT); pinMode(redLed, OUTPUT); pinMode(pumpPin, OUTPUT); } void loop() { // Read from the moisture sensor moistureVal = analogRead(moistureSensor); Serial.println(moistureVal); if (moistureVal < 500) { // turn on the water pump digitalWrite(pumpPin, LOW); digitalWrite(redLed, HIGH); digitalWrite(greenLed, LOW); // water for 2 seconds delay(2000); } else { // turn the pump off digitalWrite(pumpPin, HIGH); digitalWrite(greenLed, HIGH); digitalWrite(redLed, LOW); } delay(1000); // wait }
0 comments:
Post a Comment