I am working on a project with pushbuttons that need to switch a relay and turn it off after a few seconds. After that, it should not be possible to press the button for x number of seconds.
However I want to connect 4 buttons to one Arduino, it seems to me that this should just be able to run synchronously with separate timers running alongside each other. Unfortunately I am not getting this to work as intended.
Even though the timer variables all have different names (see code example). As soon as you press one button the program is activated and the button is triggered.
You should still be able to press the other 3 buttons but when I do that nothing happens until the number of seconds has passed.
Also, sometimes when I press a certain button the program crashes completely. The relay then turns on but does not turn off after that. Pressing the buttons does not change the situation.
Unfortunately I can't manage to find the problem, I have the idea that all the code is just fine. Hopefully someone here has an idea what the problem is.
My code:
int SensorPin1 = 3;
int SensorPin2 = 4;
int SensorPin3 = 5;
int SensorPin4 = 6;
int ValvePin1 = 7;
int ValvePin2 = 8;
int ValvePin3 = 9;
int ValvePin4 = 10;
int ValvePin5 = 11;
int ValvePin6 = 12;
unsigned long lastActive2;
unsigned long lastActive3;
unsigned long lastActive4;
unsigned long lastActive5;
unsigned long lastActive6;
int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;
int delayTime = 20000;
int timeoutTime = 7000;
unsigned long currentTime1;
unsigned long currentTime2;
unsigned long currentTime3;
unsigned long currentTime4;
void setup() {
pinMode(SensorPin1, INPUT_PULLUP);
pinMode(SensorPin2, INPUT_PULLUP);
pinMode(SensorPin3, INPUT_PULLUP);
pinMode(SensorPin4, INPUT_PULLUP);
pinMode(ValvePin1, OUTPUT);
pinMode(ValvePin2, OUTPUT);
pinMode(ValvePin3, OUTPUT);
pinMode(ValvePin4, OUTPUT);
pinMode(ValvePin5, OUTPUT);
pinMode(ValvePin6, OUTPUT);
digitalWrite (ValvePin1, HIGH);
digitalWrite (ValvePin2, HIGH);
digitalWrite (ValvePin3, HIGH);
digitalWrite (ValvePin4, HIGH);
digitalWrite (ValvePin5, HIGH);
digitalWrite (ValvePin6, HIGH);
}
void loop() {
currentTime1 = millis();
// Sensor 2
buttonState1 = digitalRead(SensorPin1);
if (buttonState1 == LOW) {
if ((currentTime1 - lastActive2) > delayTime) {
digitalWrite (ValvePin2, LOW);
lastActive2 = millis();
}
}
if ((currentTime1 - lastActive2) > timeoutTime) {
digitalWrite (ValvePin2, HIGH);
}
currentTime2 = millis();
// Sensor 3
buttonState2 = digitalRead(SensorPin2);
if (buttonState2 == LOW) {
if ((currentTime2 - lastActive3) > delayTime) {
digitalWrite (ValvePin3, LOW);
lastActive3 = millis();
}
}
if ((currentTime2 - lastActive3) > timeoutTime) {
digitalWrite (ValvePin3, HIGH);
}
currentTime3 = millis();
// Sensor 4
buttonState3 = digitalRead(SensorPin3);
if (buttonState3 == LOW) {
if ((currentTime3 - lastActive4) > delayTime) {
digitalWrite (ValvePin4, LOW);
lastActive4 = millis();
}
}
if ((currentTime3 - lastActive4) > timeoutTime) {
digitalWrite (ValvePin4, HIGH);
}
currentTime4 = millis();
// Sensor 5
buttonState4 = digitalRead(SensorPin4);
if (buttonState4 == LOW) {
if ((currentTime4 - lastActive5) > delayTime) {
digitalWrite (ValvePin5, LOW);
digitalWrite (ValvePin6, LOW);
lastActive5 = millis();
}
}
if ((currentTime4 - lastActive5) > timeoutTime) {
digitalWrite (ValvePin5, HIGH);
digitalWrite (ValvePin6, HIGH);
}
}