Last active
July 21, 2019 17:41
-
-
Save jonmagic/347f2c69328ceb122721335b5732eb39 to your computer and use it in GitHub Desktop.
Program for red and green light interactive alarm clock.
This file contains hidden or 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
#include <math.h> | |
int BUTTON = D0; | |
int GREEN_LED = D1; | |
int RED_LED = A4; | |
int buttonState = 0; | |
// Takes the current hour (0-23) and multiplies it by 60 (minutes) and then | |
// adds the minutes after the hour (0-59. | |
// | |
// Example: | |
// 8:20am = 8 * 60 + 20 = 480 + 20 = 500 | |
// 1:42pm = 13 * 60 + 42 = 780 + 42 = 822 | |
// | |
int hourAndMinutes() { | |
return (Time.hour() * 60) + Time.minute(); | |
} | |
// Is it between 6:45am and 8:00pm? | |
bool okToGetUp() { | |
if (hourAndMinutes() >= 405 && hourAndMinutes() < 1200) { // 6:45 to 20:00 | |
return true; | |
} | |
return false; | |
} | |
// Is it between 5:30am and 6:45am? | |
bool notifyStayInBed() { | |
if (hourAndMinutes() >= 330 && hourAndMinutes() < 405) { // 5:30 to 6:44 | |
return true; | |
} | |
return false; | |
} | |
// Is it between 6:45am and 8:30am? | |
bool notifyOkToGetUp() { | |
if (hourAndMinutes() >= 405 && hourAndMinutes() < 510) { // 6:45 to 8:30 | |
return true; | |
} | |
return false; | |
} | |
// Is it daylight savings time? | |
bool isDST(int day, int month, int dayOfWeek) { | |
//January, february, and december are out. | |
if (month < 3 || month > 11) { return false; } | |
//April to October are in | |
if (month > 3 && month < 11) { return true; } | |
int previousSunday = day - dayOfWeek; | |
//In march, we are DST if our previous sunday was on or after the 8th. | |
if (month == 3) { return previousSunday >= 8; } | |
//In november we must be before the first sunday to be dst. | |
//That means the previous sunday must be before the 1st. | |
return previousSunday <= 0; | |
} | |
// Runs once at startup. | |
void setup() { | |
// Disable onboard RGB LED. | |
RGB.control(true); | |
RGB.brightness(64); | |
// Configure buttons. | |
pinMode(BUTTON, INPUT); | |
pinMode(GREEN_LED, OUTPUT); | |
pinMode(RED_LED, OUTPUT); | |
// Handle timezones. | |
int offset = isDST(Time.day(), Time.month(), Time.weekday() - 1) ? -7 : -8; | |
Time.zone(offset); | |
} | |
// Runs in a loop. | |
void loop() { | |
// First get the button state. | |
buttonState = digitalRead(BUTTON); | |
// Return early if there are no notifications and the button has not been pressed. | |
if (!notifyStayInBed() && !notifyOkToGetUp() && buttonState == LOW) { | |
// Turn off the LEDs. | |
analogWrite(RED_LED, 0); | |
analogWrite(GREEN_LED, 0); | |
// Skip the rest of our code. | |
return; | |
} | |
int on_led; | |
int off_led; | |
// Determine which LED should be on and which one should be off. | |
if (okToGetUp()) { | |
on_led = GREEN_LED; | |
off_led = RED_LED; | |
} else { | |
on_led = RED_LED; | |
off_led = GREEN_LED; | |
} | |
// If the button was pressed. | |
if (buttonState == HIGH) { | |
// Log the event. | |
Particle.publish("button_press", PRIVATE); | |
// Quickly pulse the LED a few times. | |
float in, out; | |
for (in = 0; in < 50.264; in = in + 0.001) { | |
out = sin(in) * 127.5 + 127.5; | |
analogWrite(on_led, out); | |
analogWrite(off_led, 0); | |
delayMicroseconds(125); | |
} | |
// If the button was not pressed. | |
} else { | |
// Turn on whatever LED should be on and turn off the other one. | |
analogWrite(on_led, on_led == RED_LED ? 127 : 255); | |
analogWrite(off_led, 0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment