Hello.
I bought 2 buttons like this: (different color, same number of legs) ;
On my breadboard i managed to give them 5V each by wiring them in parallel. Everything good untill now!
I have 3 leds on my breadboard and i want to control them using the buttons (left and right), to be more precisely:
Start with center led turned on and stay ON untill a button is pressed. Press left/right button, turn off the previous led (center one) and turn on the one on the left/on the right untill another button is pressed. But it doesn`t work: when i press right or left, sometimes all leds blink, or i need to press multiple times a button, sometimes it skips a led, it has many bugs…
This is my code:
int left=2;
int right=4;
int led[3]={11,12,13};
int LF;
int RG;
int j=1;
void setup()
{
pinMode(left, INPUT);
pinMode(right, INPUT);
for(int i=0;i<3;i++)
pinMode(led[i], OUTPUT);
digitalWrite(led[j],HIGH); //start with center led turned ON
}
void loop()
{
LF = digitalRead(left); //read left button
RG = digitalRead(right); //read right button
if (LF == HIGH) // if left button is pressed
{
digitalWrite(led[j],LOW);
j=j+1;
if(j>2) //end of the array
j=0;
if(j<0) //the other end of the array
j=2;
digitalWrite(led[j], HIGH);
}
if (RG == HIGH) //if right button is pressed
{
digitalWrite(led[j],LOW);
j=j-1;
if(j>2) //end of the array
j=0;
if(j<0) //the other end of the array
j=2;
digitalWrite(led[j], HIGH);
}
}
I bought some 220 ohm resistors and 4 x 20K ohms (wired in parallel 2 - to obtain 10K ohms)... And my setup looks like this:
Did i wired corectly the 2x20K ohms in parallel to obtain 10K for the button?
Wire Colors:
Yellow = 5V (Orange and Red should proive 5V each (parallel) for the 2 buttons)
Brown = GND
Orange + Green = left button pin 2 and right button pin 4
Purple + Green + Red = pins 11 12 13 for leds
But the code doesnt work... When i press a button, all leds turn on. After that sometimes the correct led stays on, sometimes other led stays on. I mean... the code doesnt do what i want to do :0
It looks like you have not wired those LEDs in series with the resistors, they all look to be on the same line so the resistors are shorting out.
Thanks for the video.
What you are forgetting is that the the loop function repeats many hundreds of times a second, so when you have a button held down it just zips through lighting all the LEDs in turn. That is why it looks like they are all on. One way round this is to wait until a button is released before looking for anything else. One way to do this is here:-
int left=2;
int right=4;
int led[3]={11,12,13};
int LF;
int RG;
int j=1;
void setup()
{
pinMode(left, INPUT);
pinMode(right, INPUT);
for(int i=0;i<3;i++)
pinMode(led[i], OUTPUT);
digitalWrite(led[j],HIGH); //start with center led turned ON
}
void loop()
{
LF = digitalRead(left); //read left button
RG = digitalRead(right); //read right button
if (LF == HIGH) // if left button is pressed
{
digitalWrite(led[j],LOW);
j=j+1;
if(j>2) //end of the array
j=0;
if(j<0) //the other end of the array
j=2;
digitalWrite(led[j], HIGH);
while(digitalRead(left) == HIGH) { } // wait until you release the button
delay(30); // debounce delay
}
if (RG == HIGH) //if right button is pressed
{
digitalWrite(led[j],LOW);
j=j-1;
if(j>2) //end of the array
j=0;
if(j<0) //the other end of the array
j=2;
digitalWrite(led[j], HIGH);
while(digitalRead(right) == HIGH) { } // wait until you release the button
delay(30); // debounce delay
}
}
The other way to do this is to look not for a high but for an edge. That is when the button is high but the last time round the loop it was low. This looks for a change in the button state not simply just a level.
You do that like this:-
int left=2;
int right=4;
int led[3]={11,12,13};
int LF;
int RG;
int j=1;
void setup()
{
pinMode(left, INPUT);
pinMode(right, INPUT);
for(int i=0;i<3;i++)
pinMode(led[i], OUTPUT);
digitalWrite(led[j],HIGH); //start with center led turned ON
}
void loop()
{
static int lastLeft,lastRight;
LF = digitalRead(left); //read left button
RG = digitalRead(right); //read right button
if ((LF == HIGH) && (LF != lastLeft)) // if left button has just been pressed
{
digitalWrite(led[j],LOW);
j=j+1;
if(j>2) //end of the array
j=0;
if(j<0) //the other end of the array
j=2;
digitalWrite(led[j], HIGH);
delay(30);
}
if ((RG == HIGH) && (RG != lastRight) ) //if right button has just been pressed
{
digitalWrite(led[j],LOW);
j=j-1;
if(j>2) //end of the array
j=0;
if(j<0) //the other end of the array
j=2;
digitalWrite(led[j], HIGH);
delay(30);
}
lastLeft = LF; // save current values so you can detect a edge rather than a just a push
lastRight = RG;
}
If you look in the arduino IDE under File -> examples -> 02.Digital -> StateChangeDetection it gives you another example.
What it does is to detect the first time that the button is pressed and ignores when it is held down.
You see when the code is like you had it you incremented the LED you wanted to turn on every time round the loop. This meant this happened thousands of times a second. That last code only triggers your LEDs when the last time round the loop the button was unpressed and this time round the loop it is pressed. Therefore you only detect when the button changes not all the time it is held down.