(Another) Arduino Light Controller DIY build.

Reef Aquarium & Tank Building Forum

Help Support Reef Aquarium & Tank Building Forum:

i want mine to ramp up in percent increments and the same for ramping down... will i still use the same code as you?
 
Yes, with this code, it tells you on your screen that Blue = 99 white = 99... And you have control on how long you want that increment to go up and down.

Example... on this line
int blueramptime = 5 ; // time for blue LEDs to dim on and off in minutes

You tell it to ramp blue within 5 minutes, from 0 power to 100% power. You can simply change that to 60 minutes.

What the whole program does is...



Standby and wait for the correct time to start.... this case 12:00 pm. You can change this to what ever ie: 11 for 11am, or 14 for 2pm
Code:
int ontime = 12 ;        // time of day (hour, 24h clock) to begin photoperiod fade in

Then if the correct time is met... this line is activated.... you can change 5 to 60. This means 1 hour of ramp up time.
Code:
int blueramptime = 5 ;   // time for blue LEDs to dim on and off in minutes

Then after blue reaches 100%... then white starts to ramp up... you can do the same here or maybe different value? ie: 120? for 2 hours?
Code:
int whiteramptime = 5 ;  // time for white LEDs to dim on and off in minutes

Then after that, if they are both 100%... it will execute this line... also change this to what ever.... the catch is, you have to do some math. So if you want 12 hours of dusk dawn effect. Minus the 2 hours of ramp up and 2 hours of ramp down then youre left with 8 hours or 100% full blast... so we can change the value to 480.
Code:
int photoperiod = 5 ;    // amount of time array is on at full power in minutes

Then after that, after it finish 8 hours of full blast.... then it will initiate 1 hour of white ramp down and after that 1 hour of blue ramp down....



This line is if you want to start your LEDs at a certain percent already ie: 50%. This means 50% of 255 = 127??? , same with white which is whitemin
Code:
int bluemin = 0 ;        // minimmum dimming value of blue LEDs, range of 0-255
 
Here is the hardware side... testing, testing...

2011-01-30%2017-33-36.578.jpg



Next thing to do is to wire up the transistors so that we can have 10 volts signal for the LED drivers.
 
Last edited:
just got this from the techs who built my plasma

"It's not PWM. It's a voltage divider circuit which provides 1-10V analog DC signal"
 
just got this from the techs who built my plasma

"It's not PWM. It's a voltage divider circuit which provides 1-10V analog DC signal"

Oh boy! I dont even know what it means. I guess they dont recommend PWM signal to control the 10 volts signal on their driver?


For whats it worth, I finished my Arduino Light Controller and I found out the difference between the 48P driver and 48D driver.

The Mean Well ELN60-48P can dim your lights in a linear fashion way with 0-255 pwm steps.

While the 48D (analog) version will dim alright but not in perfect linear. From 0-80 pwms it will go up to 80% brightness (which is not actually bad). But the remaining 20% brigtness will be spread out from 80-255 pwms.

So if you have a ramptime of 60 minutes. The first 20 minutes, you will end up with 80% brightness. Then it will slowly go to 100% brightness in the next 40 minutes.

I just mentioned this just in case you plasma driver is similar to 48D mean wells.
 
On another note. Just to be safe... maybe we can JerryRigged something like the manual switch on the first page.

Arduino + Servo attached to your potentiometer. Then we just control the servo motor to turn your plasma for us?


jerryrig01.jpg
 
Last edited:
Well I am not a technical master but I am an electrician and I am pretty sure that the potentiometer is the divider circuit. What this does is have the constant voltage supplied to the outside post. The ground supplied on the other post and the division would be the center post which would have the dimming circuit output. I am almost positive that the arduino supplying the 10vdc will perform the same. If the actual ballast does not call for a digital pulsating dc, and the arduino output can be converted to an analog output. This set up should still work. It would be a matter of removing the potentiometer and rewiring the output voltage for the DIM+ and DIM- into where the POT was.
 
Tat2z, good point.

