Built in LED only lights for one button

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 general, you should not use magic numbers. The I/O pins love to have a functional name.

Have a nice day and enjoy coding in C++.

Hi!

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).

@GrandPete is right.
Since all results are on the same LED with no kind of delay between them the code moves so fast you only see the last one.

Put delays between each if/else.

P.S. then learn how to do it without delay()

Allow me to screenshot that :grin:

Hello mikaelhollsten

To get started run this example to found in the IDE:

This example using unlovely the delay() function for debouncing.

You could also check all of them together like

  if (sensorVal2 == HIGH or sensorVal3 == HIGH or sensorVal4 == HIGH or sensorVal5 == HIGH) {

What you are missing has been pointed out.

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.

a7

Please publish the sketch here.

@dlloyd nice trick with the button status LEFs there.

a7

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.

Thanks!

@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.

1 Like

Almost. It is an improvement but you can do even one thing better also. Convey the name of whatever thing is attached to the pin, like

const int SOLENOID = 13;
...
digitalWrite(SOLENOID, HIGH);

Also using a const declaration is safer than using #defines.

Hello mikaelhollsten

Many thanks for your reply.

Take a view here to gain the knowledge:

https://www.learncpp.com/general-programming/six-language-independent-ways-to-write-better-code/

Have a nice day and enjoy coding in C++.

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! :grinning:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.