/* A smart light bulb that lights up when its time to get out of your bed.
How it works: 1) when power is turned on, the light is switched on as a check that it works 2) it will slowly dim until turned off completelly 3) at 6 am in the morning light will slowly turn on until switched off at the evening 4) light will not turn on when room is not dark anymore (photocell)
How to use: - turn on the light when going to bed and it will light up the room when you wake up in the morning, turn off light when leaving room to turn it off. - the time will be preserved due to the DS3231 module
@author Jiri Praus (https://twitter.com/jipraus)
AC Light control inspired by http://arduinotehniq.blogspot.com/2014/10/ac-light-dimmer-with-arduino.html
// triac control variables volatile byte triacCounter = 0; // triac control timer volatile boolean zeroCrossed = false; // AC phase zero-crossed flag
// dim control byte dimLevel = MIN_DIM_LEVEL; // on power on, the light is on and will dim slowly as a check boolean lightOn = false;
// alarm clock RTC_DS3231 rtc;
void setup() {
Serial.begin(115200);
digitalWrite(TRIAC_PIN, LOW); pinMode(TRIAC_PIN, OUTPUT); pinMode(ZERO_CROSS_PIN, INPUT); pinMode(LIGHT_SENSOR_PIN, INPUT);
// init real time clock
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while(1);
}
if (rtc.lostPower()) {
Serial.println("RTC lost power, lets set the time!");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // following line sets the RTC to the date & time this sketch was compiled
}
// initialize interrupts and timers for triac control noInterrupts(); attachInterrupt(digitalPinToInterrupt(ZERO_CROSS_PIN), zeroCrossDetected, RISING); // Attach an Interupt to Pin 2 (interupt 0) for Zero Cross Detection interrupts();
Timer1.initialize(FREQUENCY_STEP); Timer1.attachInterrupt(triacTimerInterrupt, FREQUENCY_STEP);
}
void loop() {
delay(CHANGE_DIM_LEVEL_EVERY * 1000); checkAlarmClock(); adjustDimLevel();
debugPrint();
}
void zeroCrossDetected() {
zeroCrossed = true; // set the boolean to true to tell our dimming function that a zero cross has occured triacCounter = 0; // start counting when triac should be opened digitalWrite(TRIAC_PIN, LOW); // turn off light
}
void triacTimerInterrupt() {
if (dimLevel >= MAX_DIM_LEVEL) { // permanently off digitalWrite(TRIAC_PIN, LOW); } else if (dimLevel <= MIN_DIM_LEVEL) { // permanently on digitalWrite(TRIAC_PIN, HIGH); } else if (zeroCrossed) { if (triacCounter >= dimLevel) { digitalWrite(TRIAC_PIN, HIGH); // open triac zeroCrossed = false; // reset zero cross detection until next half vawe } else { triacCounter ++; // increment time step counter } }
}
void adjustDimLevel() {
if (lightOn && dimLevel > MIN_DIM_LEVEL) { dimLevel--; } else if (!lightOn && dimLevel < MAX_DIM_LEVEL) { dimLevel++; }
}
void checkAlarmClock() {
if (!lightOn && isRoomDark()) { // do not waste energy when room is already light up
DateTime now = rtc.now();
if (now.hour() == 6 && now.minute() == 0) {
lightOn = true; // start alarm clock at 6am
}
}
}
boolean isRoomDark() {
return analogRead(LIGHT_SENSOR_PIN) < DARK_THRESHOLD;
}
void debugPrint() {
Serial.print("lightOn="); Serial.println(lightOn);
Serial.print("dimLevel="); Serial.println(dimLevel);
DateTime now = rtc.now();
Serial.print("datetime=");
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.println(now.second(), DEC);
Serial.print("lightLevel="); Serial.println(analogRead(LIGHT_SENSOR_PIN));
}