Skip to content Skip to sidebar Skip to footer

Turn On 3 Leds Alternately using 1 Button Arduino

Turn On 3 Leds Alternately using 1 Button Arduino - Sometimes we want to turn on some led with a button, just a button. How to turn it on? here I give you a simulation of turn on 3 led with a button using an arduino simulator called SimulIDE. Download SimulIDE here. 



Code:
int led_1 = 13;
int led_2 = 12;
int led_3 = 11;
int button= 10;
int i = 0;
int button_state = 0;

void setup() {
  pinMode( led_1, OUTPUT );
  pinMode( led_2, OUTPUT );
  pinMode( led_3, OUTPUT );
  pinMode( button, INPUT );
}

void loop() {
  button_state = digitalRead (button);
  
  if (button_state == HIGH){
    i++;
  }

  if (i == 1){
    digitalWrite(led_1, HIGH);
    digitalWrite(led_2, LOW);
    digitalWrite(led_3, LOW);
    delay(100);
  }

  if (i == 2){
    digitalWrite(led_1, LOW);
    digitalWrite(led_2, HIGH);
    digitalWrite(led_3, LOW);
    delay(100);
  }

  if (i == 3){
    digitalWrite(led_1, LOW);
    digitalWrite(led_2, LOW);
    digitalWrite(led_3, HIGH);
    delay(100);
    i = 0;
  }
}


Video:

Post a Comment for "Turn On 3 Leds Alternately using 1 Button Arduino"