Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
For full tutorial, search http://www.makeuseof.com for "sunrise alarm"
/*
* Sunrise Alarm Clock and Night Light by James Bruce
* http://duinobits.com
* Initial setup requires you to reset the Arduino sometime in the evening.
* Set the hoursUntilSunrise relative to this time.
* eg, if you reset at 10pm, and want the sunrise to start at 6:30am,
* the value should be 8.5 - this will then be automatically changed to minutes for internal clock to function
*
*/
float hoursUntilSunrise = 8.0;
/* How many milliseconds the nightlight should stay on for (1 minutes would be 1 x 60 x 1000ms = 60000 */
int nightLightTimeOut = 60000;
/* Dont adjust these global variables below */
#define RED 9
#define GREEN 11
#define BLUE 10
#define nightLight 12
#define trigger 2
unsigned long currentMillis = 0; // stores current time value
unsigned long previousMillis = 0; // the value of the previous days millis(), so we can reset each day
unsigned long currentMinutes = 0; // just easier to work with
unsigned int minutesUntilSunrise;
unsigned long nightLightTimeOff = 0; // A single number to store the time when we should deactive the current night light cycle
bool nightLightOn = false; // enable re-activation even when already activated
void setup() {
pinMode(nightLight,OUTPUT);
pinMode(trigger,INPUT);
pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
Serial.begin(9600);
analogWrite(RED,0);
analogWrite(GREEN,0);
analogWrite(BLUE,0);
// minutes until begin sunrise
minutesUntilSunrise = hoursUntilSunrise * 60;
Serial.print("Minutes until sunrise:");
Serial.println(minutesUntilSunrise);
}
void loop(){
clock();
nightlight();
sunrisealarm();
delay(1000); // Use when debugging to examine serial values at a sensible rate
}
void clock(){
if(millis() >= previousMillis+86400000){
// a full day has elapsed, reset the clock;
previousMillis +=86400000;
}
currentMillis = millis() - previousMillis; // this keeps our currentMillis the same each day
currentMinutes = (currentMillis/1000)/60;
Serial.println(currentMinutes);
}
void sunrisealarm(){
//each second during the 30 minite period should increase the colour value by:
float increment = (float) 255/(30*60);
//red 255 , green 255 gives us full brightness yellow
if(currentMinutes >= minutesUntilSunrise){
//sunrise begins!
float currentVal = (float)((currentMillis/1000) - (minutesUntilSunrise*60)) * increment;
Serial.print("Current value for sunrise:");
Serial.println(currentVal);
//during ramp up, write the current value of minutes X brightness increment
if(currentVal < 255){
analogWrite(RED,currentVal);
analogWrite(GREEN,currentVal);
}
else if(currentMinutes - minutesUntilSunrise < 40){
// once we're at full brightness, keep the lights on for 10 minutes longer
analogWrite(RED,255);
analogWrite(GREEN,255);
}
else{
//after that, we're nuking them back to off state
analogWrite(RED,0);
analogWrite(GREEN,0);
}
}
}
void nightlight(){
// Only work between the hours of reset -> sunrise.
if(currentMinutes < minutesUntilSunrise){
if(digitalRead(trigger) == 1){
nightLightTimeOff = millis()+nightLightTimeOut; // activate, or extend the time until turning off the light
Serial.println("Activating nightlight");
}
}
//Turn light on if needed
if(millis() < nightLightTimeOff){
digitalWrite(nightLight,HIGH);
}
else{
digitalWrite(nightLight,LOW);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.