user29887: I'm doing a project using Arduino Uno R3 and I'm trying to use two buttons to change LED state between HIGH and LOW. I have already a code but someting its not right because the LED should be starting in LOW state but it's starting in HIGH state. I checked the circuit and its all good none of the components is connected in wrong place or not connected corretly and the resistors I used are the right ones.
Here is the code I'm using:
 int ledpin = 13;
 int pushbutton1 = 8;
 int pushbutton2 = 7;
 boolean lastbuttonstate1 = LOW;
 boolean lastbuttonstate2 = LOW;

void setup() 
{ 
  pinMode(ledpin,OUTPUT);
  pinMode(pushbutton1,INPUT);
  pinMode(pushbutton2,INPUT);
}

void loop() {


static bool ledState = false; // LED is off by default
 if ((digitalRead(pushbutton1) == LOW) && !ledState) { // 1 pressed & led is off
    digitalWrite(ledpin, HIGH);
    ledState = true;
 }
  if ((digitalRead(pushbutton2) == LOW) && ledState) { // 2 pressed & led is on
    digitalWrite(ledpin, LOW);
    ledState = false;
  }
}


user15180: You need some PULLUP there - either external resistors or INPUT_PULLUP settings, otherwise disconnected pin have not defined valou (so it can be LOW even if button is not pressed.

user32480: I think you have just minor changes. First of all, you haven't applied pullup. And here, what is the use of boolean lastbuttonstate1 = LOW; and boolean lastbuttonstate2 = LOW;? I think it not useful for the code so I remove it. And always initialize flag or something in starting of the code means globally. I update your code, just check it out. It might be helpful.
int ledpin = 13;
int pushbutton1 = 8;
int pushbutton2 = 7;

static bool ledState = false; // LED is off by default

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

  pinMode(pushbutton1,INPUT_PULLUP); // Provide pullup
  pinMode(pushbutton2,INPUT_PULLUP); // Provide pullup
}

void loop()
{
  if((digitalRead(pushbutton1) == LOW) && !ledState) // 1 pressed & led is off
  { 
    digitalWrite(ledpin, HIGH);
    ledState = true;
  }
  if((digitalRead(pushbutton2) == LOW) && ledState) // 2 pressed & led is on
  {         
    digitalWrite(ledpin, LOW);
    ledState = false;
  }
}


user29899: This is an excellent tutorial for exactly that:
http://www.instructables.com/id/How-to-use-a-Push-Button-Arduino-Tutorial/
Its a bit hard to give you a specific answer without seeing you circuit. 
but as a start, i would set the ledPin to LOW in the setup routine. 
void setup() 
{ 
  pinMode(ledpin,OUTPUT);
  pinMode(pushbutton1,INPUT);
  pinMode(pushbutton2,INPUT);

  //initialize the pin to low
  digitalWrite(ledpin,LOW);

}


29887: <p>I'm doing a project using <strong>Arduino Uno R3</strong> and I'm trying to use two buttons to change <strong>LED</strong> state between <strong>HIGH</strong> and <strong>LOW</strong>. I have already a code but someting its not right because the <strong>LED</strong> should be starting in <strong>LOW</strong> state but it's starting in <strong>HIGH</strong> state. I checked the circuit and its all good none of the components is connected in wrong place or not connected corretly and the resistors I used are the right ones.</p>

<p>Here is the code I'm using:</p>

<pre><code> int ledpin = 13;
 int pushbutton1 = 8;
 int pushbutton2 = 7;
 boolean lastbuttonstate1 = LOW;
 boolean lastbuttonstate2 = LOW;

void setup() 
{ 
  pinMode(ledpin,OUTPUT);
  pinMode(pushbutton1,INPUT);
  pinMode(pushbutton2,INPUT);
}

void loop() {


static bool ledState = false; // LED is off by default
 if ((digitalRead(pushbutton1) == LOW) &amp;&amp; !ledState) { // 1 pressed &amp; led is off
    digitalWrite(ledpin, HIGH);
    ledState = true;
 }
  if ((digitalRead(pushbutton2) == LOW) &amp;&amp; ledState) { // 2 pressed &amp; led is on
    digitalWrite(ledpin, LOW);
    ledState = false;
  }
}
</code></pre>
 15180: <p>You need some PULLUP there - either external resistors or INPUT_PULLUP settings, otherwise disconnected pin have not defined valou (so it can be LOW even if button is not pressed.</p>
 32480: <p>I think you have just minor changes. First of all, you haven't applied pullup. And here, what is the use of <code>boolean lastbuttonstate1 = LOW;</code> and <code>boolean lastbuttonstate2 = LOW;</code>? I think it not useful for the code so I remove it. And always initialize flag or something in starting of the code means globally. I update your code, just check it out. It might be helpful.</p>

<pre><code>int ledpin = 13;
int pushbutton1 = 8;
int pushbutton2 = 7;

static bool ledState = false; // LED is off by default

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

  pinMode(pushbutton1,INPUT_PULLUP); // Provide pullup
  pinMode(pushbutton2,INPUT_PULLUP); // Provide pullup
}

void loop()
{
  if((digitalRead(pushbutton1) == LOW) &amp;&amp; !ledState) // 1 pressed &amp; led is off
  { 
    digitalWrite(ledpin, HIGH);
    ledState = true;
  }
  if((digitalRead(pushbutton2) == LOW) &amp;&amp; ledState) // 2 pressed &amp; led is on
  {         
    digitalWrite(ledpin, LOW);
    ledState = false;
  }
}
</code></pre>
 29899: <p>This is an excellent tutorial for exactly that:
<a href="http://www.instructables.com/id/How-to-use-a-Push-Button-Arduino-Tutorial/" rel="nofollow noreferrer">http://www.instructables.com/id/How-to-use-a-Push-Button-Arduino-Tutorial/</a></p>

<p>Its a bit hard to give you a specific answer without seeing you circuit. 
but as a start, i would set the ledPin to LOW in the setup routine. </p>

<pre><code>void setup() 
{ 
  pinMode(ledpin,OUTPUT);
  pinMode(pushbutton1,INPUT);
  pinMode(pushbutton2,INPUT);

  //initialize the pin to low
  digitalWrite(ledpin,LOW);

}
</code></pre>