What i ended up doing is keep the potentiometer since it uses the positive side of the wire.
Then tap/use the negative side of the wire for arduino + NPN transistor. Works great.

Ie: if i want my LED to be at 750ma as max, then I just set my potentiometer at 750ma and just keep the arduino (sketch) as is.

And in case you want to put it back to original config, just short the two wires going to arduino. I will post a diagram later.
 
My only question is whether or not the arduino outputs a pulsating DC signal or is it the standard DC output?
 
Tat2z,

There are two way I believe you can connect it to Arduino.

1) Arduino can give off 5 volts (max) 40 ma per pin. This will just be on or off
2) On the PWM side, It can also do 5 volts 40 ma from 0 to 255 steps.

No. 2 is what Im using, but since it can only do 5 volts, I did not bother connecting it directly. I then just used a transistor (npn) to switch on or off the circuit. Its just a switch of very very fast on and offs.

So the arduino and the 0-10 volts original controller is/are fully isolated. You just simply splice your orig setup.

Here are the diagrams i made.

Original setup.
wiringdiag03A.jpg



And the one with arduino + transistor
wiringdiag03b.jpg
 
Ok, I believe I got it.

Because of the basic know how I have with regards to coding Arduino which I believe is the same or similar as C++. Here is my first attempt on the code itself.

Code:
#include "Wire.h" 
#define DS1307_I2C_ADDRESS 0x68 //set rtc
#include <LiquidCrystal.h> // initialize the library with the numbers of the interface pins



