Ok, so i'm about to start a project and need to learn how to use buttons properly. I have 4 buttons connected to a Arduino Nano, connected to D2, D3, D4 and D5. They are supposed to turn on the built in LED and write the status to the terminal.
All four buttons writes their status to the terminal but only the last one turns on the LED.
I have tried swapping the last two if statements, and it is always the button in the last if statement that works.
What am i missing? I'm guessing it is code related.
void setup() {
Serial.begin(9600);
//configure pin 2,3,4 and 5 as an input and enable the internal pull-up resistor
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
// built in LED
pinMode(13, OUTPUT);
}
void loop() {
//read the pushbutton value into a variable
int sensorVal2 = digitalRead(2);
int sensorVal3 = digitalRead(3);
int sensorVal4 = digitalRead(4);
int sensorVal5 = digitalRead(5);
//print out the value of the pushbutton
Serial.print("Button state: ");
Serial.print(sensorVal2);
Serial.print(sensorVal3);
Serial.print(sensorVal4);
Serial.print(sensorVal5);
Serial.print('\n');
// Keep in mind the pull-up means the pushbutton's logic is inverted. It goes
// HIGH when it's open, and LOW when it's pressed. Turn on pin 13 when the
// button's pressed, and off when it's not:
if (sensorVal2 == HIGH) {
digitalWrite(13, LOW);
} else {
digitalWrite(13, HIGH);
}
if (sensorVal3 == HIGH) {
digitalWrite(13, LOW);
} else {
digitalWrite(13, HIGH);
}
if (sensorVal4 == HIGH) {
digitalWrite(13, LOW);
} else {
digitalWrite(13, HIGH);
}
if (sensorVal5 == HIGH) {
digitalWrite(13, LOW);
} else {
digitalWrite(13, HIGH);
}
delay(200);
}
in your loop(), the signal is sent to the LED (pin 13 if I am right).
but you do your "if statments" in a consecutive way so only the last result is kept and tansfered to the led. That is the behavior you describe in your post (only the last line is effective).
Actually, your Led only displays the very last button read.
Do you have spared Leds? I propose you to connect each button to one unique led and test your code again (it should work for what I see).
Remember that the processor runs through your code at an unbelievable speed.
So pressing the second button is indeed illuminating the LED, but a dozen microseconds later it is extinguished.
If you had an oscilloscope or logic analyzer, you could see this. You may go far enough in this hobby to want one (logic analyzer), you may actually go far enough to need one.
You will know when.
With you posted code prsss down all four buttons…
You can tell what code will do by dragging your finger through it slavishly performing the lines one by one. That would show you what is happening, but you have to remember I repeat, these machines move through the code extremely rapidly.
Wow, what a fantastic community this is. Thank you for all your answers. I'll have to get back to you with a proper answer later when i have looked through everything and the kid is sleeping.
@paulpaulson thanks, I was not aware of the magic number problem. If I understand it correctly, a more proper way to write digitalWrite(13, HIGH); would be to put #define PINNUMBER 13 , or something similar in the beginning of the code, and then write digitalWrite(PINNUMBER, HIGH in the loop?
Also, did you want me to post my code in that other thread that you linked to?
@GrandPete It worked! I had some LEDs and resistors and managed to get it to work. I also tried out @dlloyd 's solution. Thanks for the link, that site will be very helpful.
@Hutkikz the delay did the trick! I guess the LED is not as fast as the serial monitor.
@anon57585045 thank you for the idea. That looks way better than my code.
thanks for your feedback. Point is to help members in a constructive way for their sketch and their future coding.
I would try to help if I can but I also learn from others members' answer or debate!