I am fairly new to code so this might be a simple question. I have been trying to create a controller for my terrarium and am now stuck on trying to get the lights to work properly. I am using 4 colors of Cree LED's with individual Mean Well LDD drivers (http://www.meanwell.com/search/LDD-H/LDD-H-spec.pdf). I got the LED's to fade over a long period of time using the example fade sketch but it uses delays that halt my other tasks. I would like the LED's to fade on at different speeds and at different times to simulate sunrise, sunset, etc. while still having my LCD display show the readings from my DHT22's as well as control a relay controlled outlet. For time measurement I have a DS1307 (DS1307 Real Time Clock breakout board kit : ID 264 : $7.95 : Adafruit Industries, Unique & fun DIY electronics and kits) and so far I have got everything running well together besides the LED's. I have looked into libraries for fading without delay such as the Arduino-LEDFader (GitHub - jgillick/arduino-LEDFader: An arduino library to fade individual LEDs in the background without blocking your main program.) library but cannot get the LED's to turn on over a long period of time. At the moment I am still working with just getting 1 LED color to turn on over the course of about 10 min using the example sketch for 1 LED in that library.
#include <LEDFader.h>
/*
Fades a single LED up and down using LED Fader.
Connect an LED to pin 3
*/
#define LED_PIN 11
#define FADE_TIME 600000
#define DIR_UP 1
#define DIR_DOWN -1
LEDFader led;
int direction = DIR_UP;
void setup() {
led = LEDFader(LED_PIN);
led.fade(255, FADE_TIME);
}
void loop() {
led.update();
// LED no longer fading, switch direction
if (!led.is_fading()) {
// Fade down
if (direction == DIR_UP) {
led.fade(0, FADE_TIME);
direction = DIR_DOWN;
}
// Fade up
else {
led.fade(255, FADE_TIME);
direction = DIR_UP;
}
}
}
The example has the FADE_TIME set to 2000 and seems to work well for a 2 second fade however setting the fade time to 600,000 to get a 10 minute fade only seems to give about a 10 second fade. Is there some reason why I cannot get over about 10 seconds and would there be a better library to try to attempt doing this?
So I found a way to do this but it seems like a stupid method. Essentially I have set if statements to fade the led every 10 seconds and could be increased to 10 minutes but would be extremely repetitive.
#include <LEDFader.h>
/*
Fades a single LED up and down using LED Fader.
Connect an LED to pin 3
*/
#define LED_PIN 11
#define FADE_TIME 10000
#define DIR_UP 1
#define DIR_DOWN -1
LEDFader led;
int direction = DIR_UP;
#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 rtc;
void setup() {
led = LEDFader(LED_PIN);
Serial.begin(57600);
#ifdef AVR
Wire.begin();
#else
Wire1.begin(); // Shield I2C pins connect to alt I2C bus on Arduino Due
#endif
rtc.begin();
}
void loop() {
led.update();
DateTime now = rtc.now();
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.print(now.second(), DEC);
Serial.println();
if (now.hour() == 9 & now.minute() == 53 & now.second() == 0) {
led.fade(40, FADE_TIME);
}
if (now.hour() == 9 & now.minute() == 53 & now.second() == 10) {
led.fade(80, FADE_TIME);
}
if (now.hour() == 9 & now.minute() == 53 & now.second() == 20) {
led.fade(120, FADE_TIME);
}
if (now.hour() == 9 & now.minute() == 53 & now.second() == 30) {
led.fade(160, FADE_TIME);
}
if (now.hour() == 9 & now.minute() == 53 & now.second() == 40) {
led.fade(200, FADE_TIME);
}
if (now.hour() == 9 & now.minute() == 53 & now.second() == 50) {
led.fade(240, FADE_TIME);
}
}
I hope that there is a better way of doing this so I don't end up with 10,000 lines of code just for adjusting LED brightness.
The example has the FADE_TIME set to 2000 and seems to work well for a 2 second fade however setting the fade time to 600,000 to get a 10 minute fade only seems to give about a 10 second fade. Is there some reason why I cannot get over about 10 seconds and would there be a better library to try to attempt doing this?
I've never used LEDFader.h, but it looks like fade time is defined as an unsigned int which can hold values between 0 and 0 to 65,535.
As far as timing more than one "thing" at a time (so your various LEDs & colors can fade independently) you can base your sketch on the [u]Blink Without Delay Example[/u], but you can have multiple variables such as previousMillisRed, previousMillisBlue, intervalRed, intervalBlue, etc.
One thing you should try to avoid (whenever possible) is checking for time being to exactly equal a value. It's only equal for a short time and you can miss it. Use >= (greater than or equal) or <= whenever you can. Even when you're not dealing with time, it's often "safer" to use logic that doesn't have to match exactly.
...but would be extremely repetitive.
Computers are really good at repetitive tasks! That's what loops are for! If you find yourself (your program) doing the same thing over-and-over, you probably need a loop. Of course, you can change/update variables each time through the loop.
I don't know if you'll need them, but you can have nested loops (loops inside loops).
Loops, conditional branching (if-statements, etc.), and mathematical/logical operations are the 3 things that make computers & programming useful!