Why my led is not blinking?

this is my code: (question is on the end of the code)

define firstButton 8

define secondButton 9

define thirdButton 10

define greenLed 11

define blueLed 12

define redLed 13

int ledDelay = 2000;

void setup()
{
pinMode(firstButton, INPUT);
pinMode(secondButton, INPUT);
pinMode(thirdButton, INPUT);
pinMode(greenLed, OUTPUT);
pinMode(blueLed, OUTPUT);
pinMode(redLed, OUTPUT);
}

void loop()
{
if ( digitalRead(firstButton) == HIGH )
{
digitalWrite(greenLed, HIGH);
delay(ledDelay);
digitalWrite(greenLed, LOW);
}
else if ( digitalRead(secondButton) == HIGH )
{
digitalWrite(blueLed, HIGH);
delay(ledDelay);
digitalWrite(blueLed, LOW);
}
else if ( digitalRead(thirdButton) == HIGH )
{
digitalWrite(redLed, HIGH);
delay(ledDelay);
digitalWrite(redLed, LOW);
}
}
so my question is that when my code executes and runs on Arduino and when I press my firstbutton and leave it to make my IF statements condition TRUE and after the condition is TRUE. it first turns ON the green led and after that 2-SEC DELAY it turns OFF the led but instead of leaving the button if I hold it then it must turn ON the led and then after a 2-sec delay it will turn OFF and these will repeat again and again but instead of this led remains ON as long as I keep pressing the button. SO WHY IT'S NOT BLINKING?
WHY IT IS NOT READING NEXT TWO LINES AFTER digitalWrite(greenLed, HIGH); AND SKIPPING TO IF STATEMENT?

Use CTRL T to format your code then copy it and paste it here between code tags, i.e. between 2 sets of 3 grave accents ```
``` Your code is pasted here ```

1 Like

Think about it...if you hold the button down then right after the LED is turned off loop() is called again and it is turned right back on so fast you can't see it turning off.

1 Like

I think you need to add a second delay() after the led turns off.
In your code the led actually turns off, but when the loop repeats itself, the led turns instantly on.
You want it to blink whenever you press any button, right?
Take a look at this (and compare it with your code):

#define firstButton 8
#define secondButton 9
#define thirdButton 10
#define greenLed 11
#define blueLed 12
#define redLed 13

int ledDelay = 2000;

void setup()
{
  pinMode(firstButton, INPUT);
  pinMode(secondButton, INPUT);
  pinMode(thirdButton, INPUT);
  pinMode(greenLed, OUTPUT);
  pinMode(blueLed, OUTPUT);
  pinMode(redLed, OUTPUT);
}

void loop()
{
  if ( digitalRead(firstButton) == HIGH )
  {
    digitalWrite(greenLed, HIGH);
    delay(ledDelay);
    digitalWrite(greenLed, LOW);
    delay(ledDelay);
  }
  else if ( digitalRead(secondButton) == HIGH )
  {
    digitalWrite(blueLed, HIGH);
    delay(ledDelay);
    digitalWrite(blueLed, LOW);
    delay(ledDelay);
  }
  else if ( digitalRead(thirdButton) == HIGH )
  {
    digitalWrite(redLed, HIGH);
    delay(ledDelay);
    digitalWrite(redLed, LOW);
    delay(ledDelay);
  }
}

Good luck :slightly_smiling_face:

1 Like

are your buttons connected between the pin and ground, are they active LOW?

1 Like

thank u sir for your advice.

yes.

if the buttons are connected to ground, shouldn't the code check that the input is LOW, not HIGH?

1 Like

i am sorry they are active high.

presumably the button is connected between the pin and 5V.
what pulls the input LOW when the button is not pressed?

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