I'm trying to follow lesson 6 of the Adafruit Arduino lessons on digital inputs found here.
I have the same code and the board setup the exact same and the code it is working exactly as it should. I just can't figure out WHY.
Board:
Code:
/*
Adafruit Arduino - Lesson 6. Inputs
*/
int ledPin = 5;
int buttonApin = 9;
int buttonBpin = 8;
byte leds = 0;
void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(buttonApin, INPUT_PULLUP);
pinMode(buttonBpin, INPUT_PULLUP);
}
void loop()
{
if (digitalRead(buttonApin) == LOW)
{
digitalWrite(ledPin, HIGH);
}
if (digitalRead(buttonBpin) == LOW)
{
digitalWrite(ledPin, LOW);
}
}
Why does the light turn on when one of he buttons is pressed once and turn off when the other button is pressed once. I don't understand the mechanism of how the press is being remembered.
There doesn't seem to be anything in the code that can explain why the button stays on even when no longer pressed, so it should be some component? But what?
Can anyone help explain why this is working?
if the button is pressed and the light turns on, then the light must turn off if the button is released
... which part of the code tells the light to turn off when buttonApin is released?digitalWrite
maintains a state, kind of like a variable.