Pushbutton and led

Hello everyone, can anyone help me to fix this code. My operation for my project is when the pushbutton is hold for 3 sec it will turn on the led if not hold for 3 sec the led keep turn off. Then when the led was turn on it will keep to turn on if the pushbutton keep push and led will turn off if I release pushbutton for 3 sec.
this code I got from Use Arduino millis() with buttons to delay events - Bald Engineer

> #include <LiquidCrystal.h>
> 
> //Global Variables
> const byte BUTTON = 8; // our button pin
> const byte LED = 9; // LED (built-in on Uno)
> 
> unsigned long buttonPushedMillis; // when button was released
> //unsigned long ledTurnedOnAt; // when led was turned on
> unsigned long buttonReleased;
> unsigned long turnOnDelay = 3000; // wait to turn on LED
> unsigned long turnOffDelay = 3000; // turn off LED after this time
> bool ledReady = false; // flag for when button is let go
> bool ledState = false; // for LED is on or not.
> const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
> LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
> 
> void setup() {
>   pinMode(BUTTON, INPUT_PULLUP);
>   pinMode(LED, OUTPUT);
>   digitalWrite(LED, LOW);
>   lcd.setCursor(0, 1);
>   lcd.print("RUNNING!!    ");
>   Serial.begin (9600);
> 
> }
> 
> void loop() {
>   // get the time at the start of this loop()
>   unsigned long currentMillis = millis();
> 
>   // check the button
>   if (digitalRead(BUTTON) == LOW) {
>     // update the time when button was pushed
>     buttonPushedMillis = currentMillis;
>     ledReady = true;
>   }
> 
>   // make sure this code isn't checked until after button has been let go
>   if (ledReady) {
>     //this is typical millis code here:
>     if ((unsigned long)(currentMillis - buttonPushedMillis) >= turnOnDelay) {
>       // okay, enough time has passed since the button was let go.
>       digitalWrite(LED, HIGH);
>       lcd.setCursor(0, 1);
>       lcd.print("LF PRESENT!!");
>       // setup our next "state"
>       ledState = true;
>       // save when the LED turned on
>       
>       buttonReleased = currentMillis;
>       // wait for next button press
>       ledReady = false;
>       
>     }
>   }
> 
>   // see if we are watching for the time to turn off LED
>  if (ledState) {
>     // okay, led on, check for now long
>  
>     if ( (unsigned long)(currentMillis -  buttonReleased) >= turnOffDelay) { //if the button release it will hold led on in 3 sec then back to running
>      
>    
>       ledState = false;
>       digitalWrite(LED, LOW);
>       lcd.setCursor(0, 1);
>       lcd.print("RUNNING!!    ");
> 
>     }
> 
>   }
> }

Please read the forum guidelines to see how to properly post code and some information on making a good post.

Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

Please go back and fix your original post.

What does the code actually do? How is that different from what you want?

because when I running this code, during the led turn on it should not turn off when I'm holding the pushbutton. But during I'm holding the push button the led is turn off after 3 sec.

Hello irfansalleh
This looks like a school asignment.
Take some time and use the Search Forum function to get similar solutions to gain the knowledge.

  • need to recognize when the button changes state and is pressed (not being pressed)
  • capture a timestamp when pressed
  • when captured, do something when 3 secs pass
  • reset the timestamp when button is released, which preventing the timer from expiring

don't worry about the LCD until you get above to work

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