led turn on based on the button pressed time

Hi

My concept is if button is pressed less than 10 seconds then led should turn on.

if button is pressed more than 10 seconds then device should reset.

problem is device does not reset if i press the button more than 10 seconds.

to reset the device i have connected digital pin to reset pin of arduino.

can someone please check the code and help me

const int ledPin =  5;
const int ledPin1 =  6;
const int buttonPin = 3;
int Reset = 10;

int programState = 0;

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

long ledMillis = 0;


void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);     
  pinMode(buttonPin, INPUT);     
  digitalWrite(buttonPin, LOW);
  
  digitalWrite(ledPin, LOW);
  Serial.println("siren connection on ");
   digitalWrite(ledPin1, LOW);
   Serial.print("buzzer connection off ");
  digitalWrite(Reset, HIGH);
  delay(200); 
  pinMode(Reset, OUTPUT);     
}

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

    digitalWrite(ledPin, HIGH);
    Serial.print("SIREN connection off ");
    Serial.print ("        /        ");
     if (digitalRead(ledPin) == HIGH) {
          digitalWrite(ledPin1, HIGH);
          Serial.print("buzzer connection on ");
        }
    
  }
  }
  {if (buttonState == HIGH && programState == 0) {
    buttonMillis = currentMillis;
    programState = 1;
  }
  else if (programState == 1 && buttonState == LOW) {
        programState = 0; //reset
  }
  if(currentMillis - buttonMillis > intervalButton && programState == 1) {
    programState = 2;
    ledMillis = currentMillis;

               
  
 
  digitalWrite(Reset, LOW);
   Serial.println("reset done");
        }
    
  }}

First off, all of the variables you use for time calculations should be unsigned. I know that BWD mixes long with unsigned long but that is wrong even though it will take just under 25 days to bug. With micros it shows in less than 40 minutes.

You should make the button read run on its own, not inside of conditions. It should set a variable that your attempt at state code should read. It should also debounce the button.

When the button has its own code, the rest will get easier.

Everyone's probably wondering why on Earth you're keen on resetting your Arduino....

Yep, and the used "self reset" method isn't guaranteed to work either.

ok, forget about the reset if i press the button for more than 10 seconds then both led should go low.

how this can be done?

Start by changing to code to the suggestions made by GoForSmoke :slight_smile:

The following is a simple way to check if a button is pressed continuously for a certain length of time

void loop() {
   buttonState = digitalRead(buttonPin);
   if (buttonState == HIGH) {   // assumes LOW when pressed and HIGH when not-pressed
       lastTimeButtonWasHigh = millis();
   }

   if (millis() - lastTimeButtonWasHigh >= requiredTime) {
       // input has been LOW throughout the required time
       // do whatever needs to be done
   }
}

If you need to ensure that the button has NOT been pressed during that time you will obviously need to invert the logic.

...R

The code below uses Robin2's method, with a flag to ensure if the led goes off at 10 seconds but the button is held down, it stays off until a release and new press.

May benefit from a bit of neatening up but it does what I understand the OP asked for:

//  https://forum.arduino.cc/index.php?topic=629524.0
//  2 aug 2019
// reset if button pressed longer than 10 seconds

byte buttonPin = 3;
unsigned long resetTheClock;
int interval = 3000; //shorter for testing
bool buttonState;
bool flagWithNoFancyName = false;

void setup()
{
  Serial.begin(9600);
  Serial.println(".... reset if button pressed longer than 10 seconds ....");
  Serial.print("Compiler: ");
  Serial.print(__VERSION__);
  Serial.print(", Arduino IDE: ");
  Serial.println(ARDUINO);
  Serial.print("Created: ");
  Serial.print(__TIME__);
  Serial.print(", ");
  Serial.println(__DATE__);
  Serial.println(__FILE__);

  //turn off L13
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);

  pinMode(buttonPin, INPUT_PULLUP);


} //setup

void loop()
{
  buttonState = digitalRead(buttonPin);

  if (buttonState == HIGH)  // input pullup means a press is low
  {
    resetTheClock = millis();
    flagWithNoFancyName = true;
  }

  if (buttonState == LOW && flagWithNoFancyName)
  {
    digitalWrite(LED_BUILTIN, HIGH);
  }
  else
  {
    digitalWrite(LED_BUILTIN, LOW);
  }

  if (millis() - resetTheClock >= interval)
  {
    // do the reset voodoo
    Serial.println("Resetting....");
    // or just turn the led off...
    digitalWrite(LED_BUILTIN, LOW);
    flagWithNoFancyName = false;
    resetTheClock = millis();
  }
} //loop

meltDown:
The code below uses Robin2’s method, with a flag to ensure if the led goes off at 10 seconds but the button is held down, it stays off until a release and new press.

May benefit from a bit of neatening up but it does what I understand the OP asked for:

//  https://forum.arduino.cc/index.php?topic=629524.0

//  2 aug 2019
// reset if button pressed longer than 10 seconds

byte buttonPin = 3;
unsigned long resetTheClock;
int interval = 3000; //shorter for testing
bool buttonState;
bool flagWithNoFancyName = false;

void setup()
{
  Serial.begin(9600);
  Serial.println("… reset if button pressed longer than 10 seconds …");
  Serial.print("Compiler: “);
  Serial.print(VERSION);
  Serial.print(”, Arduino IDE: ");
  Serial.println(ARDUINO);
  Serial.print("Created: “);
  Serial.print(TIME);
  Serial.print(”, ");
  Serial.println(DATE);
  Serial.println(FILE);

//turn off L13
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);

pinMode(buttonPin, INPUT_PULLUP);

} //setup

void loop()
{
  buttonState = digitalRead(buttonPin);

if (buttonState == HIGH)  // input pullup means a press is low
  {
    resetTheClock = millis();
    flagWithNoFancyName = true;
  }

if (buttonState == LOW && flagWithNoFancyName)
  {
    digitalWrite(LED_BUILTIN, HIGH);
  }
  else
  {
    digitalWrite(LED_BUILTIN, LOW);
  }

if (millis() - resetTheClock >= interval)
  {
    // do the reset voodoo
    Serial.println(“Resetting…”);
    // or just turn the led off…
    digitalWrite(LED_BUILTIN, LOW);
    flagWithNoFancyName = false;
    resetTheClock = millis();
  }
} //loop

It is sort of a co-operative debounce routine. A button watchdog.

I like to use pin state over time (debounce interval) to determine an abstract button state that other tasks use.

if ( millis() - start < interval )
{
// code to watch for intervening events
}
else
{
// code to do timeout actions
}