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;
}
}