const byte BUTTON = 2;
const byte LED = 10;
const int BLINK_TIME = 500;
const int BLINK_CYCLE = 5;
const int NO_BLINK = -1;
int prevButton = LOW;
int blink = NO_BLINK; // Current blink counter, -1 = blink disabled
bool ledState = false;
unsigned long tmrBlink = 0;
void setup() {
Serial.begin(9600);
pinMode(BUTTON, INPUT);
pinMode(LED, OUTPUT);
Serial.println("START");
}
void loop() {
// Check transition from LOW to HIGH (only if not blinking)
if (blink == NO_BLINK && prevButton == LOW && digitalRead(BUTTON) == HIGH) {
// Activate blink
blink = 0;
tmrBlink = millis();
// A bit of debounce, if no hardware...
delay(60);
prevButton = HIGH;
Serial.println("BUTTON");
}
if (blink > -1 ) {
if (millis() - tmrBlink > BLINK_TIME) {
Serial.print("Timer! ");
blinkLed();
if (!ledState) {
++blink;
tmrBlink = millis();
// End?
if (blink == BLINK_CYCLE) blink = NO_BLINK;
}
}
}
}
void blinkLed() {
ledState = !ledState;
digitalWrite(LED, ledState);
Serial.print("B");Serial.print(blink);Serial.print("(");
Serial.print(ledState);Serial.print(") ");
Serial.println();
}