Maker Pro
Maker Pro

Arduino led

Amit Dhiman

Oct 14, 2017
9
Joined
Oct 14, 2017
Messages
9
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
Nov 17, 2011
13,700
Joined
Nov 17, 2011
Messages
13,700
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