// stab of light alto777
// ----- symbolic indices -----
enum {
START,
STOP,
ERROR,
};
const unsigned long STAB = 1000; // flash of LED duration in milliseconds
const unsigned char ledPins[] = { 8, 9, 10 }; // LED pins (parallel array)
const unsigned char buttonPins[] = { 2, 3, 4 }; // button pins (one per LED)
unsigned long flashStart[] = { 0, 0, 0 }; // timing array (parallel)
const unsigned char ledCount = sizeof ledPins / sizeof *ledPins;
void setup()
{
setupFlash();
for (unsigned char i = 0; i < ledCount; i++)
pinMode(buttonPins[i], INPUT_PULLUP);
}
void loop() {
serviceFlash(); // finish things we started elsewhere
if (digitalRead(buttonPins[START]) == LOW) flash(START);
if (digitalRead(buttonPins[STOP]) == LOW) flash(STOP);
if (digitalRead(buttonPins[ERROR]) == LOW) flash(ERROR);
}
// THE FOLD
// ----- flash functions -----
void setupFlash(void)
{
for (unsigned char i = 0; i < ledCount; i++) {
pinMode(ledPins[i], OUTPUT);
digitalWrite(ledPins[i], LOW);
}
}
void flash(int index)
{
if (index < 0 || index >= ledCount) return; // ? out of range, ignore
digitalWrite(ledPins[index], HIGH);
flashStart[index] = millis();
}
void serviceFlash(void)
{
unsigned long now = millis();
for (unsigned char ii = 0; ii < ledCount; ii++) {
if (now - flashStart[ii] >= STAB) {
digitalWrite(ledPins[ii], LOW);
flashStart[ii] = 0;
}
}
}
start
stop
error