How can I make the code of this project where I will press one time a button, to turn on the first led (and turn off second led) and when other one press to turn on second led (and turn off the first led).

  • 2
    Your question is barely answerable and shows no effort of trying it yourself. You could try starting out with Arduino Tutorials instead of a project directly. – Paul Mar 4 '19 at 9:07
  • This is a poor quality lazy question. You need to exert some effort. – Duncan C Mar 4 '19 at 12:02
  • From the question it appears that person's first language is not english, it looks like it was possible processed through some translator. – Gil Mar 4 '19 at 19:34

You can do this by keeping the 'state' saved in a variable. Depending on it's state, the button will have a different effect.

After this effect, the state is updated, so that next iteration it will behave according to the other state.

state = led1
   if(state == led2){
     state = led1;
     state = led2;
  • 1
    Also check the Arduino example about debouncing buttons. – Michel Keijzers Mar 4 '19 at 9:28
  • You do the OP a disservice by giving them functioning code. This allows "copy/paste programming" without learning. – Duncan C Mar 4 '19 at 22:58
  • This code isn't completely functional and only to illustrate how using state works. I somewhat agree that I could have used a different example as the case for which this person was asking. – Paul Mar 5 '19 at 8:44

Not the answer you're looking for? Browse other questions tagged or ask your own question.