user31108: 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); } 
}


user31137: 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.   

user24459: 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.

31108: <p>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</p>

<pre><code> 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 &gt;= 4); 
{ digitalWrite(led3, HIGH); }
}
else { digitalWrite(led1, LOW); } 
}
</code></pre>
 31137: <p>Your code won't work as is. Each time you assign a value to <code>state</code> you're overwriting it in the next line. You could, for example have multiple int state variables such as <code>state1</code> for the first line in the loop then <code>state2</code> in the second etc. Then you need to compare values (don't use <code>=</code> on its own in a compare as it will assign the value being tested - use <code>==</code> instead). This way you gather all the button states first.</p>

<p>Or you could test the state of a button and if pressed add 1 to <code>state</code>:</p>

<pre><code>if (digitalRead(bp1)) {
    state++};
</code></pre>

<p>Then you compare the number of button presses counted in state as you were doing and light the LEDs based on that.</p>

<p>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.</p>

<p>R3.   </p>
 24459: <p>To get the functionality you want the code should be:</p>

<pre><code>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 &gt;= 1) 
   digitalWrite(led1, HIGH);
 else
   digitalWrite(led1, LOW);


 if (state &gt;= 3 ) 
   digitalWrite(led2, HIGH);
 else
   digitalWrite(led2, LOW);

 if (state &gt;= 4) // you had a ; here.
   digitalWrite(led3, HIGH);
 else
   digitalWrite(led3, LOW);
}
</code></pre>

<p>There are more shorthand ways to write this e.g.<code>if (digitalRead(bp4) == HIGH) state = state + 1;</code> could be replaced with <code>digitalRead(bp4)?state++;</code> but until you are familiar with programming it's often best to use the longer but clearer way of writing things.</p>

<p>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.</p>

<p>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.</p>
