Blinking LED

Hi
In this code the LED2 has two blink speeds, when the button is pressed for longer than 1 sec. should blink faster but is not, it is ON all the time. The LED1 is working properly, it is turn ON when the button is hold for longer than 1 sec.
Where is the mistake ?

int LED1 = PB15;
int button = PB12;
//int button = PB12;
int LED2 = PC13;

boolean LED1State = false;
boolean LED2State = false;

long buttonTimer = 0;
//long longPressTime = 250;
long longPressTime = 1000;

boolean buttonActive = false;
boolean longPressActive = false;

void setup()
{
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  //pinMode(button, INPUT);
  pinMode(button, INPUT_PULLDOWN);
  Serial.begin(115200);
}

void loop()
{
 
  if (digitalRead(button) == HIGH)
  {
    if (buttonActive == false)
    {
      buttonActive = true;
      buttonTimer = millis();
    }

    if ((millis() - buttonTimer > longPressTime) && (longPressActive == false))
    {
      longPressActive = true;
      //LED1State = !LED1State;
      // digitalWrite(LED1, LED1State);
      digitalWrite(LED1, 1);
      digitalWrite(LED2, HIGH);   
      delay(500);               
      digitalWrite(LED2, LOW);   
      delay(500);

    }
   
  }

  else

  {
    digitalWrite(LED1, 0);
    digitalWrite(LED2, HIGH);  
    delay(1000);               
    digitalWrite(LED2, LOW);    
    delay(1000);

    if (buttonActive == true)
    {
      if (longPressActive == true)
      {
        longPressActive = false;
      }
      else
        buttonActive = false;
    }
  }
 }

What kind of board are you using ? Particle electron ? Otherwise INPUT_PULLDOWN is not a correct parameter for pinMode in the Arduino world.

You should not use delays with buttons, because this may prevent the correct timing and button press events. Try to understand the example ‘blink without delay’ or read the tutorial pinned on top of this forum section

Could you make a sketch with three seperate parts: reading the button, determine short press and long press and blinking the led.

When you use Blink Without Delay to blink the led, then you can change the interval.

      buttonTimer = millis();

millis() returns a time, not a timer.

     delay(500);

It is a waste of time trying to write blink without delay code for one LED while using delay() for the other.

Ok
I changed blink to serial print and is nothing on serial monitor.
LED 1 is working properly.

{

  if (digitalRead(button) == HIGH)
  {
    if (buttonActive == false)
    {
      buttonActive = true;
      buttonTimer = millis();
    }

    if ((millis() - buttonTimer > longPressTime) && (longPressActive == false))
    {
      longPressActive = true;

      digitalWrite(LED1, HIGH);
      Serial.println(500);
    }

  }

  else

  {
    digitalWrite(LED1, 0);
    Serial.println(1000);

    if (buttonActive == true)
    {
      if (longPressActive == true)
      {
        longPressActive = false;
      }
      else
        buttonActive = false;
    }
  }
}

I changed

 //Serial.begin(115200);
   Serial.begin(9600);

serial is printing 1000 properly, after pressing the button 500 is displayed only once =serial is not refreshing.

blink serial.jpg

I changed

//Serial.begin(115200);
   Serial.begin(9600);

serial is printing 1000 properly, after pressing the button 500 is displayed only once = serial is not refreshing.

blink serial.jpg

Why the loop stops ?

Why the loop stops ?

Would you like to see the answer spread over several responses, in snippets?

If not, post ALL of your code, without the extraneous curly braces and with proper indenting, as Tools + Auto Format can do in a flash.

here is the code

int LED1 = PB15;
int button = PB12;
//int button = PB12;
int LED2 = PC13;

boolean LED1State = false;
boolean LED2State = false;

long buttonTimer = 0;
//long longPressTime = 250;
long longPressTime = 1000;

boolean buttonActive = false;
boolean longPressActive = false;

void setup()
{
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  //pinMode(button, INPUT);
  pinMode(button, INPUT_PULLDOWN);
  //Serial.begin(115200);
  Serial.begin(9600);
}

void loop()
{

  if (digitalRead(button) == HIGH)
  {
    if (buttonActive == false)
    {
      buttonActive = true;
      buttonTimer = millis();
    }

    if ((millis() - buttonTimer > longPressTime) && (longPressActive == false))
    {
      longPressActive = true;

      digitalWrite(LED1, HIGH);
      Serial.println(500);
    }

  }

  else

  {
    digitalWrite(LED1, 0);
    Serial.println(1000);

    if (buttonActive == true)
    {
      if (longPressActive == true)
      {
        longPressActive = false;
      }
      else
        buttonActive = false;
    }
  }
}

I moved serial print to the end , but is displaying only 1000, regardless LED1 is ON or OFF

int LED1 = PB15;
int button = PB12;
//int button = PB12;
int LED2 = PC13;

boolean LED1State = false;
boolean LED2State = false;

long buttonTimer = 0;
//long longPressTime = 250;
long longPressTime = 1000;

boolean buttonActive = false;
boolean longPressActive = false;

void setup()
{
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  //pinMode(button, INPUT);
  pinMode(button, INPUT_PULLDOWN);
  //Serial.begin(115200);
  Serial.begin(9600);
}

void loop()
{

  if (digitalRead(button) == HIGH)
  {
    if (buttonActive == false)
    {
      buttonActive = true;
      buttonTimer = millis();
    }

    if ((millis() - buttonTimer > longPressTime) && (longPressActive == false))
    {
      longPressActive = true;
     
      digitalWrite(LED1, 1);
   

    }

  }

  else

  {
    digitalWrite(LED1, 0);
  

    if (buttonActive == true)
    {
      if (longPressActive == true)
      {
        longPressActive = false;
      }
      else
        buttonActive = false;
    }
  }
  ////////////////////////
 if (LED1 == 1)
  
  {
    Serial.println(500);
  }
  else
  {
    Serial.println(1000);
  }
  /////////////////////
}

What device are you running that code on?

I think it would be a lot simpler to handle the switch in two stages. Look at the state change detection example to see how to determine that a switch HAS BECOME pressed. When that happens, record the time.

Then, separately, see if the switch IS pressed. If it is, calculate the interval (millis() - whenTheSwitchBecamePressed). If that exceeds some value, it is a long press. You only need three variables - the current state of the switch, the previous state of the switch, and the time when the switch became pressed.

That means to start over, I just curious why the last code is not working, the stage of the LED1 should terminate the stage of the button ?

I just curious why the last code is not working, the stage of the LED1 should terminate the stage of the button ?

Think about what you are using in the if statement at the end of loop(). Does that make sense?

You explicitly set LED1 to PB15 at the start of the program. You never change the value of LED1, but you want to know if it equals 1, so you can print 1000. Here's a hint. Never in a million years.