twoButtons_forTwoLeds

Hi Everyone,

I hope all is well. I have a code issue and would like to see if anyone could shed some light! I am trying to use two push buttons to turn on two separate LEDs. My circuit seems to be working just fine. I am very new to programming so I am almost positive my issue lies within the code. Here is what I have. Please Help!!! Code will Verify however I can not achieve my goal.

const int buttonPin={9}; //Button 1 assigned pin 9
const int buttonTwoPin={8}; //Button 2 assigned pin 8
const int ledPin = 10; // Green LED assigned pin 10
const int ledTwoPin = 11; // Red LED assigned pin 11

int buttonState= 0; // variable for reading pushbuttons

void setup() {
pinMode(ledPin, OUTPUT); // Green LED is OUTPUT
pinMode(ledTwoPin, OUTPUT); // Red LED is OUTPUT
for(int x=0; x<2; x++) {
pinMode(buttonPin, INPUT); //Button 1 is a INPUT
pinMode(buttonTwoPin, INPUT); //Button 2 is a INPUT
}

}

void loop() {
for(int x=0; x<2; x++) {
buttonState= digitalRead(buttonPin);
buttonState= digitalRead(buttonTwoPin);
if (buttonState== HIGH &&buttonPin==2) { //Check to see if Button 1 is pressed
digitalWrite(ledPin, HIGH); // If so Turn ON Green LED
}
if (buttonState== HIGH &&buttonTwoPin==2) { //Check to see if Button 2 is pressed
digitalWrite(ledTwoPin, HIGH); // If so Turn ON Red LED
}
if (buttonState== HIGH &&buttonPin==3) { // Check to see if Button 1 is pressed again
digitalWrite(ledPin, LOW); // If so Turn OFF Green LED
}
if (buttonState== HIGH &&buttonTwoPin==3) { // Check to see if Button 2 is pressed again
digitalWrite(ledTwoPin, LOW); // If so Turn OFF Red LED
}
}

}

How to use this forum

Hi

Can you please explain what happens when you try to press one of your buttons.

Daniel

Nothing at all happens when I press a button.

Right, first things first.

Go and read the instructions, then go back and modify your post (use the "More --> Modify" option to the bottom right of the post) to mark up the code as such so we can examine it conveniently and accurately.

If you do not do this, your code may well be meaningless.

Note: Also mark up any data in the same way. This includes error output that you get from the IDE.