/*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  R E L A Y   P A R T  |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/
/*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  S I M P L E   O N   A N D   O F F   F E A T U R E |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/



const int ledPin1 =  3;          // the number of the LED pin
const int ledPin2 =  2;

int ledState1 = LOW;             // ledState used to set the RELAY
int ledState2 = LOW; 
long previousMillis1 = 0;        // will store last time RELAY was updated
long previousMillis2 = 0;
long interval1 = 5000;           // interval at which to blink (milliseconds)
long interval2 = 3000;



/*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  L E D   D I M M I N G   P A R T  |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/
/*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  F A D E S   I N   A N D   O U T  |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/



int blueramptime = 5 ;   // time for blue LEDs to dim on and off in minutes
int whiteramptime = 5 ;  // 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 = 5 ;    // amount of time array is on at full power in minutes
int ontime = 12 ;        // time of day (hour, 24h clock) to begin photoperiod fade in
int blue = 10;           // blue LEDs connected to digital pin 5
int white = 11;          // white LEDs connected to digital pin 6



LiquidCrystal lcd(8, 9, 4, 5, 6, 7);



/*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  C L O C K  |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/



// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
  return ( (val/10*16) + (val%10) );
}

// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
  return ( (val/16*10) + (val%16) );
}

// 1) Sets the date and time on the ds1307
// 2) Starts the clock
// 3) Sets hour mode to 24 hour clock
// Assumes you're passing in valid numbers
void setDateDs1307(byte second, // 0-59
byte minute, // 0-59
byte hour, // 1-23
byte dayOfWeek, // 1-7
byte dayOfMonth, // 1-28/29/30/31
byte month, // 1-12
byte year) // 0-99
{
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  Wire.send(0);
  Wire.send(decToBcd(second)); // 0 to bit 7 starts the clock
  Wire.send(decToBcd(minute));
  Wire.send(decToBcd(hour));   // If you want 12 hour am/pm you need to set
                               // bit 6 (also need to change readDateDs1307)
  Wire.send(decToBcd(dayOfWeek));
  Wire.send(decToBcd(dayOfMonth));
  Wire.send(decToBcd(month));
  Wire.send(decToBcd(year));
  Wire.endTransmission();
}

// Gets the date and time from the ds1307
void getDateDs1307(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
{
  // Reset the register pointer
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  Wire.send(0);
  Wire.endTransmission();

  Wire.requestFrom(DS1307_I2C_ADDRESS, 7);

  // A few of these need masks because certain bits are control bits
  *second = bcdToDec(Wire.receive() & 0x7f);
  *minute = bcdToDec(Wire.receive());
  *hour = bcdToDec(Wire.receive() & 0x3f); // Need to change this if 12 hour am/pm
  *dayOfWeek = bcdToDec(Wire.receive());
  *dayOfMonth = bcdToDec(Wire.receive());
  *month = bcdToDec(Wire.receive());
  *year = bcdToDec(Wire.receive());
}



/*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  O N E S E C O N D   F U N C T I O N |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/



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);
}




/*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  R E L A Y 1   O N   O F F   F U N C T I O N |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/


void relay1()
{
  unsigned long currentMillis = millis();
 
  if(currentMillis - previousMillis1 > interval1) {
    // save the last time you blinked the RELAY 
    previousMillis1 = currentMillis;   

    // if the RELAY is off turn it on and vice-versa:
    if (ledState1 == LOW)
      ledState1 = HIGH;
    else
      ledState1 = LOW;

    // set the RELAY with the ledState of the variable:
    digitalWrite(ledPin1, ledState1);
  }
}
  
  
  
/*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  R E L A Y 2   O N   O F F   F U N C T I O N |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/




void relay2()
{
  unsigned long currentMillis2 = millis();
 
  if(currentMillis2 - previousMillis2 > interval2) {
    // save the last time you blinked the RELAYD 
    previousMillis2 = currentMillis2;   

    // if the RELAY is off turn it on and vice-versa:
    if (ledState2 == LOW)
      ledState2 = HIGH;
    else
      ledState2 = LOW;

    // set the RELAY with the ledState of the variable:
    digitalWrite(ledPin2, ledState2);
  }
}




/*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  S E T U P  |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/




void setup() {
  // set the digital pin as output:
  pinMode(ledPin1, OUTPUT);  
  pinMode(ledPin2, OUTPUT); 
  lcd.clear();


/*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  S E T U P - D I S P L A Y |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/



  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  Wire.begin();

  // Change these values to what you want to set your clock to.
  // You probably only want to set your clock once and then remove
  // the setDateDs1307 call.
  second = 50;
  minute = 59;
  hour = 11;
  dayOfWeek = 6;
  dayOfMonth = 26;
  month = 1;
  year = 11;
  setDateDs1307(second, minute, hour, dayOfWeek, dayOfMonth, month, year);

  analogWrite(blue, bluemin);
  analogWrite(white, whitemin);
  lcd.begin(16, 2); // set up the LCD's number of rows and columns: 
//  lcd.print("12:00 80.6"); // Print a message to the LCD.
//  lcd.print(char(223));
  lcd.setCursor(0, 1);
  lcd.print("blue:");
  lcd.print(33*bluemin/85);
  lcd.print("  white:");
  lcd.print(33*whitemin/85);  
}




/*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  L O O P |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/




void loop()
{
  onesecond();
  relay2();
  relay1();





/*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  L O O P - D I M   F U N C T I O N |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/

  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
  int daybyminute = ((hour * 60) + minute); //converts time of day to a single value in minutes
  



/*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  L O O P - F A D E  I N |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/


  
  
  if (daybyminute >= (ontime*60))
  { 
    if (daybyminute <= ((ontime*60) + blueramptime)) //if time is in range of fade in, start fading in
    {

      // fade blue LEDs in from min to max in increments of 1 point:
      for(int bluefadeval = bluemin ; bluefadeval <= bluemax; bluefadeval ++) // sets the minimum and maximum values
      { 
        analogWrite(blue, bluefadeval); 
        lcd.setCursor(5, 1);
        lcd.print(33*bluefadeval/85);
        lcd.print(" "); 

        int countdown = ((blueramptime*60)/(bluemax-bluemin)); // calculates seconds to next step
        while (countdown>0)
        {
          onesecond(); // updates clock once per second
          countdown--;
          relay2();
          relay1();
        }
      }

      // fade white LEDs in from min to max in increments of 1 point:
      for(int whtfadeval = whitemin ; whtfadeval <= whitemax; whtfadeval ++) // sets the minimum and maximum values
      { 
        analogWrite(white, whtfadeval); 
        lcd.setCursor(14, 1);
        lcd.print(33*whtfadeval/85);
        lcd.print(" "); 

        int countdown = ((whiteramptime*60)/(whitemax-whitemin)); // calculates seconds to next step
        while (countdown>0)
        {
          onesecond(); // updates clock once per second
          countdown--;
          relay2();
          relay1();
        }
      } 
    }
  }



/*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  L O O P - M A X  V A L U E |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/


  
  if (((ontime * 60) + blueramptime + whiteramptime) < daybyminute) 
  { 
    if ( daybyminute < ((ontime * 60) + blueramptime + whiteramptime + photoperiod)) // if time is in range of photoperiod, turn lights on to maximum fade value
    {
      analogWrite(white, whitemax);
      analogWrite(blue, bluemax);
      lcd.setCursor(14, 1);
      lcd.print(33*whitemax/85);
      lcd.print(" "); 
      lcd.setCursor(5, 1);
      lcd.print(33*bluemax/85);
      lcd.print(" "); 
    } 
  }
 


/*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  L O O P - F A D E  O U T |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/



  if (((ontime * 60) + photoperiod + blueramptime + whiteramptime) <= daybyminute)
  { 
    if (((ontime * 60) + photoperiod + 2*blueramptime + whiteramptime) >= daybyminute)
    {
      // fade white LEDs out from max to min in increments of 1 point:
      for(int whtfadeval = whitemax; whtfadeval >= whitemin; whtfadeval --) // sets the minimum and maximum values
      { 
        analogWrite(white, whtfadeval);
        lcd.setCursor(14, 1);
        lcd.print(33*whtfadeval/85);
        lcd.print(" "); 

        int countdown = ((whiteramptime*60)/(whitemax-whitemin)); // calculates seconds to next step
        while (countdown>0)
        {
          onesecond(); // updates clock once per second
          countdown--;
          relay2();
          relay1();
        }

      } 

      // fade blue LEDs out from max to min in increments of 1 point:
      for(int bluefadeval = bluemax; bluefadeval >= bluemin; bluefadeval --) // sets the minimum and maximum values
      { 
        analogWrite(blue, bluefadeval);
        lcd.setCursor(5, 1);
        lcd.print(33*bluefadeval/85);
        lcd.print(" "); 

        int countdown = ((blueramptime*60)/(bluemax-bluemin)); // calculates seconds to next step
        while (countdown>0)
        {
          onesecond(); // updates clock once per second
          countdown--;
          relay2();
          relay1();
        }
      }
    }
  }


}  // END LOOP

looks remarkably familiar, its almost as if someone ripped that code off my website, removed my name and email and made a couple minor changes in hopes of passing it off as their own
 
looks remarkably familiar, its almost as if someone ripped that code off my website, removed my name and email and made a couple minor changes in hopes of passing it off as their own

cptbjorn,

Im sorry if this was your code, no intention of stealing it, but no matter where i go, the codes are the same or similar, so i did not know that there is a patent on how codes are written? I apologize if thats the case, not my intention. Its been reviewed, cut, paste, critiqued by so many people, I dont even know what i have in there... (told you Im a novice)... Can you please point me to the source of this code so I can give credit to the person who did this wonderful code.

Just a note. I have no intention of calling it my own. Like I said, its a bunch of different codes cut and paste together since the original wont work on my original plan.
 
cptbjorn,

Im sorry if this was your code, no intention of stealing it, but no matter where i go, the codes are the same or similar, so i did not know that there is a patent on how codes are written? I apologize if thats the case, not my intention. Its been reviewed, cut, paste, critiqued by so many people, I dont even know what i have in there... (told you Im a novice)... Can you please point me to the source of this code so I can give credit to the person who did this wonderful code.

Just a note. I have no intention of calling it my own. Like I said, its a bunch of different codes cut and paste together since the original wont work on my original plan.

ahh don't worry about it I'm just giving you a hard time, I apologize for getting snappy about it. I posted a few different programs to a section of my website that I never publicly gave out links for and now I've seen them several times, usually with my info removed.

what mostly bugs me is that my half-finished work is circulating the internet and it makes me look sloppy because I didn't annotate and cite sources like I would for something I was planning to release. i dunno.
 
Whew! thats good news... I just wanted to share what I have learned and the least i want to do is offend someone... To be honest, that was the earlier tests, and it still did not work on my project. So its been dissected even more. The problem is, a lot of it I dont know, and I keep removing stuff i dont know and restart/recode the stuff i only know...

can i ask you question about the code???

Specially dealing with mean well 48D versions.... it does not translate well with arduino pwm...

If i may, here is the latest code i have been using for the past two weeks.

PHP:
#include "Wire.h" 
#define DS1307_I2C_ADDRESS 0x68 //set rtc
#include <LiquidCrystal.h> // initialize the library with the numbers of the interface pins



/*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  R E L A Y   P A R T  |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/
/*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  S I M P L E   O N   A N D   O F F   F E A T U R E |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/



const int ledPin1 =  2;          // pin number for relay 1
const int ledPin2 =  8;          // pin number for relay 2

int ledState1 = LOW;             // ledState used to set the RELAY
int ledState2 = LOW; 
long previousMillis1 = 0;        // will store last time RELAY was updated
long previousMillis2 = 0;
long interval1 = 30000;           // interval at which to blink (milliseconds)
long interval2 = 50000;



/*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  L E D   D I M M I N G   P A R T  |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/
/*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  F A D E S   I N   A N D   O U T  |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/



int blueramptime = 120 ;   // time for blue LEDs to dim on and off in minutes
int whiteramptime = 235 ;  // 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 = 10 ;    // amount of time array is on at full power in minutes
int ontime = 10 ;        // time of day (hour, 24h clock) to begin photoperiod fade in
int blue = 10;           // blue LEDs connected to digital pin 5 (pwm)
int white = 11;          // white LEDs connected to digital pin 6 (pwm)


int bluepercent[11] = { 0, 1, 2, 5, 8 ,12, 18, 27, 44, 80, 255 };
int whitepercent[11] = { 0, 1, 2, 3, 5, 8 ,14, 24, 42, 84, 255 };


// int pwm_one = 3;      // extra pwm pin for future use
// int pwm_one = 9;      // extra pwm pin for future use


LiquidCrystal lcd(12, 13, 4, 5, 6, 7); // typically 8, 9, 4, 5, 6, 7
                                       // have to change to free more pwm pins



/*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  R T C   C L O C K   D S 1 3 0 7  |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/



// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
  return ( (val/10*16) + (val%10) );
}

// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
  return ( (val/16*10) + (val%16) );
}

// 1) Sets the date and time on the ds1307
// 2) Starts the clock
// 3) Sets hour mode to 24 hour clock
// Assumes you're passing in valid numbers
void setDateDs1307(byte second, // 0-59
byte minute, // 0-59
byte hour, // 1-23
byte dayOfWeek, // 1-7
byte dayOfMonth, // 1-28/29/30/31
byte month, // 1-12
byte year) // 0-99
{
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  Wire.send(0);
  Wire.send(decToBcd(second)); // 0 to bit 7 starts the clock
  Wire.send(decToBcd(minute));
  Wire.send(decToBcd(hour));   // If you want 12 hour am/pm you need to set
  // bit 6 (also need to change readDateDs1307)
  Wire.send(decToBcd(dayOfWeek));
  Wire.send(decToBcd(dayOfMonth));
  Wire.send(decToBcd(month));
  Wire.send(decToBcd(year));
  Wire.endTransmission();
}

// Gets the date and time from the ds1307
void getDateDs1307(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
{
  // Reset the register pointer
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  Wire.send(0);
  Wire.endTransmission();

  Wire.requestFrom(DS1307_I2C_ADDRESS, 7);

  // A few of these need masks because certain bits are control bits
  *second = bcdToDec(Wire.receive() & 0x7f);
  *minute = bcdToDec(Wire.receive());
  *hour = bcdToDec(Wire.receive() & 0x3f); // Need to change this if 12 hour am/pm
  *dayOfWeek = bcdToDec(Wire.receive());
  *dayOfMonth = bcdToDec(Wire.receive());
  *month = bcdToDec(Wire.receive());
  *year = bcdToDec(Wire.receive());
}



/*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  D E F I N E  :  O N E S E C O N D |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/



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);
}




/*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  D E F I N E  :  R E L A Y 1 |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/




void relay1()  //FUNCTION TO TURN ON AND OFF RELAY 1.
{ 
  unsigned long currentMillis = millis();

  if(currentMillis - previousMillis1 > interval1) 
  { 
    previousMillis1 = currentMillis;   
    if (ledState1 == LOW)
      ledState1 = HIGH;
    else
      ledState1 = LOW;
    digitalWrite(ledPin1, ledState1);
  }
}



/*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  D E F I N E  :  R E L A Y 2 |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/




void relay2()
{
  unsigned long currentMillis2 = millis();

  if(currentMillis2 - previousMillis2 > interval2) 
  {
    previousMillis2 = currentMillis2;   
    if (ledState2 == LOW)
      ledState2 = HIGH;
    else
      ledState2 = LOW;
    digitalWrite(ledPin2, ledState2);
  }
}




/*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  S E T U P  |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/


void setup() {
  pinMode(ledPin1, OUTPUT);    // set the digital pin as output:
  pinMode(ledPin2, OUTPUT);    // set the digital pin as output:


  /*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  S E T U P - D I S P L A Y |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/



  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  Wire.begin();

  // Change these values to what you want to set your clock to.
  // You probably only want to set your clock once and then remove
  // the setDateDs1307 call.
  second = 50;
  minute = 46;
  hour = 22;
  dayOfWeek = 2;  // Sunday is 0
  dayOfMonth = 8;
  month = 2;
  year = 11;
  //setDateDs1307(second, minute, hour, dayOfWeek, dayOfMonth, month, year);

  analogWrite(blue, bluemin);
  analogWrite(white, whitemin);
  lcd.begin(16, 2); // set up the LCD's number of rows and columns: 
  //  lcd.print("12:00 80.6"); // Print a message to the LCD.
  //  lcd.print(char(223));
  lcd.setCursor(0, 1);
  lcd.print("blue:");
  lcd.print(33*bluemin/85);
  lcd.setCursor(8, 1);
  lcd.print("white:");
  lcd.print(33*whitemin/85);  
}




/*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  L O O P |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/




void loop()
{
  onesecond();
  relay2();
  relay1();





  /*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  L O O P - D I M   F U N C T I O N |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/

  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
  int daybyminute = ((hour * 60) + minute); //converts time of day to a single value in minutes
  

  /*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  L O O P - F A D E  I N |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/


 if (daybyminute >= (ontime*60))
  { 
    if (daybyminute <= ((ontime*60) + blueramptime + whiteramptime)) //if time is in range of fade in, start fading in
    {
      // fade blue LEDs in from min to max.
      for (int i = 1; i <= 10; i++) // setting i value for 10% increment. Start with 0% 
      { 
        analogWrite(blue, bluepercent[i]); 
        lcd.setCursor(5, 1);
        lcd.print(i);
        lcd.print(" "); 
      
        int countdown = ((blueramptime*60)/10); // calculates seconds to next step
        while (countdown>0)
          {
          onesecond(); // updates clock once per second
          countdown--;
          relay2();
          relay1();
        }
      }      

      // fade white LEDs in from min to max.
      for (int i = 1; i <= 10; i++) // setting i value for 10% increment. Start with 0%
      { 
        analogWrite(white, whitepercent[i]); 
        lcd.setCursor(14, 1);
        lcd.print(i);
        lcd.print(" "); 

        int countdown = ((whiteramptime*60)/10); // calculates seconds to next step
        while (countdown>0)
        {
          onesecond(); // updates clock once per second
          countdown--;
          relay2();
          relay1();
        }
      } 
    }
  }


  /*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  L O O P - M A X  V A L U E |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/



  if (((ontime * 60) + blueramptime + whiteramptime) < daybyminute) 
  { 
    if ( daybyminute < ((ontime * 60) + blueramptime + whiteramptime + photoperiod)) // if time is in range of photoperiod, turn lights on to maximum fade value
    {
      int i = 10; // setting blue and white to maximum value
      analogWrite(white, i); 
      analogWrite(blue, i);
    } 
  }



  /*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  L O O P - F A D E  O U T |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/



  if (((ontime * 60) + photoperiod + blueramptime + whiteramptime) <= daybyminute)
  { 
    if (((ontime * 60) + photoperiod + 2*blueramptime + whiteramptime) >= daybyminute)
    {
      // fade white LEDs out from max to min in increments of 1 point:
      for (int i = 10; i >= 0; i--) // setting i value for 10% increment. Start with 10%
      { 
        analogWrite(blue, 255);
        lcd.setCursor(5, 1);
        lcd.print(10);
        lcd.print(" "); 
        
        analogWrite(white, whitepercent[i]); 
        lcd.setCursor(14, 1);
        lcd.print(i);
        lcd.print(" ");  

        int countdown = ((whiteramptime*60)/10); // calculates seconds to next step
        while (countdown>0)
        {
          onesecond(); // updates clock once per second
          countdown--;
          relay2();
          relay1();
        }

      } 

      // fade blue LEDs out from max to min in increments of 1 point:
      for (int i = 10; i >= 0; i--) // setting i value for 10% increment. Start with 10%
      { 
        analogWrite(blue, bluepercent[i]);
        lcd.setCursor(5, 1);
        lcd.print(i);
        lcd.print(" "); 

        int countdown = ((blueramptime*60)/10); // calculates seconds to next step
        while (countdown>0)
        {
          onesecond(); // updates clock once per second
          countdown--;
          relay2();
          relay1();
        }
      }
    }
  }


}  // END LOOP


The problem I used to have, is what if for some reason you have to reset the arduino in the middle of the ramp up and down... it does not do anything... it will just wait for the next "if"

The way i understand it, I have three major "if's"
1) ramp up
2) full blast
3) ramp down

so if i reset it during ramp up, lets say 4 hours of ramp up. what happens is it will wait for the "full blast" and for the mean time, all my lights are off...

Thanks for your help.
 
yes, that is one issue with using the for() statement to do the fade in/out, you can't easily start halfway into one. the easiest workaround is just to not reset it during ramp times, I found that once I stopped working on my setup I just never need to reset it so i haven't bothered to fix it.

one possible fix would be to put a series of if statements in the setup where if the arduino is started/reset in the middle of a ramp time it will alter the ramp speed, so that it "catches up". for example if the fade in is 4 hours and it gets turned on 1 hour into the fade, the ramptime needs to be multiplied by a factor of (4-1)/4 or 3/4 to catch up. it shouldn't be that difficult to implement.

it is odd that you say it sits with the lights off until the full on time is reached though, it looks like it should just start the ramping late. I'll have to get a better look at it later tonight.
 
Rol, no problem... go to my website.

I have to change servers.... and I cannot go back on this thread and edit the images... I wish I can... but.... I have everything in my website. Also if you have questions... dont be a stranger and just ask here on this thread...

btw, WELCOME, WELCOME, WELCOME. If we havent greeted you yet.....

Also, on my website, there are other links there than might help you too.... so just browse around on the menu...

Hope this helps.....
 
Hi Katchupoy,

Thanks for the welcome and info. Going to have a look-see at your site now.
I think it's a matter of getting ones head around the basics, then it all falls into place.
Just completed a 72 LED diy build, biggest project to date, but really satisfying.
Cheers, Rol


Rol, no problem... go to my website.

I have to change servers.... and I cannot go back on this thread and edit the images... I wish I can... but.... I have everything in my website. Also if you have questions... dont be a stranger and just ask here on this thread...

btw, WELCOME, WELCOME, WELCOME. If we havent greeted you yet.....

Also, on my website, there are other links there than might help you too.... so just browse around on the menu...

Hope this helps.....
 
Back
Top