Created
October 28, 2019 15:00
-
-
Save wastrachan/65cbd9920125fd69235654ed4b0c2646 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Switchboard LED Flasher | |
* Created for Teensy 3.2 | |
* Copyright (c) Winston Astrachan 2019 | |
* | |
* Emulates a telephone switchboard with 8 lights, blinking them | |
* to simulate incoming calls at psuedo-random intervals. | |
*/ | |
const int LAMP1 = 9; | |
const int LAMP2 = 10; | |
const int LAMP3 = 11; | |
const int LAMP4 = 12; | |
const int LAMP5 = 13; | |
const int LAMP6 = 14; | |
const int LAMP7 = 15; | |
const int LAMP8 = 16; | |
// State machine for changing LED state | |
// Every time update() is called checks last change time and | |
// last state. Changes state if time has elapsed | |
class LampBlink { | |
int led; | |
int numBlinks; | |
int lastBlink; | |
int cycleDelay; | |
bool ledState; | |
long onDuration; | |
long offDuration; | |
long lastUpdateTime; | |
public: | |
LampBlink(int pled, int pnumBlinks, long ponDuration, long poffDuration, long pcycleDelay) { | |
led = pled; | |
numBlinks = pnumBlinks; | |
onDuration = ponDuration; | |
offDuration = poffDuration; | |
cycleDelay = pcycleDelay; | |
ledState = 0; | |
lastUpdateTime = 0; | |
lastBlink = 0; | |
pinMode(led, OUTPUT); | |
} | |
void update() { | |
// Update state of the LED if enough time has passed | |
long currentTime = millis(); | |
if((ledState == 0) && (lastBlink == numBlinks) && (currentTime - lastUpdateTime < cycleDelay)) { | |
// Waiting out cycle delay, do not update state | |
} | |
else if((ledState == 0) && (lastBlink == numBlinks) && (currentTime - lastUpdateTime >= cycleDelay)) { | |
// End of cycle delay reached, reset blink counter and turn LED on | |
lastBlink = 0; | |
ledState = 1; | |
lastUpdateTime = currentTime; | |
digitalWrite(led, ledState); | |
} | |
else if((ledState == 1) && (currentTime - lastUpdateTime >= onDuration)) { | |
// Turn LED off it has been on long enough | |
ledState = 0; | |
lastUpdateTime = currentTime; | |
digitalWrite(led, ledState); | |
lastBlink++; | |
} | |
else if((ledState == 0) && (currentTime - lastUpdateTime >= offDuration)) { | |
// Turn LED on if it has been off long enough | |
ledState = 1; | |
lastUpdateTime = currentTime; | |
digitalWrite(led, ledState); | |
} | |
} | |
}; | |
LampBlink lamp1(LAMP1, 4, 500, 200, 3000); | |
LampBlink lamp2(LAMP2, 4, 500, 200, 8000); | |
LampBlink lamp3(LAMP3, 4, 500, 200, 12000); | |
LampBlink lamp4(LAMP4, 4, 500, 200, 6000); | |
LampBlink lamp5(LAMP5, 4, 500, 200, 10000); | |
LampBlink lamp6(LAMP6, 4, 500, 200, 5000); | |
LampBlink lamp7(LAMP7, 4, 500, 200, 14000); | |
LampBlink lamp8(LAMP8, 4, 500, 200, 9000); | |
void setup() {} | |
void loop() { | |
lamp1.update(); | |
lamp2.update(); | |
lamp3.update(); | |
lamp4.update(); | |
lamp5.update(); | |
lamp6.update(); | |
lamp7.update(); | |
lamp8.update(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment