I am working on an Aquarium controller for some custom LEDs I have built for my fish tank.
Please see my previous post: Unix Time 'if' statements - Project Guidance - Arduino Forum
I have hit yet another wall so after hours of researching i decided to turn to help again!
This is my first project using Arduino so please excuse any mistakes/messy code but this is what i have so far:
// Aquarium Control // Dean Cohen //
// v0.1 Testing PWM control of LED Driver
// v0.2 Added DS3231 RTC, Configuring date/time to show in serial monitor (Thanks to http://www.rinkydinkelectronics.com/library.php?id=73)
// v0.3 Configured to use real time instead of unix time (Thanks to sterretje)
// v0.4 Set to turn LEDs on/off at set times
//Connect 'Dim' wire from LED Driver to pin 10
//Connect Driver GND input to arduino GND (same side as pin 10)
//Connect (RTC-Arduino): GND-GND, VCC-3.3v, SDA-A4, SCL-A5
#include <DS3231.h>
// Init the DS3231 using the hardware interface
DS3231 rtc(A4, A5);
int led = 10;
// int fan = 11;
int brightness = 0;
int fadeqty = 20;
void setup() {
pinMode(led, OUTPUT);
pinMode(fan, OUTPUT);
Serial.begin(9600);
rtc.begin();
// The following lines can be uncommented to set the date and time
//rtc.setDOW(WEDNESDAY); // Set Day-of-Week to SUNDAY
//rtc.setTime(23, 07, 00); // Set the time to 12:00:00 (24hr format)
//rtc.setDate(30, 03, 2016); // Set the date to January 1st, 2014
}
void loop()
{
// Send Day-of-Week
Serial.print(rtc.getDOWStr());
Serial.print(" ");
// Send date
Serial.print(rtc.getDateStr());
Serial.print(" -- ");
// Send time
Serial.println(rtc.getTimeStr());
// pointer to a time string
char *strTime;
// Get formatted Time
strTime = rtc.getTimeStr(FORMAT_LONG);
//LEDs turn on if given time is met
if(strcmp(strTime, "07:30:00") == 0) {
Serial.print("***LED ON***");
Serial.print('\n');
analogWrite(led, 255);
}
//LEDs turn off if given time is met
if(strcmp(strTime, "17:00:00") == 0) {
Serial.print("***LED OFF***");
Serial.print('\n');
analogWrite(led, 0);
}
// fade LEDs on/off
//analogWrite(led, brightness);
//if(strcmp(strTime, "23:03:15") == 0) {
//brightness = brightness + fadeqty;
//}
//if (brightness == 255) {
//brightness == 255;
//Serial.print("BRIGHTNESS: ");
//Serial.print(brightness);
//Serial.print('\n');
//delay (500);
//}
delay (1000);
}
// Not Yet Used - Set fan to 100% if exceeds Temp
// if(rtc.getTemp() > "VALUE")
// Serial.print("***FAN ON***");
// analogWrite(fan, 255);
// Not Yet Used - PWM LED code
// analogWrite(led, 64);
// delay(1000);
// analogWrite(led, 127);
// delay(1000);
// analogWrite(led, 191);
// delay(1000);
// analogWrite(led, 255);
// delay(1000);
// Not Used - Print RTC built-in Temp
// // RTC Temp
// Serial.print("RTC Temperature: ");
// Serial.print(rtc.getTemp());
// Serial.println(" C");
// Not Used - Print Unix time to serial (useful for code below)
// //Send Unix Time
// Serial.print("Unix Time: ");
// Serial.print(rtc.getUnixTime(rtc.getTime()));
// Serial.print('\n');
// Not used - if (Unix time) = (defined time) print ***LED ON***
// if (rtc.getUnixTime(rtc.getTime()) == 1459255200)
// Serial.print("***LED ON***");
// Serial.print('\n');
so the part I am working on/stuck on is:
//LEDs turn on if given time is met
if(strcmp(strTime, "07:30:00") == 0) {
Serial.print("***LED ON***");
Serial.print('\n');
analogWrite(led, 255);
}
//LEDs turn off if given time is met
if(strcmp(strTime, "17:00:00") == 0) {
Serial.print("***LED OFF***");
Serial.print('\n');
analogWrite(led, 0);
Id like to come up with an if statement to define when to start fading the lights on and off, ive tried a few examples however these use 'delay' which doesnt seem to work right with the delay I already have,
my thoughts are that i can link it into the RTC somehow?
a very messy solution would be something like this:
//LEDs turn on if given time is met
if(strcmp(strTime, "07:30:00") == 0) {
Serial.print("***LED ON***");
Serial.print('\n');
analogWrite(led, 0); }
//LEDs turn on if given time is met
if(strcmp(strTime, "07:31:00") == 0) {
Serial.print("***LED ON***");
Serial.print('\n');
analogWrite(led, 64); }
//LEDs turn on if given time is met
if(strcmp(strTime, "07:32:00") == 0) {
Serial.print("***LED ON***");
Serial.print('\n');
analogWrite(led, 127); }
//LEDs turn on if given time is met
if(strcmp(strTime, "07:33:00") == 0) {
Serial.print("***LED ON***");
Serial.print('\n');
analogWrite(led, 191); }
//LEDs turn on if given time is met
if(strcmp(strTime, "07:34:00") == 0) {
Serial.print("***LED ON***");
Serial.print('\n');
analogWrite(led, 255); }
but I was hoping for a smooth gradual fade over say 30 minutes from 0-255.
I also tried something along the lines of this:
fade LEDs on/off
analogWrite(led, brightness);
if(strcmp(strTime, "23:03:15") == 0) {
brightness = brightness + fadeqty;
}
if (brightness == 255) {
brightness == 255;
Serial.print("BRIGHTNESS: ");
Serial.print(brightness);
Serial.print('\n');
delay (500);
//}
But it has the delay and i struggled getting my head around the formula last night.
Thanks!