
const int ledPin = 2;
const int buttonPin = 3;
unsigned long timer = 0;
bool onOff = LOW;
byte prevButtonState = LOW;
bool blinking = false;
void setup() {
// put your setup code here, to run once:
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
Serial.begin(9600);
}
void loop() {
// read the button pin
// store that in a local variable
byte buttonState = digitalRead(buttonPin);
// print out the state of the button stored in the variable
Serial.println(buttonState);
// check to see if the button is pressed and last time it wasn't
// only do something if that is the case
if (buttonState == HIGH && prevButtonState == LOW) {
// change blinking to not blinking
blinking = !blinking;
}
// record the current button state for use next time through loop
prevButtonState = buttonState;
// if blinkkng is true, do the blinking stuff
if (blinking == true) {
// check to see if the current time (millis) is greater than the timer we recorded
if (millis() > timer) {
// flip the boolean
onOff = !onOff;
// record a new time to check against
timer = millis() + 250;
// turn the led on and off
digitalWrite(ledPin, onOff);
}
/ otherwise turn the LED off
} else {
digitalWrite(ledPin, LOW);
}
}