Setting variable and overriding for a timer in arduino

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!!!!!

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;
  }
}

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

how do you think timer will change its value ?

study

you might also benefit from studying state machines. Here is a small introduction to the topic: Yet another Finite State Machine introduction

Try this code:

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);
  }
}

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

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;
  }
}

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

also you don't handle bouncing...

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?


have you read

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

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)


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)

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.

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