Hi, this is my first project with Arduino and I need some help. I am trying to toggle a led with a button, but it actually just stays on while I hold it down. Could somebody please help me? Thanks in advance.
const int buttonPin = 8;
const int stopButtonPin = 7;
const int ledB = 12; // BLUE led pin
const int ledR = 11; // RED led pin
int button = 0;
int stop = 0;
void setup()
{
//setting the pin modes
pinMode(ledB, OUTPUT);
pinMode(ledR, OUTPUT);
pinMode(buttonPin, INPUT);
pinMode(stopButtonPin, INPUT);
}
void loop()
{
button = digitalRead(buttonPin); //read button state
stop = digitalRead(stopButtonPin); //read stop state
if (button == HIGH) //if button is pressed, light BLUE led and turn off the other
{
digitalWrite(ledB, HIGH);
digitalWrite(ledR, LOW);
}
else if (button == LOW) //if button is released, light RED led and turn off the other
{
digitalWrite(ledB, LOW);
digitalWrite(ledR, HIGH);
}
if (stop == HIGH) //if stop is pressed, turn off both leds
{
digitalWrite(ledB, LOW);
digitalWrite(ledR, LOW);
}
}