Why is my voltage less when using this code

When i press button 2 for led1, the voltage at pin 8 (led) is only 2.0v. BUT when i press the other two buttons for led2 or led3, the voltage at pin 8 is 5V. Dont get it. I have 10k pull down resistors on all input pins.

It seems that the more i try to learn this code the dumber i get. I someone can help me I would love it. I am not sure how you respond, but my email is jsinger@jas-technologies.com

Thanks


const int led = 8;
const int led1 = 2;
const int led2 = 3;
const int led3 = 4;

void setup()
{
pinMode (led, OUTPUT);

pinMode (led1, INPUT);
pinMode (led2, INPUT);
pinMode (led3, INPUT);

}

void loop()
{
for (int i=2;i<5;i++){
if (digitalRead (i) == HIGH) {
digitalWrite(led, HIGH);
} else if (digitalRead(led2) == HIGH) {
digitalWrite(led, HIGH);
} else if (digitalRead(led3) == HIGH) {
digitalWrite(led, HIGH);

} else {
digitalWrite(led, LOW);

}
}
}

You'll have to post your electrical schematic. Code seems fine.

When i press button 2 for led1, the voltage at pin 8 (led) is only 2.0v.

If you had a Scope, you would see the output toggling. This is because when i=2, and pin 2 being HIGH makes Output LED go HIGH, but when i=3or4 the switches are LOW then the else{} makes Output LED go LOW.

I have 10k pull down resistors on all input pins.

You will need to turn on the internal pull-up resistors.

pinMode (led1, INPUT_PULLUP);
pinMode (led2, INPUT_PULLUP);
pinMode (led3, INPUT_PULLUP);

Wouldn’t it be better to call a switch SW1 SW2 SW3 rather than led1, led2, led3 ?

What exactly are you trying to do here? :o

  for (int i = 2; i < 5; i++)
  {
    if (digitalRead (i) == HIGH)
    {
      digitalWrite(led, HIGH);
    }
    else if (digitalRead(led2) == HIGH)
    {
      digitalWrite(led, HIGH);
    }
    else if (digitalRead(led3) == HIGH)
    {
      digitalWrite(led, HIGH);
    }

    else
    {
      digitalWrite(led, LOW);
    }
  }

Why not:

    if (digitalRead (led1) == HIGH)
    {
      digitalWrite(led, HIGH);
    }
    
    else if (digitalRead(led2) == HIGH)
    {
      digitalWrite(led, HIGH);
    }
    
    else if (digitalRead(led3) == HIGH)
    {
      digitalWrite(led, HIGH);
    }

    else
    {
      digitalWrite(led, LOW);
    }

Why not

Because it is stupid using led1, led2, etc. as the names of pins that have switches connected to them.

Post #3 says “Wouldn't it be better to call a switch SW1 SW2 SW3 rather than led1, led2, led3 ? ”

Cannot change too many things at one time as OPs often disappear never to be seen again.