I am behind a problem of controling multiple leds with multiple push buttons. Lets say that I have 5 push buttons and 3 leds. Led1 turns on if at least 1 out of the 5 push buttons is pressed. Led2 turns on if at lest 3 push buttons are pressed and Led3 turns on if at least 4 push buttons are pressed. How can this be solved ? With if else ?arrays? here is my code that i have
const int led1 = 2;
const int led2 = 3;
const int led3 = 4;
const int bp1 = 9;
const int bp2 = 8;
const int bp3 = 7;
const int bp4 = 6;
const int bp5 = 5;
int state;
void setup()
{
Serial.begin(9600);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(bp1, INPUT);
pinMode(bp2, INPUT);
pinMode(bp3, INPUT);
pinMode(bp4, INPUT);
pinMode(bp5, INPUT);
}
void loop()
{
state = digitalRead(bp1);
state = digitalRead(bp2);
state = digitalRead(bp3);
state = digitalRead(bp4);
state = digitalRead(bp5);
if (state = HIGH)
{ digitalWrite(led1, HIGH); }
if (state = 3 )
{ digitalWrite(led2, HIGH);
if (state >= 4);
{ digitalWrite(led3, HIGH); }
}
else { digitalWrite(led1, LOW); }
}
-
Please format your text so it can be readenEnric Blanco– Enric Blanco2017年02月19日 03:09:29 +00:00Commented Feb 19, 2017 at 3:09
-
i did it but i don't know exactly how to edit it very nicelyMishMash– MishMash2017年02月19日 03:18:50 +00:00Commented Feb 19, 2017 at 3:18
-
1When including code in a stackexchange question or answer, first paste the code into the question or answer editing box; then in that editing box, highlight the code and press ctrl-k. Or highlight the code and click the {} icon in the toolbar at the top of the editing box. To highlight text click-drag across it or use the arrow keys while holding the shift key.James Waldby - jwpat7– James Waldby - jwpat72017年02月19日 03:57:28 +00:00Commented Feb 19, 2017 at 3:57
4 Answers 4
To get the functionality you want the code should be:
void loop()
{
state = 0;
if (digitalRead(bp1) == HIGH)
state = state + 1;
if (digitalRead(bp2) == HIGH)
state = state + 1;
if (digitalRead(bp3) == HIGH)
state = state + 1;
if (digitalRead(bp4) == HIGH)
state = state + 1;
if (digitalRead(bp5) == HIGH)
state = state + 1;
if (state >= 1)
digitalWrite(led1, HIGH);
else
digitalWrite(led1, LOW);
if (state >= 3 )
digitalWrite(led2, HIGH);
else
digitalWrite(led2, LOW);
if (state >= 4) // you had a ; here.
digitalWrite(led3, HIGH);
else
digitalWrite(led3, LOW);
}
There are more shorthand ways to write this e.g.if (digitalRead(bp4) == HIGH) state = state + 1;
could be replaced with digitalRead(bp4)?state++;
but until you are familiar with programming it's often best to use the longer but clearer way of writing things.
If all you are doing is lighting LEDs then de-bouncing the buttons isn't necessary, any LED flicker caused by the contacts bouncing won't be visible to your eye.
If however at a later date you want to trigger other logic in your program to run once per button press then you will need to worry about that. All buttons and switches bounce a little when you close them, without something in the electronics or software to cope with that a single button press can look like a dozen very quick presses to your software.
Your code won't work as is. Each time you assign a value to state
you're overwriting it in the next line. You could, for example have multiple int state variables such as state1
for the first line in the loop then state2
in the second etc. Then you need to compare values (don't use =
on its own in a compare as it will assign the value being tested - use ==
instead). This way you gather all the button states first.
Or you could test the state of a button and if pressed add 1 to state
:
if (digitalRead(bp1)) {
state++};
Then you compare the number of button presses counted in state as you were doing and light the LEDs based on that.
However you will likely have a problem with contact bounce on the switches. This happens when a switch is closing and the voltage is not steady for about 20ms (imaging a switch sparking as it is closing). To get around this you'll need either some additional components or add a time delay in your code for each button press to check it several times until the voltage is stable. Do a Google search for 'Arduino contact bounce' for solutions.
R3.
I would do this:
state = (state & ~STATE_MASK1) | ((digitalRead(bp1)==HIGH)?STATE_MASK1:0); //assuming buttons are active high
...
if (state & STATE_MASK1)
{ digitalWrite(led1, HIGH); }
it holds up to 8 button states for a uint8_t type.
edit: here is a quick implementation.
//state = 0; //reset the state
//read the buttons - all active high
state = (digitalRead(bp1)==HIGH)?STATE_BP1:0;
state|= (digitalRead(bp2)==HIGH)?STATE_BP2:0;
state|= (digitalRead(bp3)==HIGH)?STATE_BP3:0;
state|= (digitalRead(bp4)==HIGH)?STATE_BP4:0;
state|= (digitalRead(bp5)==HIGH)?STATE_BP5:0;
//process the buttons
digitalWrite(led1, (state & STATE_BP1)?HIGH:LOW); //active high - led1 is high if bp1 is high
digitalWrite(led2, (state & (STATE_BP2 | STATE_BP4))?LOW:HIGH); //active low - led2 mimics bp2|bp4 => led2 high if both bp2+bp4 are low
digitalWrite(led3, ((state & STATE_BP3) && (~state & STATE_BP5))?HIGH:LOW); //active high -> led3 is high if bp3 is high and bp5 low
the code controls 3 leds:
led1: it mimics bp1;
led2: it goes high if both bp2/bp4 are high;
led3: it goes high if bp3 is high and bp5 is low.
I picked the output logic for no particular reason.
here is the output:
you can check the waveform against the logic, :)
again, this is just one particular implementation and you can write your own code / logic to produce your desired outcome. the principle is the same.
-
Thank you all very much for your time and help, much appreciated ! Now I am going to read and to study everything.MishMash– MishMash2017年02月20日 19:36:52 +00:00Commented Feb 20, 2017 at 19:36
-
I read everything, tested both codes and still not working :(MishMash– MishMash2017年02月20日 20:33:12 +00:00Commented Feb 20, 2017 at 20:33
-
My programming level unfortunately is not yet so good to fully understand above written: (MishMash– MishMash2017年02月23日 21:55:22 +00:00Commented Feb 23, 2017 at 21:55
I solved my problem by myself with if..else structures. I am happy that I got this working and I learned something new. Thank you all for help and inputs.
int led1 = 2;
int led2 = 3;
int led3 = 4;
int bp1 = 9;
int bp2 = 8;
int bp3 = 7;
int bp4 = 6;
int bp5 = 5;
void setup()
{
Serial.begin(9600);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(bp1, INPUT);
pinMode(bp2, INPUT);
pinMode(bp3, INPUT);
pinMode(bp4, INPUT);
pinMode(bp5, INPUT);
}
void loop()
{
if (digitalRead(bp1)== HIGH or digitalRead(bp2)== HIGH or digitalRead(bp3)== HIGH or digitalRead(bp4)== HIGH or digitalRead(bp5)== HIGH )
{
digitalWrite(led1, LOW);
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);
}
else
{
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);
}
if (digitalRead(bp1)== HIGH and digitalRead(bp4)== HIGH and digitalRead(bp5)== HIGH or digitalRead(bp1)== HIGH and digitalRead(bp2)== HIGH and digitalRead(bp3)== HIGH or digitalRead(bp1)== HIGH and digitalRead(bp2)== HIGH and digitalRead(bp4)== HIGH or digitalRead(bp1)== HIGH and digitalRead(bp3)== HIGH and digitalRead(bp5)== HIGH or digitalRead(bp1)== HIGH and digitalRead(bp2)== HIGH and digitalRead(bp5)== HIGH or digitalRead(bp2)== HIGH and digitalRead(bp4)== HIGH and digitalRead(bp5)== HIGH or digitalRead(bp3)== HIGH and digitalRead(bp4)== HIGH and digitalRead(bp5)== HIGH or digitalRead(bp2)== HIGH and digitalRead(bp3)== HIGH and digitalRead(bp5)== HIGH or digitalRead(bp2)== HIGH and digitalRead(bp3)== HIGH and digitalRead(bp4)== HIGH)
{
digitalWrite(led1, HIGH);
digitalWrite(led2, LOW);
digitalWrite(led3, HIGH);
}
else
{
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);
}
if (digitalRead(bp2)== HIGH and digitalRead(bp3)== HIGH and digitalRead(bp4)== HIGH and digitalRead(bp5)== HIGH or digitalRead(bp1)== HIGH and digitalRead(bp2)== HIGH and digitalRead(bp4)== HIGH and digitalRead(bp5)== HIGH or digitalRead(bp1)== HIGH and digitalRead(bp2)== HIGH and digitalRead(bp3)== HIGH and digitalRead(bp4)== HIGH or digitalRead(bp1)== HIGH and digitalRead(bp2)== HIGH and digitalRead(bp3)== HIGH and digitalRead(bp5)== HIGH)
{digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(led3, LOW);
}
else
{ digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);
}
}
-
1Your solution works, that's nice. But it's far from efficient. You check the state of the button for ridiculous amount in each loop. You may want to consider another algorithm, as seen on another answer here. And since the problem is solved, don't forget to accept an answer :)duck– duck2017年02月24日 03:36:31 +00:00Commented Feb 24, 2017 at 3:36