Button held for 1 second and when released causes led to flash

Hello, I am working on a project in which I press a button that lights an led, and when held for 1 second lights another led. But when I release the button after 1 second has elapsed, the leds begin to flash on and off. Pressing the led again causes the leds to stop flashing. But if the button is released before 1 second has elapsed, the single led turns off.

Can someone point me in the right direction for what to do? I have already have a program when a button is held for 1 second activates another led. But when the button is released, the leds turn off.

const byte buttonPin = 11;
const byte ledPin    = 15;  // 15;
const byte ledPin2   = 16;  // 16;

unsigned long buttonPressedMillis = 0;
unsigned int duration             = 1000;

byte lastButtonState = LOW;  // set default to inactive, not pressed.
boolean enableTimer = false;
unsigned long previousMillis = 0;
int interval = 100;
int ledState = digitalRead(ledPin);
int ledState2 = digitalRead(ledPin2);


void setup()
{
  Serial.begin( 9600);
  Serial.println();
  Serial.println( "Sketch has started.");

  pinMode(ledPin, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(buttonPin, INPUT);  // Switch pressed reads as a HIGH
  
}


void loop()
{
  unsigned long currentMillis = millis();
  int buttonState = digitalRead( buttonPin);

  if ( buttonState != lastButtonState)
  {
    // Remember the button state for the next time.
    lastButtonState = buttonState;

    //Was the switch pressed?
    if ( buttonState == HIGH)
    {
      //enable timing
      enableTimer = true;
      //initialize the timer
      buttonPressedMillis = millis();

      Serial.println();
      Serial.println( "Event: button was just pressed");
      digitalWrite(ledPin, HIGH);  //A HIGH turns on the LED
    }

    if (buttonState == LOW) {

      
        
      digitalWrite(ledPin, LOW);
      digitalWrite(ledPin2, LOW);
      Serial.println("button released");
      enableTimer = false;
        

        
          
      
    }
      
      
      
      
    }
    
    
  

  
    if (enableTimer == true) {
      if (currentMillis - buttonPressedMillis >= duration) {
        enableTimer = false;

    digitalWrite(ledPin2, HIGH);  //A HIGH turns on the LED
    Serial.println("Both leds are on!");

    

    

    
      
      }
    }

    


    
    
  }

  

  

// END of   loop()

Pressing the led again causes the leds to stop flashing.

Why would you press on the LED?

int ledState = digitalRead(ledPin);
int ledState2 = digitalRead(ledPin2);

Why, at compile time, do you care what the state of the pins are, on some other computer?

  pinMode(buttonPin, INPUT);  // Switch pressed reads as a HIGH

How IS the switch wired, and why do you refer to the pin that the switch is connected to as buttonPin? Did you sew a button on?

Why are there

so blinking many

blank lines in your

code?

Your problem sounds like the pin that the switch is connected to is floating when the switch is not pressed.

Hey @PaulS, I apologize for the sloppiness. Can you direct me on how I can incorporate debouncing into my current code? I have declared the variables already.

const byte button = 11;
const byte ledPin    = 15;  // 15;
const byte ledPin2   = 16;  // 16;

unsigned long buttonPressedMillis = 0;
unsigned int duration             = 1000;

byte lastButtonState = LOW;  // set default to inactive, not pressed.
boolean enableTimer = false;
unsigned long previousMillis = 0;
int interval = 100;

long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 50;

void setup()
{
  Serial.begin( 9600);
  Serial.println();
  Serial.println( "Sketch has started.");

  pinMode(ledPin, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(button, INPUT);  // Switch pressed reads as a HIGH

}


void loop()
{
  unsigned long currentMillis = millis();
  int buttonState = digitalRead(button);

  if (buttonState != lastButtonState)
  {
    // Remember the button state for the next time.
    lastButtonState = buttonState;
    

    //Was the switch pressed?
    if (buttonState == HIGH)
    {
      //enable timing
      enableTimer = true;
      //initialize the timer
      buttonPressedMillis = millis();

      Serial.println();
      Serial.println( "Event: button was just pressed");
      digitalWrite(ledPin, HIGH);  //A HIGH turns on the LED
    }

    if (buttonState == LOW) {
      digitalWrite(ledPin, LOW);
      digitalWrite(ledPin2, LOW);
      Serial.println("button released");
      enableTimer = false;
    }
  }
  if (enableTimer == true) {
    if (currentMillis - buttonPressedMillis >= duration) {
      enableTimer = false;

      digitalWrite(ledPin2, HIGH);  //A HIGH turns on the LED
      Serial.println("Both leds are on!");
    }
  }
}
// END of   loop()

Can you direct me on how I can incorporate debouncing into my current code?

delay(oneWeek); // no more bouncing problems!

Now, how about you answer the questions I asked?

How EXACTLY the switch/button is wired would be a good start. And if you're only interested in press,hold for at least a second then release why are you worried about debouncing?

Steve

Hello @slipstick, I am only interested in press, hold, and release functionality. My confusion comes from how I can incorporate the flashing leds once the button is released after held for 1 second, and to turn off the leds once the button is pressed again. If released when button is held for less than a second, the single led activated from the initial press turns off. Can you offer some insight? Thank you.

Certainly...AFTER you have finally told us how your switch is wired.

But basically to know what to do next you're going to have to set flags so that code knows what you're doing now.

Steve

The circuit is looks like this |500x289

Although the button is connected to pin 11, and leds connected to pin 16 and 15 (I am using Adafruit NRF52 Feather)

The circuit is looks like this

So, when the switch is pressed, there is a circuit from the digital pin to ground, and when the switch is not pressed, there is no circuit, and the pin is in some undefined state.

That is NOT a good thing. If you are not going to use the internal pullup resistor (why wouldn't you?), you MUST have an external pullup or pulldown resistor, or you are NOT going to know, with any certainty, what state the switch is in.

|500x312

I have followed your instruction and updated my circuit design. But can you tell me on how I can approach my code? @slipstick mentioned using flags. Can you be more specific? Thank you.

Your original schematic was better. (I.e. the button connected between the input pin and ground.) What you have to do is change pinMode(button, INPUT); to pinMode(button, INPUT_PULLUP);

Pieter