Anonymous Anonymous - 8 months ago
618 0

No description

C++

Sunrise LED Fader

// 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 fadeValueLimit[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;
  
  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;
  }

  if (timeHr != timeNow.hour()) fadeComplete == false;   //this checks if the hour has changed and enables the fade process if it has
  timeHr = timeNow.hour();  //this is used to select the fade value limit
  
  if (timeNow.minute() == 00 && fadeComplete == false) {
    for (int fadeValue = 0 ; fadeValue <= fadeValueLimit[timeHr]; fadeValue += 5) {
      analogWrite(ledPin, fadeValue);
    }
    fadeComplete = true;  //this stops constant fading of the LED while minutes is still zero.  You want this to execute once every hour
  }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download