Hi guys!!
I've built my own controller for my aquarium, It has temperature control via relays, and led control over PWM signal.
The LEDs go up slowly after the "ontime" function is met (11am) but for some reason (maybe a bug), the light starts to go up after 12:30 or so..
Could you guys help me to optimize the code? Im not very good at coding..
The main problem is that it has to be physically reset every day in the morning before the lights go on to avoid problems with the RTC, and the lights won't sync properly, it would take more than an hour for the lights to start to ramp up.. even if I set ( int ramptime = 60 ) for 60 minutes..
part of the code:
int ramptime = 120; // time for white LEDs to dim on and off in minutes
int bluemin = 0 ; // minimmum dimming value of blue LEDs, range of 0-255
int bluemax = 255 ; // maximum dimming value of blue LEDs, range of 0-255
int whitemin = 0 ; // minimum dimming value of white LEDs, range of 0-255
int whitemax = 255 ; // maximum dimming value of white LEDs, range of 0-255
int photoperiod = 360 ; // amount of time array is on at full power in minutes
int ontime = 11 ; // time of day (hour, 24h clock) to begin photoperiod fade in
int blue = 10; // blue LEDs connected to digital pin 3 (pwm)
int white = 11; // white LEDs connected to digital pin 11 (pwm)
int bluepercent[] = { 13, 26, 52, 78, 103, 128, 154, 180, 205, 230, 255, 255, 255, 255, 255, 255 };
int whitepercent[]= { 0, 0, 0, 6, 13, 26, 52, 78, 103, 128, 154, 180, 205, 230, 255, 255 };
int abc(sizeof(bluepercent)/sizeof(bluepercent[0]));
LiquidCrystal lcd(12,13,4,5,6,7);
void onesecond() //function that runs once per second while program is running
{
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
lcd.setCursor(0, 0);
if(hour>0)
{
if(hour<=12)
{
lcd.print(hour, DEC);
}
else
{
lcd.print(hour-12, DEC);
}
}
else
{
lcd.print("12");
}
lcd.print(":");
if (minute < 10) {
lcd.print("0");
}
lcd.print(minute, DEC);
//lcd.print(":");
//if (second < 10) {
// lcd.print("0");
//}
//lcd.print(second, DEC);
if(hour<12)
{
lcd.print("am");
}
else
{
lcd.print("pm");
}
//lcd.print(" ");
delay(1000);
}
void setup() {
pinMode(ledPin1, OUTPUT); // set the digital pin as output:
pinMode(ledPin2, OUTPUT); // set the digital pin as output:
pinMode(fan, OUTPUT);
sensors.begin(); // Start up the DS18B20 Temp library
/*||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| L O O P - D I M F U N C T I O N |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/
float fSecond, fHour, fMinute; //turn the times read from the RTC into float for math ops
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year; //declare variables to hold the times
getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year); //read the RTC times
int daybyminute = ((hour * 60) + minute); //converts time of day to a single value in minutes
fSecond = (float) second; //sets fSecond as the float version of the integer second from the RTC
fMinute = (float) minute; //same as above, but for the minute
fHour = (float) hour; //ditto, but for the hour
int rampup;
if (daybyminute >= (ontime*60))
rampup = (((ontime*60) + ramptime) - daybyminute);
else
rampup = ramptime;
int rampdown;
if (((ontime * 60) + photoperiod + ramptime) <= daybyminute)
rampdown = (((ontime*60) + photoperiod + 2*ramptime) - daybyminute);
else
rampdown = ramptime;
/*|||||||||||||||||||||||||||||||||||||||||| F A D E I N ||||||||||||||||||||||||||||||||||||||||||||||*/
if (daybyminute >= (ontime*60))
{ if (daybyminute < ((ontime*60) + ramptime))
{
Temperature();
int i;
for (int i = 0; i < abc; i++)
{
analogWrite(blue, bluepercent[i]);
analogWrite(white, whitepercent[i]);
lcd.setCursor(13, 1);
lcd.print((bluepercent[i]*99)/255);
if (i < 10) lcd.print(" ");
lcd.setCursor(18, 1);
lcd.print((whitepercent[i]*99)/255);
if (i < 10) lcd.print(" ");
int countdown = ((rampup*60)/abc);
while (countdown>0)
{
onesecond();
countdown--;
Temperature();
lcd.setCursor(6, 1);
if (countdown < 100) lcd.print ("0");
if (countdown < 10) lcd.print ("0");
lcd.print(countdown);
}
}
}
}
/*||||||||||||||||||||||||||||||||||||||||||||||| M A X |||||||||||||||||||||||||||||||||||||||||||||*/
if ( daybyminute >= ((ontime * 60) + ramptime))
{
if ( daybyminute < ((ontime * 60) + ramptime + photoperiod ))
{
analogWrite(blue, 255);
analogWrite(white, 255);
lcd.setCursor(13, 1); //Poss Problem
lcd.print(99); //Poss Problem
lcd.setCursor(18, 1); //Poss Problem
lcd.print(99); //Poss Problem
lcd.setCursor(6, 1);
lcd.print(" ");
}
}
/*||||||||||||||||||||||||||||||||||||||||||||| F A D E O U T |||||||||||||||||||||||||||||||||||||||||*/
if ( daybyminute >= ((ontime * 60) + photoperiod + ramptime))
{
if ( daybyminute < (ontime * 60) + photoperiod + (ramptime *2) )
{
lcd.setCursor(0, 0);
lcd.setCursor(18, 0);
for (int i = abc-1; i >= 0; i--)
{
analogWrite(blue, bluepercent[i]);
analogWrite(white, whitepercent[i]);
lcd.setCursor(13, 1);
lcd.print((bluepercent[i]*99)/255);
if (i < 10) lcd.print(" ");
lcd.setCursor(18, 1);
lcd.print((whitepercent[i]*99)/255);
if (i < 10) lcd.print(" ");
int countdown = ((rampdown*60)/abc); // calculates seconds to next step
while (countdown>0)
{
onesecond();
countdown--;
Temperature();
lcd.setCursor(6, 1);
if (countdown < 100) lcd.print ("0");
if (countdown < 10) lcd.print ("0");
lcd.print(countdown);
}
}
}
}
//*|||||||||||||||||||||||||||||||||||||||||||||||| Night Time ||||||||||||||||||||||||||||||||||||||||||||||*/
if (daybyminute >= (((ontime * 60) + photoperiod + (2 * ramptime))))
{
lcd.setCursor(6, 1);
lcd.print(" ");
}
}
I would appreciate any help..