Arduino sketches

Sound pretty much like what I wanted. I posted a sketch on page one that will do what you want, you'd just have to adapt it to your needs.

Not at all trying to sound condescending, but do you have any experience programming? You need to know what all to point out or not.

Ihave very little progaming experience. The final sketch would be fine, I am just concerned about power outages. they are commom where I live. I don't know where to begin writing the code to check time and verify pin position. I know I can use an if statement, just not sure how to write it. sorry for my ignorance.
 
Ihave very little progaming experience. The final sketch would be fine, I am just concerned about power outages. they are commom where I live. I don't know where to begin writing the code to check time and verify pin position. I know I can use an if statement, just not sure how to write it. sorry for my ignorance.

No, No, No. We all had to start somewhere. I just didnt want to throw alot of stuff out that you didn't know what I was talking about.
The RTC can take care of the power outage. What most of us use is a DS1307, they are about $10 or so. It has an internal clock that the arduino can draw from. You'll have to include the statements that allows the arduino to draw the time. Ive set mine up like DZW suggested, convert every minute of the day to a number. 0=Midnight, 60=1am, 120=2am, etc etc. Then when you tell the arduino to turn ON at 420, its knows to turn ON at 7am.

When you assign the pins, its completely up to you.

Trust me dude, it took me weeks and weeks of reading and just playing with it to get it to work.
We are here to help with any questions.
 
No, No, No. We all had to start somewhere. I just didnt want to throw alot of stuff out that you didn't know what I was talking about.
The RTC can take care of the power outage. What most of us use is a DS1307, they are about $10 or so. It has an internal clock that the arduino can draw from. You'll have to include the statements that allows the arduino to draw the time. Ive set mine up like DZW suggested, convert every minute of the day to a number. 0=Midnight, 60=1am, 120=2am, etc etc. Then when you tell the arduino to turn ON at 420, its knows to turn ON at 7am.

When you assign the pins, its completely up to you.

Trust me dude, it took me weeks and weeks of reading and just playing with it to get it to work.
We are here to help with any questions.

ok maybe I know more than I give myself credit for, or confident enough to admit. This is what I have so far. I have my dc1307 hooked upon 12c, and accuratly reporting time. And I have my led pins assigned. I can turn them on with a wright command. I can change the coad to dim them. That's all I could figure out on my own. I can understand, and use most of the code that I read, but weighting my own has not gone so well. My real concern is if I set my LEDs to come on at say 4:30, and the power goes out, would I just use a statement like if rtc returns a time range say between 4:30 and9:30,verify pin status, and if low set to high. I'm pretty sure I understand the. Principal, I just don't know how to code it.
 
I am no expert, but it will check the time, and then do what is appropriate, no matter what time the power goes out, or comes back on, it will start at the correct time and the correct function.
That is the part of the main loop, the If (mincounter > bluestartmins) means it will check the time (mincounter) and if its greater than the time to start the blues(bluestartmins) it will do whatever state you tell it.

You could use the set LOW/HIGH thing, but thats only going to be ON/OFF. No dimming.

Sounds like you have it figued out though. Watch, soon you'll be answering the questions in this thread!!
 
I am no expert, but it will check the time, and then do what is appropriate, no matter what time the power goes out, or comes back on, it will start at the correct time and the correct function.
That is the part of the main loop, the If (mincounter > bluestartmins) means it will check the time (mincounter) and if its greater than the time to start the blues(bluestartmins) it will do whatever state you tell it.

You could use the set LOW/HIGH thing, but thats only going to be ON/OFF. No dimming.

Sounds like you have it figued out though. Watch, soon you'll be answering the questions in this thread!!


ahhh i missed that part. see learn something new everyday.
 
Can someone point me to a thread were the hardware part is all figured out or might give some hints? I picked up a DS1307 clock and some dimmable buckpucks in a attempt to figure this stuff out in baby steps then move up to meanwells and more LEDs. The more I read the more confused I get. I couldn't even get the stupid arduino to output time on a LCD :lmao:. Thanks...Jeff
 
