// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
#include <Wire.h>
#include "RTClib.h"
#define ledPin 9;
RTC_DS1307 rtc;
DateTime timeNow;
char daysOfTheWeek[7][12] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Sunday", "Saturday"};
void setup () {
Serial.begin(57600);
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (! rtc.isrunning()) {
Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
}
void printTimeToSerial(){
Serial.print(timeNow.year(), DEC);
Serial.print('/');
Serial.print(timeNow.month(), DEC);
Serial.print('/');
Serial.print(timeNow.day(), DEC);
Serial.print(" (");
Serial.print(daysOfTheWeek[timeNow.dayOfTheWeek()]);
Serial.print(") ");
Serial.print(timeNow.hour(), DEC);
Serial.print(':');
Serial.print(timeNow.minute(), DEC);
Serial.print(':');
Serial.print(timeNow.second(), DEC);
Serial.println();
}
// 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24 <<-- This is the hours
int fadeValueSetpoint[24] = {5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 127, 127, 127, 127, 127, 127, 127}; //<<-- This is the fade value limit for the respective hour
void loop () {
static boolean fadeComplete = false; //static means this variable will not be destroyed at the end of the loop and be used in the next scan cycle within this function
static int timeHr = 0;
static long printDelay = 0;
static long fadeDelay = 0;
timeNow = rtc.now();
if (millis() > printDelay) { //this is a passive delay to send the time to the serial port, but does not pause the program
printTimeToSerial();
printDelay = millis() + 3000;
}
timeHr = timeNow.hour(); //this is used to select the fade value limit
if (millis() > printDelay) { //this will constantly update to the setpoint you give for a given hour. It will close in on it's setpoint every 500ms
if (fadeValue) > (fadeValueSetPoint[timeHr] + 5)) fadeValue -= 5;
if (fadeValue) < (fadeValueSetPoint[timeHr] - 5)) fadeValue += 5;
analogWrite(ledPin, fadeValue);
printDelay = millis() + 500; //to change fade timebase, change the 500 to the ms you want.
}
}