I have a project where there are 6 drawers that each have a magnetic reed switch in them (normally closed). Each drawer is linked to an LED light that I want to turn on when it opens, while all the others then turn off, irespective of whether other drawers are already open.
Details
When all drawers are shut, all 6 lights are on, highlighting 6 messages above the drawers.
When a drawer opens, the LED that drawer is linked to stays on, but the others go off. It also sends a digital out to an external media player.
If that drawer gets pushed back in, all LEDs come back on again.
If another drawer is opened but the other drawer is still open, the new one becomes the drawer "of focus", so that light is on, and all other lights go off (including the drawer that was previously opened).
Problem
I have if statements set up on whether the contact is open or not, so if it is open, then the LED stays on but the others go off. However when I'm opening another drawer, the old drawer is still being triggered by the if statement, so it stays on too.
Is there another way I can set up my code so that whatever the latest drawer to be opened is, that LED stays on and all others go off?
I've put my code below, which is currently just working on 3 while I'm testing. I think it is likely the case I need to start from scratch, as it isn't going to work with if statements.
Please let me know if you can give me some pointers. Thanks in advance.
int relay1 = 5;
int relay2 = 6;
int relay3 = 7;
int LED1 = 9;
int LED2 = 14;
int LED3 = 15;
int relayState1;
int relayState2;
int relayState3;
boolean gpio1Sent;
boolean gpio2Sent;
boolean gpio3Sent;
int lastOpened = 0;
int relayCheck = 0;
void setup(){
Serial.begin(9600);
gpio1Sent = false;
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(relay1, INPUT_PULLUP);
pinMode(relay2, INPUT_PULLUP);
pinMode(relay3, INPUT_PULLUP);
//digitalWrite(LED1, HIGH);
//digitalWrite(LED2, HIGH);
//digitalWrite(LED3, HIGH);
}
void loop(){
relayState1 = digitalRead(relay1);
relayState2 = digitalRead(relay2);
relayState3 = digitalRead(relay3);
relayCheck = relayState1 + relayState2 + relayState3;
if (relayState1 == 1 && gpio1Sent == false){
digitalWrite(LED1, HIGH);
Serial.println("DRAWER 1 GPIO");
gpio1Sent = true;
}
else if (relayState1 == 1 && gpio1Sent == true){
digitalWrite(LED1, HIGH);
}
else if (relayState1 == 0){
digitalWrite(LED1, LOW);
gpio1Sent = false;
}
if (relayState2 == 1 && gpio2Sent == false){
digitalWrite(LED2, HIGH);
Serial.println("DRAWER 2 GPIO");
gpio2Sent = true;
}
else if (relayState2 == 1 && gpio2Sent == true){
digitalWrite(LED2, HIGH);
}
else if (relayState2 == 0){
digitalWrite(LED2, LOW);
gpio2Sent = false;
}
if (relayState3 == 1 && gpio3Sent == false){
digitalWrite(LED3, HIGH);
Serial.println("DRAWER 3 GPIO");
gpio3Sent = true;
}
else if (relayState3 == 1 && gpio3Sent == true){
digitalWrite(LED3, HIGH);
}
else if (relayState3 == 0){
digitalWrite(LED3, LOW);
gpio3Sent = false;
}
if (relayCheck == 0){
digitalWrite(LED1, HIGH);
digitalWrite(LED2, HIGH);
digitalWrite(LED3, HIGH);
}
}