Help with LED and button

Hi there, can someone fix my code? so i want GreenLed will blinking for a while then it will stays on if button is pressed, if button not pressed, RedLight will blinking forever. How can i fix my code?

My Code : GreenLed always blinking when button pressed

here's my cod:

int buttonPin = 12;
int GreenLed = 3;
int RedLed = 2;

void setup() {
  pinMode(buttonPin, INPUT_PULLUP);
  // Define pin #13 as output, for the LED
  pinMode(GreenLed, OUTPUT);
  pinMode(RedLed, OUTPUT);
}

void loop(){
  int buttonValue = digitalRead(buttonPin);
  
  if (buttonValue == LOW){  
    digitalWrite(GreenLed, LOW);  
    delay(200);
    digitalWrite(GreenLed, HIGH);  
    delay(200);
    digitalWrite(GreenLed, LOW);  
    delay(200);
    digitalWrite(GreenLed, HIGH);  
    delay(200);
    digitalWrite(GreenLed, LOW);  
    delay(200);
    digitalWrite(GreenLed, HIGH);  
    delay(200);
    digitalWrite(GreenLed, LOW);  
    delay(200);
    digitalWrite(GreenLed, HIGH);  
    delay(200);
    digitalWrite(RedLed, HIGH);
  } else {
    digitalWrite(GreenLed, HIGH);
    digitalWrite(RedLed, LOW);  
    delay(200);
    digitalWrite(RedLed, HIGH);  
    delay(200);
    digitalWrite(RedLed, LOW);  
    delay(200);
    digitalWrite(RedLed, HIGH);  
    delay(200);
    digitalWrite(RedLed, LOW);  
    delay(200);
    digitalWrite(RedLed, HIGH);  
    delay(200);
    digitalWrite(RedLed, LOW);  
    delay(200);
    digitalWrite(RedLed, HIGH);  
    delay(200);
  }
  
}}

Thanks, i hope someone fix my code

i think you need to build a little state machine, a switch statement

in the idle state, you call a code to flash the red LED.

when you press the button (while you hold the button down), you set a flashing green state

while in the flash green state, you either wait for time to pass or count the number of flashes and transition to a final state where the green LED is held on.

while in that final state, recognizing a button press, not that it is held down, resets the state to idle

gcjr:
i think you need to build a little state machine, a switch statement

in the idle state, you call a code to flash the red LED.

when you press the button (while you hold the button down), you set a flashing green state

while in the flash green state, you either wait for time to pass or count the number of flashes and transition to a final state where the green LED is held on.

while in that final state, recognizing a button press, not that it is held down, resets the state to idle

How can i make that? can you tell me how to do it? is there any reference?

adrian12000:
How can i make that? can you tell me how to do it? is there any reference?

This might help: State Machine
Also, BlinkWithoutDelay

It's not so complicated that you need any reference. Just make a variable, call it 'flashing' for example. Then you construct two branches that you execute depending on the state of 'flashing':

bool flashing = false;
void loop() {
...
if (flashing) {
...}
else {
...}
}

Inside these blocks you do the things that you should do when flashing and not flashing. You change from one to the other by changing the value of 'flashing' to true or false..

aarg:
It's not so complicated that you need any reference. Just make a variable, call it 'flashing' for example. Then you construct two branches that you execute depending on the state of 'flashing':

bool flashing = false;

void loop() {
...
if (flashing) {
...}
else {
...}
}



Inside these blocks you do the things that you should do when flashing and not flashing. You change from one to the other by changing the value of 'flashing' to true or false..

Can you give an example? i'm a newbie so i don't understand it :frowning:

adrian12000:
Hi there, can someone fix my code? so i want GreenLed will blinking for a while then it will stays on if button is pressed, if button not pressed, RedLight will blinking forever. How can i fix my code?

Make your statement clear:

1. "GreenLed will (be) blinking for a while" -- for how long? You need to give the time duration.

2. If you press the button within that duration, the GreenLED will stay ON. Is it correct?

3. If you don't press the button within that duration, the GreenLED will be eventually OFF.

