Blink led stays on ( sometimes ) when push button is released

Hi All

The sketch will light up a Star Wars Tie Fighter …
All is working well … the only issue i have is the green LED that will represent the laser cannons is on a push button switch . the led blinks as desired when pressed in but sometimes , when released - it stays on ( ie - on permanently , not blinking ) i guess it happens when i release the button while the led is lit …

please advise how I can recitify this to ensure it goes off and stays off till pressed again …
many thanks

here is the code : Last section is where the issue is :
int CabinBlueLed = 13;
int MultiWhiteLed = 12;
int EngineRedLed = 11;
int LaserButton = 2;

#define CabinRedLed 8
#define CabinYellowLed 6
#define LaserGreenLed 10

void setup() {
pinMode(CabinBlueLed, OUTPUT);
pinMode(MultiWhiteLed,OUTPUT);
pinMode(EngineRedLed, OUTPUT);
pinMode(LaserGreenLed, OUTPUT);
pinMode(CabinRedLed, OUTPUT);
pinMode(CabinYellowLed, OUTPUT);
pinMode(LaserButton, INPUT);

}

void loop() {
digitalWrite(CabinBlueLed, HIGH);
digitalWrite(MultiWhiteLed, HIGH);
digitalWrite(EngineRedLed, HIGH);

BlinkRed(500);
BlinkYellow(450);
BlinkGreen(100);
}

void BlinkRed (int interval){
static long prevMill = 0;
if (((long)millis() - prevMill) >= interval){
prevMill = millis();
digitalWrite(CabinRedLed, !digitalRead(CabinRedLed));
}
}
void BlinkYellow (int interval){
static long prevMill = 0;
if ((millis() - prevMill) >= interval){
prevMill = millis();
digitalWrite(CabinYellowLed, !digitalRead(CabinYellowLed));
}
}
void BlinkGreen (int interval){
int stateButton = digitalRead(LaserButton);
if(stateButton == 1) {
static long prevMill = 0;
if ((millis() - prevMill) >= interval){
prevMill = millis();
digitalWrite(LaserGreenLed, !digitalRead(LaserGreenLed));
}
}
}

Darth_Vader_Tie_Fighter3.ino.ino (1.24 KB)

You've set the button pin to INPUT so do you have an external pull-down resistor?

Yep - i have the button on pin 2 - green led on pin 8 - wired like attachment

It works fine … when button is depressed - led flashed away as i want it to … but sometimes - when released - it stays on ( not flashing )
looks like if release when led is lit ( while flashing ) it stays on
if released exactly when led is off - it stays off like its supposed to ( tricky because its a fast blink )

thanks !!

Please edit your post and add code tags

Type
** **[code]** **
before your post
Type
** **[/code]** **
after your post

You will need to add an else to the below

  if (stateButton == 1)
  {
    ...
    ...
  }

Reason is that if the led is on, it will stay on if it does not go through the 'blink' part.

In the else, you will need to switch the led off; you might also want to 'reset' the timing so when you press the button again it starts at the beginning of a cycle and not halfway.

void BlinkGreen (int interval){
 int stateButton = digitalRead(LaserButton);
 if(stateButton == 1) {
  static long prevMill = 0;
  if ((millis() - prevMill) >= interval){ 
   prevMill = millis(); 
   digitalWrite(LaserGreenLed, !digitalRead(LaserGreenLed));
 }
}

The lines of code that could turn off that light are INSIDE the if statement checking the button. So it can only be turned off if the button is pressed. Instead, in the if statement, just turn it on and take the current time. Then put the turn off code outside the if so that it constantly runs and constantly checks to see if it is time to turn it off regardless of whether the button is pressed.

perfect - thanks sterretje

works fine now ( no more lingering led stay on symptoms )
added else to the loop

code
void BlinkGreen (int interval){
int stateButton = digitalRead(LaserButton);
if(stateButton == 1) {
static long prevMill = 0;
if ((millis() - prevMill) >= interval){
prevMill = millis();
digitalWrite(LaserGreenLed, !digitalRead(LaserGreenLed));
}
} else {
digitalWrite(LaserGreenLed, LOW);
}
}
/code

thanks all !!

1 Like

Great

Next time, use
* *[code]* *
and
* *[/code]* *
around your code instead of plain code and /code :wink: