Aqua controller programming question ..

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..

First auto format all your code and then POST IT ALL!!! If you have an RTC why do you need to reset it every day? In fact why have an RTC at all.

Mark

Im sorry.. but how can I post the whole code? it won't allow me to post it.. but here's the link :

http://rapidshare.com/files/90454223/readme.txt

maybe "int blueramptime = 120 ; " is multiplied to be longer (somehow?) by Temperature(); and relay2(); and relay1();

Maybe that's why the lights won't start at 11.. (int ontime = 11)

It should not be necessary to reset it every day, and if that reset is exposing some startup problems then the reset issue would be the thing to focus on.

Having said that, I have to confess that I don't follow the 'ramp' logic, which seems to do a lot of comparisons with rampup, rampdown, ontime, ramptime etc which feels as if it is probably doing something simple, but I can't follow what. If you're trying to produce a brightness value that ramps up in the morning and ramps back down in the afternoon, I feel sure that could be done more simply, and more importantly that the algorithm used to do it could be abstracted out into a function to calculate the brightness for a given time-of-day so that this isn't muddling up the logic to decide when to update the brightness (which you could simply do at regular intervals such as once at startup, and then once per minute).