const int red = 9; //1
const int yel = 10; //2
const int green = 11; //3
const int b1 = 2;
const int b2 = 3;
const int b3 = 4;
const int th1 = 30;
const int th2 = 70;
volatile boolean rstate = LOW;
volatile boolean ystate = LOW;
volatile boolean gstate = LOW;
void setup() {
Serial.begin(9600);
pinMode(red, OUTPUT);
pinMode(yel, OUTPUT);
pinMode(green, OUTPUT);
pinMode(b1, INPUT_PULLUP);
pinMode(b2, INPUT_PULLUP);
pinMode(b3, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(b1), buttonISR1, FALLING);
attachInterrupt(digitalPinToInterrupt(b2), buttonISR2, FALLING);
attachInterrupt(digitalPinToInterrupt(b3), buttonISR3, FALLING);
}
void loop() {
int ms1 = analogRead(A0);
int ms2 = analogRead(A1);
int ms3 = analogRead(A2);
autoPump(ms1, red, b1, rstate, 1);
autoPump(ms2, yel, b2, ystate, 2);
autoPump(ms3, green, b3, gstate, 3);
Serial.println();
delay(3000);
}
void autoPump(int ms, int pump, int b, volatile boolean& state, int n) {
ms = map(ms, 0, 1023, 0, 100);
Serial.print("Moisture% of plant ");
Serial.print(n);
Serial.print(" is ");
Serial.println(ms);
if (ms < th1) {
digitalWrite(pump, HIGH);
state = HIGH;
} else if (ms > th2) {
digitalWrite(pump, LOW);
state = LOW;
}
}
void buttonISR1() {
rstate = !rstate;
digitalWrite(red, rstate);
delay(2000);
}
void buttonISR2() {
ystate = !ystate;
digitalWrite(yel, ystate);
delay(2000);
}
void buttonISR3() {
gstate = !gstate;
digitalWrite(green, gstate);
delay(2000);
}