Quick question, hold button 3 seconds to turn on LED then turn off

I want to hold a button for 3 seconds and after that it turns on an led for 5 seconds then off... Its not working though, right now i load the code into my uno, the led flashes 3 times fast turns off for a few seconds then turns on and stays on... here is my code, what is wrong?

const int button1Pin = 2; // pushbutton 1 pin
const int ledPin = 13; // LED pin

void setup()
{
// Set up the pushbutton pins to be an input:
pinMode(button1Pin, INPUT);
// Set up the LED pin to be an output:
pinMode(ledPin, OUTPUT);
}

void loop()
{
int button1State; // variables to hold the pushbutton states
int count; //variables to count the time button is held

button1State = digitalRead(button1Pin);

if (button1State == HIGH)
{
count = count + 1;
}
if (count > 3000);
{
digitalWrite(ledPin, HIGH); // turn the LED on
delay (5000);
digitalWrite(ledPin, LOW); // turn the LED off
}
}

For example, you must reset the counter. I think is not a good idea do this:

  if (button1State == HIGH)
  {
    count = count + 1;
  }

This is not to good too:

    delay (5000);

I think the best way to do what you want to do is use the millis() function. Take a look to the example Blink Without Delay.

1 Like

This may do what you want:

const int ledPin =  13; 
const int buttonPin = 2;

int programState = 0;

int buttonState;
long buttonMillis = 0;
const long intervalButton = 3000;   

long ledMillis = 0;
const long intervalLed = 5000;   

void setup() {
  pinMode(ledPin, OUTPUT);      
  pinMode(buttonPin, INPUT);      
  digitalWrite(buttonPin, HIGH);
}

void loop()
{
  unsigned long currentMillis = millis();
  buttonState = digitalRead(buttonPin);
  
  if (buttonState == LOW && programState == 0) {
    buttonMillis = currentMillis;
    programState = 1;
  }
  else if (programState == 1 && buttonState == HIGH) {
        programState = 0; //reset
  }
  if(currentMillis - buttonMillis > intervalButton && programState == 1) {
    programState = 2;
    ledMillis = currentMillis;

    digitalWrite(ledPin, HIGH);
  }

  if(currentMillis - ledMillis > intervalLed && programState == 2) {
    programState = 0;

    digitalWrite(ledPin, LOW);
  }
}

That worked! can you put comments on there, im having a hard time following the flow of the loop.... But thank you very much, still learning.

This is simply the Blink Without Delay example but with the State feature.
State 0 = waiting for button to be pressed
State 1= button pressed and waiting for time to be elapsed
State 2= led on and waiting for time to be elapsed

If the button is released in the state 1 and the time is not elapsed it come back to state 0.

ok that makes much more sense, I actually didnt see your first posts, just the one at the bottom with the code, I like the idea of not delaying other code with the delay function, see what you are saying there.

So i put on a Relay shield and modified some of the pins and constant names. Im going to use the button to hook up to one that is unused on my Range Rover steering wheel, then when you hold it down for 1.5 seconds it triggers the relay which opens the exhaust dump valves, giving a more aggressive exhaust tone for that V8.

Here is the next bit i have to add: Hold down the button for another 1.5 seconds to close the dumps... or maybe click the button twice to open and twice to close?

Hold down the button for another 1.5 seconds to close the dumps...

That sounds like another state or two.

i want to hold the button down for 3 seconds to turn the led on and then hold button down for 3 seconds again to turn the led off ,please help me with the code.
i know that debounce code in examples of arduino can do what i want but i wnat a different code

@system Try this..

#include <mechButton.h>
#include <timeObj.h>


#define DUMP_PIN   13
#define BTN_PIN   2


timeObj     buttonTimer(1500);   // Button dump timer for 1.5 sec.
mechButton  myButton(BTN_PIN);   // The button, debounced and everything.
bool        dumpOpen;

void setup(void) {

   dumpOpen = false;
   pinMode(DUMP_PIN,OUTPUT);
   digitalWrite(DUMP_PIN,LOW);
   myButton.setCallback(buttonCliked);
}


void buttonCliked(void) {
   if (!myButton.trueFalse()) {
      buttonTimer.start();
   }
}


void doDump(void) {

   if (dumpOpen) {
      // Close dump
      digitalWrite(DUMP_PIN,LOW);
   } else {
      // Open dump
      digitalWrite(DUMP_PIN,HIGH);
   }
   dumpOpen = !dumpOpen;
}


void loop(void) {

   idle();                          // Run background things, like the button stuff.
   if (!myButton.trueFalse()) {     // If the button is being held down..
      if (buttonTimer.ding()) {     // If the button timer has expired (**ding!**)..
         doDump();                  // Switch the dump.
         buttonTimer.reset();       // And we are done with this button press for now.
      }
   }
}

You'll need to install LC_baseTools from the library manager to compile this but I think it'll do what you want.

Good luck!

-jim lee

yeah it worked :slight_smile:
Thank you so much, my gratitude is endless.
I'm so interested to your library too, wish you luck.

You can look at GitHub - JChristensen/JC_Button: Arduino library to debounce button switches, detect presses, releases, and long presses, it's a great button library with Long Press support, really easy to use.

yeah i added this library to my Arduino's examples too
its very usueful.

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