Residual voltage in LED with PULLUP

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.

Show us a good schematic of your circuit.
Show us a good image of your wiring.
Give links to components.
Posting images:
https://forum.arduino.cc/index.php?topic=519037.0

To be tested.

const byte button2  = 2;
const byte button3  = 3;

const byte ledPin   = 13;

byte button_2_state = 0;
byte button_3_state = 0;

byte alarm_door_2   = 0;
byte alarm_door_3   = 0;

byte alarm_sum      = 0;

unsigned long button_2_timer  = 0;
unsigned long button_3_timer  = 0;

unsigned long current_time    = 0;
const unsigned long timeDelay = 10000ul;


//******************************************************************
void setup()
{
  pinMode(button2, INPUT_PULLUP);
  pinMode(button3, INPUT_PULLUP);

  pinMode(ledPin, OUTPUT);

} //END of   setup()


//******************************************************************
void loop()
{
  //current time
  current_time = millis();

  button_2_state = digitalRead(button2);
  button_3_state = digitalRead(button3);

  //***************************************
  if (button_2_state == HIGH)
  {
    button_2_timer = current_time;
    alarm_door_2 = 0;
  }

  //***************************************
  if (button_3_state == HIGH)
  {
    button_3_timer = current_time;
    alarm_door_3 = 0;
  }

  //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 >= timeDelay)
  {
    alarm_door_2 = 1;
  }
  
  //***************************************
  if (current_time - button_3_timer >= timeDelay)
  {
    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);
  }

} //END of    loop()

uploading image

I'm uploading actual pictures of the circuit in a minute.

Here the circuit above with both the switchs open (no switch, just wires) The LED shoul be off.

See sketch offered in post #3.

+1 for using code tags and displaying images !

larryd:
See sketch offered in post #3.

+1 for using code tags and displaying images !

larryd:
To be tested.

const byte button2  = 2;

const byte button3  = 3;

const byte ledPin  = 13;

byte button_2_state = 0;
byte button_3_state = 0;

byte alarm_door_2  = 0;
byte alarm_door_3  = 0;

byte alarm_sum      = 0;

unsigned long button_2_timer  = 0;
unsigned long button_3_timer  = 0;

unsigned long current_time    = 0;
const unsigned long timeDelay = 10000ul;

//******************************************************************
void setup()
{
  pinMode(button2, INPUT_PULLUP);
  pinMode(button3, INPUT_PULLUP);

pinMode(ledPin, OUTPUT);

} //END of  setup()

//******************************************************************
void loop()
{
  //current time
  current_time = millis();

button_2_state = digitalRead(button2);
  button_3_state = digitalRead(button3);

//***************************************
  if (button_2_state == HIGH)
  {
    button_2_timer = current_time;
    alarm_door_2 = 0;
  }

//***************************************
  if (button_3_state == HIGH)
  {
    button_3_timer = current_time;
    alarm_door_3 = 0;
  }

//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 >= timeDelay)
  {
    alarm_door_2 = 1;
  }
 
  //***************************************
  if (current_time - button_3_timer >= timeDelay)
  {
    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);
  }

} //END of    loop()

That totally fixed the problem!!!

I thought that it might be something about the circuit itself, but this code is perfect!!

I don't know how to thank you, but if I can do something for you, here I am.

Thank you very much!!

Do you understand why the changes were made?

"I don't know how to thank you, but if I can do something for you, here I am."
Give back to others in the community when you are able to do so.

I understand the changes, but I can't see why it solved it.

Compare you original to the new code.

Alarm is set after or equal to the delay time.
if (current_time - button_2_timer >= timeDelay)

Clear the alarm the moment the switch is open.
if (button_2_state == HIGH)
{
button_2_timer = current_time; // <-----<<<< if switch is open, reset the timer
alarm_door_2 = 0;
}