OK, can y'all look over this. This is all 8 channels. You can add or delete as you need. Again, I am using the Mega, so YMMV. I got it to compile so I guess its something. I think I need to change the fade duration, but I am not sure with the 15 minute stagger. I know they are out of order, but thats for a reason so it will rise E->W and reverse.
The next thing Id like to add is a callDayofWeek. Im thinking maybe clouds on even days, full on the next. That way I don't have to figure out how to split the day up even more.
EDIT: Looking back over I think I need to lower the fadeDuration. Otherwise wont it take 8 hours just to fade up?
Code:
/*
//This was taken from DZW post above. I give credit to him, I just added and changed a couple of things. 

// Pins to control LEDs. Change these if you're using different pins.
int blue1Led = 3;     // LED PWM channel for blues
int blue2Led = 4;
int blue3Led = 5;
int blue4Led = 6;
int white1Led = 11;   // LED PWM channel for whites
int white2Led = 12; 
int white3Led = 13;
int white4Led = 14;

// Set up RTC
#include "Wire.h"
#define DS1307_I2C_ADDRESS 0x68

// RTC variables
byte second, rtcMins, oldMins, rtcHrs, oldHrs, dayOfWeek, dayOfMonth, month, year;

// Other variables. These control the behavior of lighting. Change these to customize behavior
int minCounter = 0;         // counter that resets at midnight. Don't change this.
int blue1StartMins = 480;    
int blue2StartMins = 495;
int blue3StartMins = 525;
int blue4StartMins = 555;    
int white1StartMins = 510;       
int white2StartMins = 540;
int white3StartMins = 570;
int white4StartMins = 585;   

int bluePhotoPeriod = 510;  
int whitePhotoPeriod = 510; // photoperiod in minutes, whites. Same as above.
int fadeDuration = 60;      // duration of the fade on and off for sunrise and sunset. 
int blueMax = 255;          // max intensity for blues. Change if you want to limit max intensity.
int whiteMax = 255;         // max intensity for whites. Same as above.

/****** LED Functions ******/
/***************************/
//function to set LED brightness according to time of day
//function has three equal phases - ramp up, hold, and ramp down
void setLed(int mins,    // current time in minutes
            int ledPin,  // pin for this channel of LEDs
            int start,   // start time for this channel of LEDs
            int period,  // photoperiod for this channel of LEDs
            int fade,    // fade duration for this channel of LEDs
            int ledMax   // max value for this channel
            )  {
  if (mins > start && mins <= start + fade)  {
    analogWrite(ledPin, map(mins - start, 0, fade, 0, ledMax));
  }
    if (mins > start + period && mins <= start + period - fade)  {
    analogWrite(ledPin, ledMax);
  }
    if (mins > start + period - fade && mins <= start + period)  {
    analogWrite(ledPin, map(mins - start + period - fade, 0, fade, ledMax, 0));
  }
}

/***** RTC Functions *******/
/***************************/
// 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));
   Wire.send(decToBcd(minute));
   Wire.send(decToBcd(hour));
   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)
{
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  Wire.send(0);
  Wire.endTransmission();

  Wire.requestFrom(DS1307_I2C_ADDRESS, 7);

  *second     = bcdToDec(Wire.receive() & 0x7f);
  *minute     = bcdToDec(Wire.receive());
  *hour       = bcdToDec(Wire.receive() & 0x3f);
  *dayOfWeek  = bcdToDec(Wire.receive());
  *dayOfMonth = bcdToDec(Wire.receive());
  *month      = bcdToDec(Wire.receive());
  *year       = bcdToDec(Wire.receive());
}

void setup()  { 
  
// init I2C  
  Wire.begin();
} 

/***** Main Loop ***********/
/***************************/
void loop()  { 
  // get time from RTC and put in hrs and mins variables
  getDateDs1307(&second, &rtcMins, &rtcHrs, &dayOfWeek, &dayOfMonth, &month, &year);
  minCounter = rtcHrs * 60 + rtcMins;

  // determine if it is day or night, and act accordingly
  if ((minCounter > blue1StartMins || minCounter > white1StartMins)
           && (minCounter < blue1StartMins + bluePhotoPeriod || minCounter < white1StartMins + whitePhotoPeriod))  {   //day
    // set LED states
    setLed(minCounter, blue1Led, blue1StartMins, bluePhotoPeriod, fadeDuration, blueMax);
    setLed(minCounter, white1Led, white1StartMins, whitePhotoPeriod, fadeDuration, whiteMax);
    setLed(minCounter, blue2Led, blue2StartMins, bluePhotoPeriod, fadeDuration, blueMax);
    setLed(minCounter, white2Led, white2StartMins, whitePhotoPeriod, fadeDuration, whiteMax);
    setLed(minCounter, blue3Led, blue3StartMins, bluePhotoPeriod, fadeDuration, blueMax);
    setLed(minCounter, white3Led, white3StartMins, whitePhotoPeriod, fadeDuration, whiteMax);
    setLed(minCounter, blue4Led, blue4StartMins, bluePhotoPeriod, fadeDuration, blueMax);
    setLed(minCounter, white4Led, white4StartMins, whitePhotoPeriod, fadeDuration, whiteMax);
}
  else  {   //night
    analogWrite(blue1Led, 0);
    analogWrite(white1Led, 100);
    analogWrite(blue2Led, 0);
    analogWrite(white2Led, 100);
    analogWrite(blue3Led, 0);
    analogWrite(white3Led, 100);
    analogWrite(blue4Led, 0);
    analogWrite(white4Led, 100);
  }
    
  // Get ready for next iteration of loop
  delay(1000);
}

