Maker Pro
Maker Pro

Arduino led

hi i was playing with arduino simulator and was trying to make led 1 ON when i press the button and when release make it OFF.

but i am finding difficulty like when i press button led 1 is on ,then on second press i want LED 2 on .
so please help me with that

this is the code i was trying .

void loop()
{

while(digitalRead(3)== LOW )
{



digitalWrite(count1, HIGH);


}
digitalWrite(count1, LOW);




}
 

Harald Kapp

Moderator
Moderator
You will need a simple state machine, i.e. some logic that "knows" which LEDs are ON and what to do next. Something along these lines:

Code:
int LED_state = 0; // no LED on

if button_is_pressed // wait for button press by whatever method you chose
{
   switch(LED_state) {
       case 0 : // no LED is on
            LED1 = ON; //use whatever code is necessary to turn LED 1 on
            LED_state = 1; // forward LED_state to indicate that LED 1 is now ON
            break; //exit from switch statement
        case 1 : // LED 1 is on
            LED2 = ON; //use whatever code is necessary to turn LED 2 on
            LED_state = 2; // forward LED_state to indicate that LED 2 is now ON
            break; 
        default :
            break;
   }
}
You can easily extend this code for more LEDs or for turning off the LEDs at some point.
 
Top