/*
State Change and Blink State 03
November 03-05, 2023
need to fix mini-blinks
*/
// circuit connections
const byte buttonPin = 2; // pushbutton to pin 2 with 10 kOhm resistor
const byte ledPin = 3; // LED to pin 3 with 220 ohm resistor
// set desired number of blinks and timing here
const int numBlinks = 3;
const long interval = 250;
// below needs no adjustment as it is derived from the two above
// note: numBlinks * 2 because each blink is on + off sequence
const long stopInterval = interval * (numBlinks * 2) + interval;
// prepare state management
int currentButtonState; // current state of button
int lastButtonState; // previous state of the button
int blinkState = LOW;
// prepare timer management
unsigned long startTime = 0;
unsigned long endTime = 0;
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
//Serial.begin(9600);
currentButtonState = digitalRead(buttonPin);
}
void loop() {
currentButtonState = digitalRead(buttonPin);
// toggle button action, blinkState, and begin timer
if (lastButtonState == HIGH && currentButtonState == LOW) {
startTime = millis();
endTime = startTime + stopInterval;
blinkState = !blinkState;
}
lastButtonState = currentButtonState;
if (blinkState == HIGH) {
// Serial.println("blinkState: HIGH");
blinkPattern();
}
if (blinkState == LOW) {
// Serial.println("blinkState: LOW");
ledOff();
}
}
void ledOff() {
digitalWrite(ledPin, LOW);
}
void ledOn() {
digitalWrite(ledPin, HIGH);
}
void blinkPattern() {
while (millis() - startTime > interval) {
// blink the LED (numBlinks) times
digitalWrite(ledPin, !digitalRead(ledPin));
startTime = millis();
// then stop blinking and do something else
if (startTime >= endTime) {
ledOn(); // if low, then needs fixing due to mini-blinks
}
}
}