int btnpin = 3;
int ledpin = 10; //sets the constant ledpin to 13
boolean lastButton = LOW; //bool can be high or low, and here I set it to start low.
boolean currentButton = LOW; //keeps track of current button state
boolean ledon = false; //VARIABLE TO let us track current state of the led. starts with 'false'
void setup() {
pinMode(btnpin, INPUT_PULLUP); //'pulls' the power of the button up to 5 v ('high') reading when it is not pressed.
pinMode(ledpin, OUTPUT);
Serial.begin(9600);
}
void loop() {
currentButton = debounce(lastButton);
int sensor = digitalRead(btnpin);
if (lastButton == HIGH && currentButton == LOW) {
ledon = !ledon;
}
lastButton = currentButton;
digitalWrite(ledpin, ledon);
}
boolean debounce (boolean last) { //debounce function
boolean current = digitalRead(btnpin);
if (last != current)
{
delay (5);
current = digitalRead(btnpin);
}
return (current);
}