// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin 13 as an output.
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(buttonPin, INPUT);
pinMode(buttonPinTwo, INPUT);
}
// the loop function runs over and over again forever
void loop() {
// ledButton A
while(digitalRead(buttonPin) == HIGH) { // Turns first LED on via button
digitalWrite(12, HIGH);
}
while(digitalRead(buttonPin) == LOW) { // LED off once button released
digitalWrite(12, LOW);
}
// ledButton B
// SHOULD turn on and off the second LED
// when code is comments out button 12 works fine
// when both codes are run the LEDs don't light up as they should
while(digitalRead(buttonPinTwo) == HIGH) {
digitalWrite(13, HIGH);
}
while(digitalRead(buttonPinTwo) == LOW) {
digitalWrite(13, LOW);
}
}
So in the code above i am controlling two LEDs via two buttons. Press button A and Led A should turn on, Press button B and Led B should come on. If i comment out either section A or section B in the code (so the code is only running a single LED) the system works fine. Press A and Led A works! Same goes for B! But when i have both uploaded together the system fails...
I still get the LEDs to turn on if i hold down one button and then click the other, but this isn't how the code is meant to work.
Does anyone have an idea on how i can fix this?
I don't believe its a hardware issue since i can comment out sections of code and make an individual section work.
I've got the 5V going into the outside positive strip, then each button has a jumper going from the positive strip into the top left of the button.
a 10K ohm resistor from the negative strip into the bottom left of the button with a jumper inbetween the resistor and the button going to the 2 & 3 ports (digital) of the arduino eleven.
Then ports 12 and 13 have jumpers going to the positive of the LEDs and a 470 ohm resistor to the negative strip.
I'm new to this stuff so sorry if my termonology isn't on point
And now on the single run its not working properly.. Not sure if i removed the whiles correctly or not.
From what i can tell from your diagram yes, but there are two of them wired off the 5v. But the way it is wired an individual circuit can be completed with just one button being pushed in (shouldnt need both to be pushed)
Also did you find that diagram or is there some sort of program for quickly whipping these schematics up?
You should know using while, loops that code as long as the condition is true.
This locks up your code execution and is far from ideal.
I may have used "while" once or twice over the years.
Look at the change in state example in the IDE.
Also: