2

I want my LED to have three states with push button

  1. on
  2. blinking
  3. off

But the second state didn't blink, it is only off-->on. Here's the code:

#define btn 7
#define led 8
int button;
int a;

void setup() {
  Serial.begin(9600);
  pinMode(led,OUTPUT);
  pinMode(btn,INPUT);
  // put your setup code here, to run once:

}

void loop() {
  Serial.print("a==");
  Serial.println(a);
  button = digitalRead(btn);
  if(button ==1){
    delay(100);
    a++;
    

    if(a ==1){
      delay(100);
      digitalWrite(led,HIGH);
    }
    if(a == 2){
      digitalWrite(led,LOW);
      delay(500);
      digitalWrite(led,HIGH);
      delay(500);
    }
    if(a ==3){
      digitalWrite(led,LOW);
      delay(100);
      a = 0;
    }
  }
}

I tried using while statement but i couldn't understand how it works😓😓

1
  • Nothing with your code technically. It just that your eyes can't really see the blinking and your blinking always end with LED on, so it gives you a perceived on instead of blinking. Try to blink the led a few times with for (int i = 0; i<20; i++) {digitalWrite(led, !digitalRead(led); delay(100); }, you will see the code is working as what you expected. Commented Aug 27 at 0:35

1 Answer 1

2

The blinking logic in state 2 (a = 2) only executes once when the button is pressed, then immediately moves on. In my code I've used millis() instead of delay() for the blinking state so the code doesn't block and can continue checking for button presses. I've added also lastButton variable to detect when the button is pressed (transition from 0 to 1) rather than continuously triggering while held:

#define btn 7
#define led 8
int button;
int lastButton = 0;
int state = 0; // 0=off, 1=on, 2=blinking
unsigned long previousMillis = 0;
const long interval = 500;
bool ledState = false;

void setup() {
  Serial.begin(9600);
  pinMode(led, OUTPUT);
  pinMode(btn, INPUT);
}

void loop() {
  button = digitalRead(btn);
  
  // Detect button press (rising edge)
  if (button == 1 && lastButton == 0) {
    delay(50); // debounce
    state++;
    if (state > 2) state = 0;
    Serial.print("State: ");
    Serial.println(state);
  }
  lastButton = button;
  
  // Handle LED based on current state
  if (state == 0) {
    // OFF
    digitalWrite(led, LOW);
  }
  else if (state == 1) {
    // ON
    digitalWrite(led, HIGH);
  }
  else if (state == 2) {
    // BLINKING
    unsigned long currentMillis = millis();
    if (currentMillis - previousMillis >= interval) {
      previousMillis = currentMillis;
      ledState = !ledState;
      digitalWrite(led, ledState);
    }
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the advice! I just tried it and it worked so well. I encoumtered a new things but i'll learn it tyyy
@BimoAnggoro I am happy to help you learn more. Would you mind accepting the answer if it suits your needs?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.