# Setting variable and overriding for a timer in arduino

**URL:** <https://forum.arduino.cc/t/setting-variable-and-overriding-for-a-timer-in-arduino/1224807>\
**Category:** Programming\
**Created:** [February 16, 2024, 6:55pm UTC](https://forum.arduino.cc/t/setting-variable-and-overriding-for-a-timer-in-arduino/1224807 "2024-02-16T18:55:11Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![bpall](https://avatars.discourse-cdn.com/v4/letter/b/a87d85/32.png) [@bpall](https://forum.arduino.cc/u/bpall)\
**Post date:** [February 16, 2024, 6:55pm UTC](https://forum.arduino.cc/t/setting-variable-and-overriding-for-a-timer-in-arduino/1224807/1 "2024-02-16T18:55:11Z")

</div>

I started a code to control an Arduino Uno with an LED light. all my electrical is fine, but I'm having trouble with the code as I want to have the LED light run when the button is pressed but turn off after 5 seconds even if it is still pressed down. I've been able to get it to turn off after releasing and waiting 5 seconds by using a delay, but that is not the outcome I want.  
I think the issue might be with the int and having it as a global versus local, but I do not know what else to do. I attached the code if yall could help!!!!!

```arduino
int led = 7;
int BUTTON_PIN = 8;
int buttonState = 0; 
int timer;
void setup() {
  pinMode(led, OUTPUT);
  pinMode (BUTTON_PIN, INPUT);
  
  Serial.begin(9600);
}

void loop() {
  buttonState = digitalRead(BUTTON_PIN);
  Serial.println(buttonState);

  if (buttonState == HIGH){
    while(timer<=5000){
    digitalWrite(led,HIGH);
    
    }
  }else{
    digitalWrite(led,LOW);
    int timer=0;
  }
}

```

---

<div class="post-metadata">

**Author:** ![bpall](https://avatars.discourse-cdn.com/v4/letter/b/a87d85/32.png) [@bpall](https://forum.arduino.cc/u/bpall)\
**Post date:** [February 16, 2024, 6:55pm UTC](https://forum.arduino.cc/t/setting-variable-and-overriding-for-a-timer-in-arduino/1224807/2 "2024-02-16T18:55:47Z")

</div>

the led turns on when thelimit switch ocnnects to the circuit and reads voltage

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [February 16, 2024, 6:58pm UTC](https://forum.arduino.cc/t/setting-variable-and-overriding-for-a-timer-in-arduino/1224807/3 "2024-02-16T18:58:56Z")

</div>

> [@bpall](#):
>
> ```arduino
> while(timer<=5000){
> digitalWrite(led,HIGH);
>     
> }
> 
> ```

how do you think timer will change its value ?

study

- [Using millis() for timing. A beginners guide](https://forum.arduino.cc/index.php?topic=503368.0)
- [Several things at the same time](https://forum.arduino.cc/index.php?topic=223286.0)
- [Flashing multiple LEDs at the same time](https://forum.arduino.cc/t/flashing-multiple-leds-at-the-same-time/1065564)

you might also benefit from studying state machines. Here is a small introduction to the topic: [Yet another Finite State Machine introduction](https://forum.arduino.cc/t/yet-another-finite-state-machine-introduction/1198997)

---

<div class="post-metadata">

**Author:** ![ruilviana](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ruilviana/32/722429_2.png) [@ruilviana](https://forum.arduino.cc/u/ruilviana)\
**Post date:** [February 16, 2024, 7:41pm UTC](https://forum.arduino.cc/t/setting-variable-and-overriding-for-a-timer-in-arduino/1224807/4 "2024-02-16T19:41:55Z")

</div>

Try this code:

```arduino
int led = 7;
int BUTTON_PIN = 8;
int buttonState = 0;
unsigned long timer = 0;
bool turn_On = false;
//------------------------------------------------------------
void setup() {
  pinMode(led, OUTPUT);
  pinMode (BUTTON_PIN, INPUT);
  Serial.begin(9600);
}
//------------------------------------------------------------
void loop() {
  if (turn_On == false) {
    if (digitalRead(BUTTON_PIN) == LOW) {
      timer = millis();
      digitalWrite(led, HIGH);
      turn_On = true;
    }
  }
  if (millis() - timer >= 5000) {
    digitalWrite(led, LOW);
    if (digitalRead(BUTTON_PIN) == HIGH)
      turn_On = false;
      delay(50);
  }
}

```

---

<div class="post-metadata">

**Author:** ![bpall](https://avatars.discourse-cdn.com/v4/letter/b/a87d85/32.png) [@bpall](https://forum.arduino.cc/u/bpall)\
**Post date:** [February 21, 2024, 2:39pm UTC](https://forum.arduino.cc/t/setting-variable-and-overriding-for-a-timer-in-arduino/1224807/5 "2024-02-21T14:39:24Z")

</div>

i tried that and it still wont turn the led off while the button is still pressed. I think that is the issue im running into.

heres the current code ive been trying. right now the led stays on when pressed and turn off when i go off the button

```arduino
int led = 7;
int BUTTON_PIN = 8;
int buttonState = 0; 
unsigned long startMillis; //some global variables available anywhere in the program
unsigned long currentMillis;
const unsigned long period = 5000;
void setup() {
  pinMode(led, OUTPUT);
  pinMode (BUTTON_PIN, INPUT);
  
  Serial.begin(9600);
  startMillis = millis();
}

void loop() {
  buttonState = digitalRead(BUTTON_PIN);
  Serial.println(buttonState);
currentMillis = millis();
  if (buttonState == HIGH){
    if(startMillis - currentMillis>=period){
    digitalWrite(led,HIGH);
    }else{
      digitalWrite(led,LOW);
    }
  }else{
    digitalWrite(led,LOW);
    startMillis = currentMillis;
  }
}

```

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [February 21, 2024, 2:43pm UTC](https://forum.arduino.cc/t/setting-variable-and-overriding-for-a-timer-in-arduino/1224807/6 "2024-02-21T14:43:13Z")

</div>

how did you wire the button? do you have an external pulldown ?

also you don't handle bouncing...

---

<div class="post-metadata">

**Author:** ![bpall](https://avatars.discourse-cdn.com/v4/letter/b/a87d85/32.png) [@bpall](https://forum.arduino.cc/u/bpall)\
**Post date:** [February 21, 2024, 2:47pm UTC](https://forum.arduino.cc/t/setting-variable-and-overriding-for-a-timer-in-arduino/1224807/7 "2024-02-21T14:47:29Z")

</div>

the limit switch is connected to port the 5v port and the ground port and connected to the led pathway so it can read the voltage and turn on and off based on that.  
what do you mean by external pulldown?

---

<div class="post-metadata">

**Author:** ![bpall](https://avatars.discourse-cdn.com/v4/letter/b/a87d85/32.png) [@bpall](https://forum.arduino.cc/u/bpall)\
**Post date:** [February 21, 2024, 2:50pm UTC](https://forum.arduino.cc/t/setting-variable-and-overriding-for-a-timer-in-arduino/1224807/8 "2024-02-21T14:50:50Z")

</div>

![IMG_0814](https://europe1.discourse-cdn.com/arduino/original/4X/c/f/7/cf73f2d3b31648dba0538e582547b408b11d2ba1.jpeg)  
 ![IMG_0813](https://europe1.discourse-cdn.com/arduino/original/4X/e/5/a/e5a6b373ee01c96553d61f0a8ca38e2d6ff6e816.jpeg)

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [February 21, 2024, 2:52pm UTC](https://forum.arduino.cc/t/setting-variable-and-overriding-for-a-timer-in-arduino/1224807/9 "2024-02-21T14:52:59Z")

</div>

> [@bpall](#):
>
> what do you mean by external pulldown?

have you read

[https://docs.arduino.cc/built-in-examples/digital/Button/](https://docs.arduino.cc/built-in-examples/digital/Button/)

* * *

it's hard to see what's connected where on the pictures, if you could draw your circuit (just rectangles with used pin names and lines for connectors) and post it here it would be easier

---

<div class="post-metadata">

**Author:** ![bpall](https://avatars.discourse-cdn.com/v4/letter/b/a87d85/32.png) [@bpall](https://forum.arduino.cc/u/bpall)\
**Post date:** [February 21, 2024, 3:01pm UTC](https://forum.arduino.cc/t/setting-variable-and-overriding-for-a-timer-in-arduino/1224807/10 "2024-02-21T15:01:38Z")

</div>

![image](https://europe1.discourse-cdn.com/arduino/original/4X/c/e/2/ce26646514d7adba94d17ff5a8607112a329acb4.jpeg)

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [February 21, 2024, 3:09pm UTC](https://forum.arduino.cc/t/setting-variable-and-overriding-for-a-timer-in-arduino/1224807/11 "2024-02-21T15:09:46Z")

</div>

sorry still hard to read

loose the breadboard just draw the wires and resistors where they are

your button and resistor needs to be wired like this (adjust the pin for the one you are using)

 ![](https://europe1.discourse-cdn.com/arduino/original/4X/e/c/9/ec99a1e2a7ac2d6be6cadc65a32d571beae531e7.png)  
The pin from which you read the signal is on the other side of the switch compared to the 5V connection, before the resistor.

(if you were to use INPUT\_PULLUP it would be easier and there would be no need for an extra resistor)

---

<div class="post-metadata">

**Author:** ![stitech](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/stitech/32/1016377_2.png) [@stitech](https://forum.arduino.cc/u/stitech)\
**Post date:** [February 21, 2024, 3:31pm UTC](https://forum.arduino.cc/t/setting-variable-and-overriding-for-a-timer-in-arduino/1224807/12 "2024-02-21T15:31:32Z")

</div>

If you use `buttonState` to start a timer, be it a `delay()`, a construction with `millis()` or a hardware timer, each with its own pros and cons, you should be aware that the next time `loop()` is executed, the `buttonState` will start the time all over again.

I guess you want to use `buttonEdge` to start your timer. To detect an edge, you need to remember the previous state and compare it to the present state.

Beyond that, you might think about what you want to happen if the button is released before the timer runs out, or if the button is pushed again before or after the timer has run out.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [August 19, 2024, 3:31pm UTC](https://forum.arduino.cc/t/setting-variable-and-overriding-for-a-timer-in-arduino/1224807/13 "2024-08-19T15:31:44Z")

</div>

Closed on August 19, 2024, 3:31pm UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