4. After that the RedLight will start blinking for ever. Is it correct?

If you answer the above questions, you may expect some help.

GolamMostafa:
Make your statement clear:

1. "GreenLed will (be) blinking for a while" -- for how long? You need to give the time duration.

2. If you press the button within that duration, the GreenLED will stay ON. Is it correct?

3. If you don't press the button within that duration, the GreenLED will be eventually OFF.

4. After that the RedLight will start blinking for ever. Is it correct?

If you answer the above questions, you may expect some help.

i'll answer your questions:

  1. It will blinking 20 times with an interval of 200ms then become solid (button still pressed)
  2. Yes correct. GreenLED will blinking for 20 times then become stays ON forever until i release the button
  3. Yes correct, GreenLED OFF and RedLED blinking for ever
  4. Correct :slight_smile:

This is what i planned for

Post number 3 explains what you have to do. Ignore your requirements and your code and start there.

That means:

Assuming button (K1) is at open condition as per following diagram (Fig-1):
led2Swx.png
Figure-1:

1. GreenLed starts blinking at 200 ms interval (ON-100 ms delay-OFF-100 ms delay). Make the interval for 1000 ms so that you can clearly observe that the GreenLed and RedLight blinks.

2. If you press the button within that duration (20 sec = 20 blinks), the GreenLED will stay ON. Is it correct?

3. If you don't press the button within that duration (20 sec = 20 blinks), the GreenLED will be
eventually OFF.

4. After that the RedLight will start blinking for ever. Is it correct?

The above four tasks could be accomplished by the following codes: Create sketch and insert these codes at the appropriate place; upload the sketch and report the result. Don't forgrt to initialize variables as needed like pin directions, counter etc.

bool n = digitalRead(12);    //check button's open/close status
if(n != LOW)   //button L1 is not closed
{
    digitalWrite(3, HIGH);
    delay(500);
    digitalWrite(3, LOW);
    delay(500);
    counter++;       //increment counter
    if(counter == 20)  //GreenLed has finished blinking for 20 times
    {
       digitalWrite(3, LOW);
       while(1);            //wait for ever
    }
}
else
{
   digitalWrite(3, LOW);   //button is closed; GreenLed stays ON  
   //------------------------------------------------------------------
   while(1)    //keep blinking RedLight for ever at 1-sec interval
   {
      digitalWrite(2, HIGH);
      delay(500);
      digitalWrite(2, LOW);
      delay(500);
   }
}

led2Swx.png

consider

#define pinBut  A1
#define pinGrn  12
#define pinRed  13

#define ON  LOW
#define OFF HIGH

enum {Idle, Flashing, Solid};
int  state = Idle;
int  count;

unsigned long msecLst = 0;

void
setup (void)
{
    Serial.begin (115200);

    pinMode (pinBut, INPUT_PULLUP);
    pinMode (pinGrn, OUTPUT);
    pinMode (pinRed, OUTPUT);
    digitalWrite (pinGrn, OFF);
}

// -----------------------------------------------------------------------------
void
flash (
    byte  pin)
{
    unsigned long msec    = millis();

#define Period  200
    if (msec - msecLst > Period)  {
        msecLst = msec;

        digitalWrite (pin, ! digitalRead (pin));
        count++;
    }
}

// -----------------------------------------------------------------------------
void
loop (void)
{
    byte butState = digitalRead (pinBut);

    switch (state) {
    case Flashing:
        if (HIGH == butState)  {
            state = Idle;
            break;
        }

        flash (pinGrn);
#define MAX_COUNT 10
        if (MAX_COUNT == count)  {
            state = Solid;
            digitalWrite (pinGrn, ON);
        }
        break;

    case Idle:
        digitalWrite (pinGrn, OFF);
        flash (pinRed);
        if (LOW == butState)  {
            state = Flashing;
            count = 0;
            digitalWrite (pinRed, OFF);
        }
        break;

    case Solid:
    default:
        if (HIGH == butState)  {
            state = Idle;
        }
        break;
    }
}

Thanks everyone it worked now :smiley: