user52933: i want to turn on an led with two buttons. If either of the buttons is pressed i want the led to turn on while the button is pressed and stay off if nothing is pressed. i have two sets of code, they both do the same thing, but the second set of code causes the led to stay in the on position even when nothing is being pushed and i dont understand why.. i am using pulldown resistors on both the buttons.
This is the first set of code that works properly:
class Button {

  public:

    int pin;
    int state;
    int location;
    int Speed;
    int Direction;
    int active;
    int prevstate;

    Button(int pin1, int state1, int location1, int active1, int previousState)
    {
      pin = pin1;
      state = state1;
      location = location1;
      active = active1;
      prevstate = previousState;
    }
};

Button button(7,0,0,0,0);
Button button2(8,0,0,0,0);
Button buttons[2] = {button, button2};

void setup() {
      pinMode(ledPin, OUTPUT); 
}

void loop() {
   if (digitalRead(button2.pin) == HIGH){
             digitalWrite(ledPin, HIGH);
          } else {
          // turn LED off:
          digitalWrite(ledPin, LOW);
          }
       if (digitalRead(button.pin) == HIGH){
             digitalWrite(ledPin, HIGH);
          } else {
          // turn LED off:
          digitalWrite(ledPin, LOW);
          }
}

this is the code i want to use but it is not working.. the led stays in the on position:
void loop(){
  for (int i = 0; i<sizeof(buttons); i++){
    if(digitalRead(buttons[i].pin) == HIGH){
          digitalWrite(ledPin, HIGH);
          } else {
          // turn LED off:
          digitalWrite(ledPin, LOW);
          }
    }
}

also if i change 
if(digitalRead(buttons[i].pin) == HIGH)

to 
if(digitalRead(buttons[0].pin) == HIGH)

the code works for the first or second button alone in the array.. but not both???

user313: You seem to have made 4 button objects: button, button2, and buttons[2]. buttons[0] and buttons[1] are full button objects whose first member, .pin, is initialized to the addresses of button and button2, respectively. This is probably not what you intended.
Did you mean to create:
Button buttons[2] = {
   {7,0,0,0,0},
   {8,0,0,0,0}
};

, an array of two initialized button objects?

52933: <p>i want to turn on an led with two buttons. If either of the buttons is pressed i want the led to turn on while the button is pressed and stay off if nothing is pressed. i have two sets of code, they both do the same thing, but the second set of code causes the led to stay in the on position even when nothing is being pushed and i dont understand why.. i am using pulldown resistors on both the buttons.</p>

<p>This is the first set of code that works properly:</p>

<pre><code>class Button {

  public:

    int pin;
    int state;
    int location;
    int Speed;
    int Direction;
    int active;
    int prevstate;

    Button(int pin1, int state1, int location1, int active1, int previousState)
    {
      pin = pin1;
      state = state1;
      location = location1;
      active = active1;
      prevstate = previousState;
    }
};

Button button(7,0,0,0,0);
Button button2(8,0,0,0,0);
Button buttons[2] = {button, button2};

void setup() {
      pinMode(ledPin, OUTPUT); 
}

void loop() {
   if (digitalRead(button2.pin) == HIGH){
             digitalWrite(ledPin, HIGH);
          } else {
          // turn LED off:
          digitalWrite(ledPin, LOW);
          }
       if (digitalRead(button.pin) == HIGH){
             digitalWrite(ledPin, HIGH);
          } else {
          // turn LED off:
          digitalWrite(ledPin, LOW);
          }
}
</code></pre>

<p>this is the code i want to use but it is not working.. the led stays in the on position:</p>

<pre><code>void loop(){
  for (int i = 0; i&lt;sizeof(buttons); i++){
    if(digitalRead(buttons[i].pin) == HIGH){
          digitalWrite(ledPin, HIGH);
          } else {
          // turn LED off:
          digitalWrite(ledPin, LOW);
          }
    }
}
</code></pre>

<p>also if i change </p>

<pre><code>if(digitalRead(buttons[i].pin) == HIGH)
</code></pre>

<p>to </p>

<pre><code>if(digitalRead(buttons[0].pin) == HIGH)
</code></pre>

<p>the code works for the first or second button alone in the array.. but not both???</p>
 313: <p>You seem to have made 4 button objects: button, button2, and buttons[2]. buttons[0] and buttons[1] are full button objects whose first member, .pin, is initialized to the addresses of <code>button</code> and <code>button2</code>, respectively. This is probably not what you intended.</p>

<p>Did you mean to create:</p>

<pre><code>Button buttons[2] = {
   {7,0,0,0,0},
   {8,0,0,0,0}
};
</code></pre>

<p>, an array of two initialized button objects?</p>