when I try to compile i get this




In function 'void setDateDs1307(byte, byte, byte, byte, byte, byte, byte)':
error: 'Wire' was not declared in this scope In function 'void getDateDs1307(byte*, byte*, byte*, byte*, byte*, byte*, byte*)':
In function 'void setup()':
In function 'void loop()':

what am i doing wrong?
 
Can someone point me to a thread were the hardware part is all figured out or might give some hints? I picked up a DS1307 clock and some dimmable buckpucks in a attempt to figure this stuff out in baby steps then move up to meanwells and more LEDs. The more I read the more confused I get. I couldn't even get the stupid arduino to output time on a LCD :lmao:. Thanks...Jeff
Im not sure, but I know of no way that an Arduino can control the buckpucks, but I am no expert. I have no idea how the buckpucks work.

Most people use the "P" MW's with the arduino. There are reports of the "D"s working, but I have no experince with them. The "P" are designed for a PWM input on the dim circuit, thats why they were choosen.
This set-up is the most basic to me at least. To have full automation and control of the LEDs you just need a arduino that will control the MW that will drive the LEDs. Really only 3 main componets.
Im not sure if that answers your question. LMK if I can help more.
 
I am Using a Duemilanov, and I'm pretty sure I have the wire library instally. It has to be for my clock to work on 12c dosen't it?

Yeah. You have to have it in your arduino library folder. It kinda pulls it out when it loads the sketch and it rides along with it. Again, I am not the expert, but thats the way I understood things.
 
Yeah. You have to have it in your arduino library folder. It kinda pulls it out when it loads the sketch and it rides along with it. Again, I am not the expert, but thats the way I understood things.

ok had the library installed, and realized that you can't start the program from a shortcut, or it doesn't recognize the libraries.
 
just got my fedex from mouser. about to build another rtc. i think i fried the other one. other than that the scrip loads fine.
 
OK. Cool. How much did the RTC run you. I think the lowest I have seen is Futurlec for $7.90

I built my own on a proto board. cost me about $3 in parts plus the board i had laying around. I could have just done it on perf board if i didn't feel like wasting a proto board.
 
I am starting this thread due to the fact that some of us have the hardware side of the LEDs and dimmers down. Now we really need to focus on the software. So please, hopefully some people with more experience will join in. Here are a couple of my sketches.

Random clouds: This one worked really well and is my favorite so far. The jumps happen so fast that its not huge "edges."
Code:
int led1Pin = 3;    // LED connected to digital pin 9
int led2Pin = 5;
int led3Pin = 6;
int led4Pin = 9;    // LED connected to digital pin 9
int led5Pin = 10;
int led6Pin = 11;
void setup(){}
void loop(){ 
int randNumber = random(100, 255);
int randTime = random (750, 1500);
analogWrite(led1Pin, randNumber); 
delay (randTime); 
analogWrite(led2Pin, randNumber); 
delay (randTime);  
analogWrite(led3Pin, randNumber); 
delay (randTime);  
analogWrite(led4Pin, randNumber); 
delay (randTime); 
analogWrite(led5Pin, randNumber); 
delay (randTime);  
}

Oh how fun! After about 15 minutes with my new board I have this one working on a bread board with leds on pins 3, 5, 6, &9. Now if I were only home and could get my dimming shield connected... Seems pretty easy so far!
 
Last edited:
There no plalce like home. tap
There no plalce like home. tap
There no plalce like home. tap
Got your ruby slippers?

Is it funny where the mind goes sometimes? Me I lost mine years ago :)
 
Nice... never though bout the slippers, not sure it's a good idea to use them at work though... 12 hours down, 24 to go:hmm4:

Just a thought here and maybe someone can tell me why I'm wrong... I'd like to know why the dimming shield is absolutely necessary, Like I said, I won't be able to check it out until tomorrow night, but if I can adjust my max current at the Arduino's max 5V output do I really need the dimming shield? If I can achieve my max power @ 5V, then dim it all the way what makes the difference? The catch is I want the blues @ ~.7A, and the whites @ ~.5A the problem I foresee is getting .7A at the 5V setting.

The reason I bring this up is after playing with it for a couple hours, is that I wonder if it's almost an "off the shelf" dimming solution for lower current arrays.

Tim
 
Hllywd,

Assuming you have the meanwells that can generate 1.3 amps. The sheild could probably be skipped for your configuration.

1.3 * 10 / 5 = 6.5 amps would be the maximum current you could get
 
Back
Top