Skip to content

Instantly share code, notes, and snippets.

@andreburto
Created November 23, 2016 20:12
Show Gist options
  • Save andreburto/c2e944645617c40aec9ae3776543d967 to your computer and use it in GitHub Desktop.
Save andreburto/c2e944645617c40aec9ae3776543d967 to your computer and use it in GitHub Desktop.
/**********************************************************************
* SANTA TRACKINATOR
* =================
* By: Andy Burton, 2016
*
* This code works in concert with an IFTTT.com applet that send the
* WIFI bit an update. When the update is recieved the LASERPEG pin is
* set high then low to blink lights. The mode of the system can also
* be changed to blink the LASERPEG pin every five seconds instead of
* waiting for an update.
**********************************************************************/
// Input pins
const int WIFIBIT_INPUT = 0;
const int BUTTON_INPUT = A0;
// Output pins
const int LASERPEG_OUT = 1;
const int LEDBIT_OUT = 5;
// Time count - 1000milli = 1sec
const unsigned long FULL_SEC = 1000;
const unsigned long HALF_SEC = 500;
const unsigned long ONE_TENTH_SEC = 100;
// Hold the last input state of the two input pins.
int WIFI_LAST_STATE;
int BUTTON_LAST_STATE;
// The system will have two modes: (1) Recieve input from the wifibit, (2) flash every five seconds.
const int MODE_ONE = 1;
const int MODE_TWO = 2;
// Holds the current mode.
int CURRENT_MODE;
// In mode 2 blink every time the counter matches
const unsigned long SECONDS_BETWEEN_BLINK = 5;
const unsigned long BLINK_TIMER_LENGTH = SECONDS_BETWEEN_BLINK * FULL_SEC;
unsigned long BLINK_NEXT;
unsigned long BLINK_LAST;
// Send digital output to a pin the given number of times, with a high and low pause for a given length of time.
void blinkOut(int pin, int times, unsigned long milliseconds) {
while (times > 0) {
// Send output, aka blink.
digitalWrite(pin, HIGH);
delay(milliseconds);
digitalWrite(pin, LOW);
delay(HALF_SEC);
// Decrement the counter to terminate the loop.
times--;
}
}
// This function will switch the system between modes. This means changing the value of CURRENT_MODE
// and blinking the LED a number that matches the mode.
void changeMode() {
// Change to mode 2
if (CURRENT_MODE == MODE_ONE) {
CURRENT_MODE = MODE_TWO;
}
// Change to mode 1
else {
CURRENT_MODE = MODE_ONE;
}
// Blink out the mode number
blinkOut(LEDBIT_OUT, CURRENT_MODE, HALF_SEC);
}
// Blinks the SANTA lights for every input from the wifi bit.
void santaTracker() {
// Only perform the tasks in this function if in MODE_ONE.
if (CURRENT_MODE != MODE_ONE) { return; }
// Turn the Santa light on for one second
blinkOut(LASERPEG_OUT, 1, FULL_SEC);
}
// In mode 2 blink every SECONDS_BETWEEN_BLINK seconds.
void modeTwoBlink() {
// Only perform if in MODE_TWO.
if (CURRENT_MODE != MODE_TWO) { return; }
// Only run millis() once.
unsigned long TIME_NOW = millis();
// If TIME_NOW is more than BLINK_NEXT then update state variables and blink.
if (TIME_NOW >= BLINK_NEXT) {
BLINK_LAST = BLINK_NEXT;
BLINK_NEXT = TIME_NOW + BLINK_TIMER_LENGTH;
blinkOut(LASERPEG_OUT, 1, FULL_SEC);
}
// Otherwise...
else {
// If TIME_NOW is less than BLINK_LAST then the millis() counter has run over.
// Update the state timers according and blink.
if (TIME_NOW < BLINK_LAST) {
BLINK_LAST = TIME_NOW;
BLINK_NEXT = TIME_NOW + BLINK_TIMER_LENGTH;
blinkOut(LASERPEG_OUT, 1, FULL_SEC);
}
}
}
// Checks digital input to ensure there is a pause between input.
// Changes value of last state variable accordingly.
// Executes void type function passed in.
void manageInput(const int* pin, int* lastState, void (*func)()) {
// Last state should be low, which indicates a break between inputs
if (*lastState == LOW) {
int inputValue = digitalRead(*pin);
// If there's input this time perform the passed function.
if (inputValue == HIGH) {
// Change the last state to high.
*lastState = HIGH;
// Execute the passed function.
func();
}
}
else {
// Reset the last state to low
*lastState = LOW;
}
}
void setup() {
// Set input pins on the left side of the Arduino bit
pinMode(WIFIBIT_INPUT, INPUT);
pinMode(BUTTON_INPUT, INPUT);
// Set output pins on the right side of the Arduino bit
pinMode(LASERPEG_OUT, OUTPUT);
pinMode(LEDBIT_OUT, OUTPUT);
// Set the last states to low since they've never been used
WIFI_LAST_STATE = LOW;
BUTTON_LAST_STATE = LOW;
// Set the current mode for wifi
CURRENT_MODE = MODE_ONE;
// Set the state checks for mode 2 blinking
BLINK_NEXT = millis() + BLINK_TIMER_LENGTH;
BLINK_LAST = 0;
}
void loop() {
// Manage input from the button first.
manageInput(&BUTTON_INPUT, &BUTTON_LAST_STATE, changeMode);
// Manage input from the wifi bit second.
manageInput(&WIFIBIT_INPUT, &WIFI_LAST_STATE, santaTracker);
// In mode 2 blink every few seconds.
modeTwoBlink();
// Delay 1/10th of a second.
delay(ONE_TENTH_SEC);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment