Hello, this is my first time asking here.
I have a problem in a project I'm doing.
The idea is to control if the doors of some chambers are open more than a period of time (in the example 10 seconds). There are 9 doors and in each one there will be a switch. If a door is open more than 10s an alarm (here a LED) will ring. Each door is independent but the alarm is the same for all of them.
So the code basically sets a variable to 1 for each door opened for more than 10s and it will ring unless all these variables are returned to 0 (closing these doors).
Here is an example of the code, just with two doors.
int button2 = 2;
int button3 = 3;
int ledPin = 13;
int button_2_state=0;
int button_3_state=0;
int alarm_door_2 = 0;
int alarm_door_3 = 0;
int alarm_sum = 0;
unsigned long button_2_timer = 0;
unsigned long button_3_timer = 0;
unsigned long current_time = 0;
void setup() {
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(13, OUTPUT);
}
void loop() {
// Current time
current_time = millis();
button_2_state = digitalRead(button2);
button_3_state = digitalRead(button3);
// If the input is HIGH then don't keep the previous value of timer
if (button_2_state == HIGH) {
button_2_timer = millis();
}
if (button_3_state == HIGH) {
button_3_timer = millis();
}
// For each door, if it has been more than 10 seconds with the input in LOW, then set alarm to 1
if ( current_time - button_2_timer < 10000) {
alarm_door_2 = 0;
}
else {
alarm_door_2 = 1;
}
if ( current_time - button_3_timer < 10000) {
alarm_door_3 = 0;
}
else {
alarm_door_3 = 1;
}
// Sums all the alarm values, if anyone is 1 then ring the alarm (LED)
alarm_sum = alarm_door_2 + alarm_door_3;
if (alarm_sum >= 1) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
}
For the doors I will be using a normally closed switch, so when a door is open it closes the circuit and sets the digital input to LOW.
I have read that when the circuit is open, in order to not leave the input unbalanced, the input should be set as INPUT_PULLUP which sets it to 5V internally.
Here comes my problem:
When the switches are pushed (Opening the circuit, the normal position with the doors closed) the LED is still fed with some residual voltage and thus it lights up. This may cause a problem ringing the alarm even with the doors closed.
I don't know why does it happen or how to solve so I would be eternally grateful if you could help me.
If i haven't expressed myself clearly I'm sorry, just ask and I will clarify.
Thanks in advance.