Skip to content

Instantly share code, notes, and snippets.

@Sigafoos
Created February 20, 2017 23:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sigafoos/da9ffdd7af688e2949a9cbc44051e3e0 to your computer and use it in GitHub Desktop.
Save Sigafoos/da9ffdd7af688e2949a9cbc44051e3e0 to your computer and use it in GitHub Desktop.
Use an Arduino Nano with two LEDs, red for "stay in bed" and green for "it's okay to get up". Set the time via serial before unplugging: make sure you have an alternate power source. I soldered a 9v connector to the VIN for that.
#include <Time.h>
#include <TimeLib.h>
#include <TimeAlarms.h>
int greenLED = 5;
int redLED = 7;
#define TIME_MSG_LEN 11 // time sync to PC is HEADER followed by Unix time_t as ten ASCII digits
#define TIME_HEADER 'T' // Header tag for serial time sync message
#define HMS_MSG_LEN 6 // time sync to PC is HEADER followed by HHMMSS as digits
#define HMS_HEADER 'H' // Header tag for serial time sync message
void setup() {
pinMode(greenLED, OUTPUT);
pinMode(redLED, OUTPUT);
Serial.begin(9600);
Serial.println("waiting for time sync...");
while (timeStatus() == timeNotSet) {
if (Serial.available()) {
processSyncMessage();
}
}
Serial.println("Time received. To the main event!");
Alarm.alarmRepeat(00, 00, 00, turnRed);
Alarm.alarmRepeat(6, 0, 0, turnGreen);
Alarm.alarmRepeat(8, 0, 0, allOff);
}
void loop() {
Serial.print(hour());
Serial.print(":");
Serial.print(minute());
Serial.print(":");
Serial.print(second());
Serial.println();
Alarm.delay(1000);
}
void turnGreen() {
Serial.println("Turning on green LED");
digitalWrite(redLED, LOW);
digitalWrite(greenLED, HIGH);
}
void turnRed() {
Serial.println("Turning on red LED");
digitalWrite(redLED, HIGH);
digitalWrite(greenLED, LOW);
}
void allOff() {
Serial.println("Turning off both LEDs");
digitalWrite(redLED, LOW);
digitalWrite(greenLED, LOW);
}
void processSyncMessage() {
// if time sync available from serial port, update time and return true
while (Serial.available() >= TIME_MSG_LEN || Serial.available() >= HMS_MSG_LEN ) { // time message consists of header & 10 ASCII digits
char c = Serial.read() ;
Serial.print(c);
if ( c == TIME_HEADER ) {
time_t pctime = 0;
for (int i = 0; i < TIME_MSG_LEN - 1; i++) {
c = Serial.read();
if ( c >= '0' && c <= '9') {
pctime = (10 * pctime) + (c - '0') ; // convert digits to a number
}
}
setTime(pctime); // Sync Arduino clock to the time received on the serial port
} else if (c == HMS_HEADER) {
int hr = 0;
int mn = 0;
int sc = 0;
for (int i = 0; i < HMS_MSG_LEN; i++) {
c = Serial.read();
switch (i) {
case 0:
hr = (c - '0') * 10;
break;
case 1:
hr = hr + (c - '0');
break;
case 2:
mn = (c - '0') * 10;
break;
case 3:
mn = mn + (c - '0');
break;
case 4:
sc = (c - '0') * 10;
break;
case 5:
sc = sc + (c - '0');
break;
}
setTime(hr, mn, sc, 1, 1, 2017); // set the time, default the date
}
} else {
Serial.print("Unrecognized header character ");
Serial.print(c);
Serial.println();
}
}
}
@Sigafoos
Copy link
Author

Sigafoos commented Feb 21, 2017

To set the time, while connected via serial you can send a timestamp (^+m to open the serial window in the Windows Arduino program). Then enter either H081500 to set the time to 8:15:00 am on 1/1/17 or T<unix timestamp> to set to the epoch timestamp. The former was easier for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment