I am doing an aquarium light sunrise/sunset. Till now programming wise I only did the sunrise. After running the code the light starts to rise up fine, than at full brightness, it turns off. I want to keep it on after it reaches the 100% pwm and than starts decreasing when it's time to get off. Any help on how to leave the pwm at 100% and than starts to dim when it's time to turn off? I am still new to coding arduino, so explanation need to be for a beginner :P Thanks before hand.
#include <DS3231.h>
//Set Variables
int fadeTime = 15; //Fade Time
int onHour = 12; //Light On Hour
int onMin = 00; //Light On Minute
int offHour = 19; //Light On Hour
int offMin = 00; //Light On Minute
int led = 9; //Set pinout with with PWM
DS3231 rtc(SDA, SCL);
Time t;
void start();
void setup()
{
pinMode(led, OUTPUT);
rtc.begin();
}
void loop()
{
t = rtc.getTime();
// Send Day-of-Week
Serial.print(rtc.getDOWStr());
Serial.print(" ");
// Send date
Serial.print(rtc.getDateStr());
Serial.print(" -- ");
// Send time
Serial.println(rtc.getTimeStr());
if (t.hour == onHour && t.min == onMin) //Check Time
{
start();
}
}
void start()
{
// Fade script
for (int i = 0 ; i <= 255; i++)
{
analogWrite(led, i);
delay(((fadeTime * 60000)/306));
}
}
1 Answer 1
Why not create a global brightness variable (e.g. cBrightness) so that you can adjust the brightness accordingly when the time comes? With a global variable, you can still maintain the brightness of the LED even after it exits the start() function.
For the start() function, you can include one argument (int/bool type variable) in the function to determine when to perform the brightening or dimming of the light depending on the RTC timing.
void start(int st)
{
if(st==0)
{
//Brightness decrement function here
}else if(st==1)
{
//Brightness increment function here
}
}
For the brightness decrement function, it is the opposite of the increment function:
for (int i = 255 ; i >= 0; i--)
{
analogWrite(led, i);
delay(((fadeTime * 60000)/306));
}
To summarize, the code could look something like this:
#include <DS3231.h>
//Set Variables
int fadeTime = 15; //Fade Time
int onHour = 12; //Light On Hour
int onMin = 00; //Light On Minute
int offHour = 19; //Light On Hour
int offMin = 00; //Light On Minute
int led = 9; //Set pinout with with PWM
int cBrightness = 0; //Current brightness
DS3231 rtc(SDA, SCL);
Time t;
void start(int st);
void setup()
{
pinMode(led, OUTPUT);
rtc.begin();
}
void loop()
{
t = rtc.getTime();
// Send Day-of-Week
Serial.print(rtc.getDOWStr());
Serial.print(" ");
// Send date
Serial.print(rtc.getDateStr());
Serial.print(" -- ");
// Send time
Serial.println(rtc.getTimeStr());
if (t.hour == onHour && t.min == onMin) //Check Time
{
start(1);
}else if (t.hour == offHour && t.min == offMin)
{
start(0);
}
//Maintains brightness
analogWrite(led, cBrightness);
}
void start(int st)
{
if(st==0) //Will decrement in brightness
{
for (int i = 255 ; i >= 0; i--)
{
analogWrite(led, i);
delay(((fadeTime * 60000)/306));
}
cBrightness = 0;
}else if(st==1) //Will increment in brightness
{
for (int i = 0 ; i <= 255; i++)
{
analogWrite(led, i);
delay(((fadeTime * 60000)/306));
}
cBrightness = 255;
}
}
start
.