recently I was trying to get my setup for a LED and button to work on toggle, like a light switch in a house. A user provided me the code that worked for me but I was wondering if someone could help explain the code so i can understand how it works instead of just making my LED and button work.
The code is
// recognize multiple button presses; tgl LED when pressed
byte butPin = A1;
byte ledPin = 10;
byte butLst;
// -----------------------------------------------------------------------------
void setup (void)
{
digitalWrite (ledPin, HIGH);
pinMode (ledPin, OUTPUT);
pinMode (butPin, INPUT_PULLUP);
butLst = digitalRead (butPin);
}
// -----------------------------------------------------------------------------
void loop (void)
{
byte but = digitalRead (butPin);
if (butLst != but) {
butLst = but;
if (LOW == but) // button pressed
digitalWrite (ledPin, ! digitalRead (ledPin));
}
delay (10); // debounce
}
/code]