controlling 2 leds with 2 push buttons

Hi,

I am using 2 push buttons to control 2 leds, one button specific to each.
when i change the state of one push button, corresponding LED lights up, but when i change the state of the other button, the other led should light up and first led should be written low.

I tried loops, but since the push button status is momentary, i have trouble keeping led in the HIGH state.
Have attached the code,
it is

const int VRAB = 7; //button 1
const int VRAL = 12; // led1
const int VRCB = 6; // button 2
const int VRCL = 11; // led 2
int val = 0;
int val1 = 0;
int state = 0;
int state1 = 0;
int jump=0;
void setup() {
 // put your setup code here, to run once:
pinMode(VRAB,INPUT);
pinMode(VRAL,OUTPUT);
pinMode(VRCB,INPUT);
pinMode(VRCL,OUTPUT);
}

void loop() {
 // put your main code here, to run repeatedly:
val=digitalRead(VRAB);
val1=digitalRead(VRCB);

if(val==HIGH && val1==LOW)
{jump = 1; state=1; state1=0;}
else if(val==LOW && val1==HIGH)
{jump = 2; state=0; state1=1;}

switch (jump) {

 case 1:
 digitalWrite(VRAB,HIGH);
 digitalWrite(VRCB,LOW);
 break;

 case 2:
 digitalWrite(VRAB,LOW);
 digitalWrite(VRCB,HIGH);
 break;
}
}

Please help.

VR_case.ino (740 Bytes)

You should look at the StateChangeDetection example

edit... I had the wrong url in the link, sorry.... it's fixed now

Have attached the code

Hi, please edit your post and use the </> icon to put your code inside code tags.

Paul

done

have used variables to detect state change.. doesn't seem to be working

Muhammad_Ubaid:
have used variables to detect state change

No, it seems to me you are looking for the current state, not the fact that it changed from last time through loop().

In these applications it's important to see that a pin has become high (or low), not that it is high (or low).

Muhammad_Ubaid:
done

Thankyou!

Your state variables seem to be tracking the state of the leds, not the state of the buttons. Like Jimbo says, right now, your code cannot tell if a button has changed state since it was last checked.

PaulRB:
Like Jombo says

That's new.... I get Jumbo instead of Jimbo, but never Jombo before :slight_smile:

yes, please tell me how do i check if my button state has changed from last time, that is my requirement

Muhammad_Ubaid:
yes, please tell me how do i check if my button state has changed from last time, that is my requirement

That's EXACTLY what the state change detection example does. But I just noticed I pasted the wrong url into the link earlier, sorry about that... here's the correct link.

Thank you, will try out