Skip to content

Instantly share code, notes, and snippets.

@pjf
Forked from anonymous/timer
Last active August 21, 2016 14:29
Show Gist options
  • Save pjf/f959ac51951708474f66f5bb2094bb93 to your computer and use it in GitHub Desktop.
Save pjf/f959ac51951708474f66f5bb2094bb93 to your computer and use it in GitHub Desktop.
// Pin-outs
int panel_1 = 10;
int panel_2 = 12;
int panel_3 = 8;
int panel_4 = 9;
int panel_5 = 6;
int panel_6 = 7;
int bottomLight = 11;
int topLight = 5;
int backLight = 4;
int coinInput = 2;
int resetPin = 3;
void setup() {
// the setup routine runs once when you press reset:
digitalWrite(resetPin, HIGH);
delay(200);
pinMode(resetPin, OUTPUT);
delay(200);
//The reset function can only be triggered when the machine has completed its run and everything is back to its station
//this means setting up a boolean machineRunning = TRUE
//so the reset can only be triggered at machineRunning = !TRUE
//the coin mechanism will send a reset signal
pinMode(coinInput, INPUT_PULLUP);
pinMode(panel_1, OUTPUT);
pinMode(panel_2, OUTPUT);
pinMode(panel_3, OUTPUT);
pinMode(panel_4, OUTPUT);
pinMode(panel_5, OUTPUT);
pinMode(panel_6, OUTPUT);
pinMode(bottomLight, OUTPUT);
pinMode(topLight, OUTPUT);
pinMode(backLight, OUTPUT);
}
void timedEvent(unsigned long epochStart, int timeStart, int timeFinish, int pin, int mode) {
unsigned long currentMillis = millis() - epochStart;
int seconds = currentMillis / 1000;
if (seconds > timeStart && seconds < timeFinish) {
digitalWrite(pin, mode);
}
}
void runShow() {
// How many seconds does our show run for?
unsigned long const showLength = 40;
// Record the current time. All events will happen
// relative to this time.
unsigned long startTime = millis();
// Run through our events until we know our show is done.
while (millis() < (startTime + showLength * 1000)) {
timedEvent(startTime, 1, 5, panel_1, HIGH);
timedEvent(startTime, 5, 8, panel_1, LOW);
timedEvent(startTime, 7, 10, panel_2, HIGH);
timedEvent(startTime, 10, 13, panel_2, LOW);
timedEvent(startTime, 13, 15, panel_3, HIGH);
timedEvent(startTime, 15, 18, panel_3, LOW);
timedEvent(startTime, 15, 20, panel_4, HIGH);
timedEvent(startTime, 20, 25, panel_4, LOW);
timedEvent(startTime, 24, 30, panel_5, HIGH);
timedEvent(startTime, 30, 35, panel_5, LOW);
timedEvent(startTime, 34, 38, panel_6, HIGH);
timedEvent(startTime, 38, 40, panel_6, LOW);
}
}
void loop() {
// Watch to see if a coin has been inserted.
int sensorVal = digitalRead(coinInput);
// If it has, then run our show!
if (sensorVal == HIGH) {
runShow(); // Runs until show completed, then returns.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment