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).
-
2Your 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.aaa– aaa2019-03-04 09:07:44 +00:00Commented Mar 4, 2019 at 9:07
-
This is a poor quality lazy question. You need to exert some effort.Duncan C– Duncan C2019-03-04 12:02:39 +00:00Commented Mar 4, 2019 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– Gil2019-03-04 19:34:19 +00:00Commented Mar 4, 2019 at 19:34
Add a comment
|
1 Answer
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(buttonpressed){
if(state == led2){
led1On();
led2Off();
state = led1;
}else{
led1Off();
led2On();
state = led2;
}
}
-
1Also check the Arduino example about debouncing buttons.Michel Keijzers– Michel Keijzers2019-03-04 09:28:26 +00:00Commented Mar 4, 2019 at 9:28
-
You do the OP a disservice by giving them functioning code. This allows "copy/paste programming" without learning.Duncan C– Duncan C2019-03-04 22:58:50 +00:00Commented Mar 4, 2019 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.aaa– aaa2019-03-05 08:44:36 +00:00Commented Mar 5, 2019 at 8:44