const int red = 9;
const int yel = 10;
const int green = 11;
const int b1 = 2;
const int b2 = 3;
const int b3 = 4;
const int th1 = 30; //thresholds
const int th2 = 70;
volatile boolean rstate = LOW; //led output
volatile boolean ystate = LOW;
volatile boolean gstate = LOW;
volatile boolean m1 = LOW; //manual override flags
volatile boolean m2 = LOW;
volatile boolean m3 = LOW;
void makeInterruptPin() {
PCICR |= bit (PCIE2);
//b00000100
// enable interrupt for D0-D7
//Pin Change Interrupt Control Register, PCIE2 = D0-D7
PCMSK2 |= bit (PCINT20);
// enable interrupt for D4
//PCMSK2 register controls which bit triggers state change interrupts
}
ISR (PCINT2_vect) {
if (digitalRead(4) == LOW) {
m3 = HIGH;
gstate = !gstate;
digitalWrite(green, gstate);
Serial.println("isr green");
}
}
void buttonISR1() {
m1 = HIGH;
rstate = !rstate;
digitalWrite(red, rstate);
Serial.println("isr red");
// delay(2000);
}
void buttonISR2() {
m2 = HIGH;
ystate = !ystate;
digitalWrite(yel, ystate);
Serial.println("isr yellow");
// delay(200);
}
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);
makeInterruptPin(); //changing b3 to external interrupt
attachInterrupt(digitalPinToInterrupt(b1), buttonISR1, FALLING);
attachInterrupt(digitalPinToInterrupt(b2), buttonISR2, FALLING);
Serial.println("Begin");
}
void loop() {
int ms1 = analogRead(A0);
int ms2 = analogRead(A1);
int ms3 = analogRead(A2);
ms1 = map(ms1, 0, 1023, 0, 100);
ms2 = map(ms2, 0, 1023, 0, 100);
ms3 = map(ms3, 0, 1023, 0, 100);
//serial monitor commands
while(Serial.available()){
char c = Serial.read();
//printing sensor values on serial monitor
if(c=='p'){
printMoisturePercentages(ms1, ms2, ms3);
}
else{
//turn off or turn on (toggle) the pumps on command
MonitorCommand(c);
}
}
if(!m1){
autoPump(ms1, red, b1, rstate);
}
if(!m2){
autoPump(ms2, yel, b2, ystate);
}
if(!m3){
autoPump(ms3, green, b3, gstate);
}
}
void printMoisturePercentages(int ms1, int ms2, int ms3){
Serial.print("Moisture% of plant 1(red) is ");
Serial.println(ms1);
Serial.print("Moisture% of plant 2(yellow) is ");
Serial.println(ms2);
Serial.print("Moisture% of plant 3(green) is ");
Serial.println(ms3);
Serial.println();
}
void autoPump(int ms, int pump, int b, volatile boolean& state) {
if (ms < th1) {
digitalWrite(pump, HIGH);
state = HIGH;
} else if (ms > th2) {
digitalWrite(pump, LOW);
state = LOW;
}
}
void MonitorCommand(char l){
if(l=='r'){
rstate = !rstate;
digitalWrite(red, rstate);
Serial.println("red serial command");
m1 = HIGH;
return;
}
else if(l=='y'){
ystate = !ystate;
digitalWrite(yel, ystate);
Serial.println("yellow serial command");
m2 = HIGH;
return;
}
else if(l=='g'){
gstate = !gstate;
digitalWrite(green, gstate);
Serial.println("green serial command");
m3 = HIGH;
return;
}
else if(l=='a'){
Serial.println("reset to automatic mode");
m1 = LOW;
m2 = LOW;
m3 = LOW;
}
else if(l=='R'){
Serial.println("Red set to automatic");
m1 = LOW;
}
else if(l=='Y'){
Serial.println("Yellow set to automatic");
m2 = LOW;
}
else if(l=='G'){
Serial.println("Green set to automatic");
m3 = LOW;
}
}