Arduino sketches

Does this make any sense to add to the if?
Code:
""if (mins > start + period && mins <= start + period - fade)  {
    analogWrite(randPin, randNumber)
    delay (randTime);""
int randPin = random(1-8); //I know not exact pins, Ill have to look at my Mega and shield to get exact
int randTime = random (1500, 3500);
int randDim = random (85, 255) //Random dimming from 33% to 100%


Ill have to change the mins equation to figure out what time to run it at. How will the delay time mess with the RTC time if any?

Delaying won't mess with the RTC - but your variables in the sketch will be inaccurate after any delays. You could refresh them. Or, you could not use delays to do the clouds - instead, for a certain period of the day (have your IF statement test if you're in that period, on certain days, whatever), run the same setLed functions, but pass a different max value, etc. This will have the same effect but not "lock" you up running delays.

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?

That looks OK, try it out! The fadeDuration is in minutes - so as it is set now (60) it'll take an hour to go from the min to the max at the beginning third of the day, and an hour to go back down at night.

One other thing - it looks like in the "night" mode you are setting some of the banks to 100. Since your fading routine will take all the banks down to zero at the last minute of the day, those banks will suddenly jump up to 100 when night hits.
 
That looks OK, try it out!
I'm waiting on my shield. :wavehand: (Just messing with ya. I know you are a busy man.)

One other thing - it looks like in the "night" mode you are setting some of the banks to 100. Since your fading routine will take all the banks down to zero at the last minute of the day, those banks will suddenly jump up to 100 when night hits.
I didn't notice that. I just wanted the whites on as a kinda moon light. I want to do the moon phasing stuff later on. I just threw the 100's in there off the top of my head.
 
Thanks for the very detailed explanation.

How can I add LCD display command to the sketches?
Thanks

Depends on how you wire it up and what you want it to do. If you wire directly to the Arduino in parallel, then there are good libraries developed:

http://www.arduino.cc/en/Reference/LiquidCrystal

If you use some sort of serial chip or other interface, you're kind of on your own to find a library, though most vendors provide them and/or the hobby community has probably written one if you pick a popular interface.

That'll just get you control over the LCD. From there it depends on what you want to display.



I'm waiting on my shield. :wavehand: (Just messing with ya. I know you are a busy man.)

Yeah, yeah! :lol:

I didn't notice that. I just wanted the whites on as a kinda moon light. I want to do the moon phasing stuff later on. I just threw the 100's in there off the top of my head.

You could use the setLed functions, or create a moonlight function, and basically do the same thing at night as you're doing during the day. It could even check the day of the month and vary the moonlight intensity to mimic moon phases.

Another cool extension would be checking the day of the year and varying the start/stop times to mimic day length changes.
 
Thanks for the very detailed explanation.

How can I add LCD display command to the sketches?
Thanks

It not hard, but its not exactly easy either. It all depends on what you want to actually do with it. It may be as easy as hooking it up, defining the pins that connect the LCD, then some simple
Code:
lcd.print("hello, world!;

As far as actually in putting and controlling the Arduino. That's alot more involved and I'm actually working on that, but haven't posted anything. There's also a DIY controller thread that I think plans to use an LCD.
 
You could use the setLed functions, or create a moonlight function, and basically do the same thing at night as you're doing during the day. It could even check the day of the month and vary the moonlight intensity to mimic moon phases.

Another cool extension would be checking the day of the year and varying the start/stop times to mimic day length changes.

Ill work on creating a moonlight function today. I took (failed) my finals yesterday so i don't have anything to do.
 
Someone asked me about this sketch today:



There are all sorts of problems in it and it's got some obscurities. Here it is cleaned up a bit. Plus added some comments near the section that you should change to alter the schedule and behavior of the dimming.

Code:
/*
 Arduino LED controller for reef aquariums

// paramterized LED lighting control for reef aquariums
// use a DS1307 RTC to kick off lighting schedules
//   fade up on a given slope
//         delay a set amount
//         fade back down
//   such that the photoperiod lasts the correct amount of time


//  Circuit
// 
// PWM pins described below connected to dimming circuits on drivers
// ledMaster pin below connected to a 120V AC relay to turn the LED drivers on and off (optional)
// grounds from drivers connected to arduino ground
// DS1307 RTC connected via I2C
//
//
//

*/

// Pins to control LEDs. Change these if you're using different pins.
int blueLed = 9;     // LED PWM channel for blues
int whiteLed = 10;   // LED PWM channel for whites

// 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 behavoir
int minCounter = 0;         // counter that resets at midnight. Don't change this.
int blueStartMins = 480;    // minute to start blues. Change this to the number of minutes past
                            //    midnight you want the blues to start.
int whiteStartMins = 480;   // minute to start whites. Same as above.
int bluePhotoPeriod = 510;  // photoperiod in minutes, blues. Change this to alter the total
                            //    photoperiod for blues.
int whitePhotoPeriod = 510; // photoperiod in minutes, whites. Same as above.
int fadeDuration = 60;      // duration of the fade on and off for sunrise and sunset. Change
                            //    this to alter how long the fade lasts.
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 > blueStartMins || minCounter > whiteStartMins)
           && (minCounter < blueStartMins + bluePhotoPeriod || minCounter < whiteStartMins + whitePhotoPeriod))  {   //day
    // set LED states
    setLed(minCounter, blueLed, blueStartMins, bluePhotoPeriod, fadeDuration, blueMax);
    setLed(minCounter, whiteLed, whiteStartMins, whitePhotoPeriod, fadeDuration, whiteMax);
  }
  else  {   //night
    analogWrite(blueLed, 0);
    analogWrite(whiteLed, 0);
  }
    
  // Get ready for next iteration of loop
  delay(1000);
}

Which pin is the LedMaster - controlling relay, assigned to?
Thanks
 
What kind of drivers are you guys using to run the leds? I kind of understand the programming and the clouds but I am concerned about only having 24 LEDS and two menawells. Can this effect be seen with so little amounts of LEDS? Can I turn on and off single LED in a string off 12? Thats my dum question for the day, LOL....

Thanks for the lead Swingrrrr... Jeff
 
What kind of drivers are you guys using to run the leds? I kind of understand the programming and the clouds but I am concerned about only having 24 LEDS and two menawells. Can this effect be seen with so little amounts of LEDS? Can I turn on and off single LED in a string off 12? Thats my dum question for the day, LOL....

Thanks for the lead Swingrrrr... Jeff

I am running the "P" drivers. I throught there was some rumor going around about the D being run on an Arduino, but with so many LED threads I lost the follow-up. It was/is my understanding that you want the "P" with the Arduinos and the "D" with pots or Apex/RK/Etc.
You cant turn off a single LED in a string of 12. You will get a little of the effect with your setup, just not a left to right, or "running clouds" so to speak. I am using the random code and I think it looks awesome. Maybe some locals that have seen it run will chime in.
 
Last edited:
I have seen the cloud program on Swingrrr's tank and is way cool. It's making me think about selling my "D"s to get "P" drivers if we can't get mine to run clouds.
 
FWIW One of the people using my "ELN shield" told me they'd run it on both styles of ELN, so I'd suppose it works.
Have any extra shields? Are these the prototypes you were selling for cost of shipping? Shot me a PM if you have any. Thank you again for your help, the sites and recommendations you sent me a while back came in handy ... Jeff
 
Have any extra shields? Are these the prototypes you were selling for cost of shipping? Shot me a PM if you have any. Thank you again for your help, the sites and recommendations you sent me a while back came in handy ... Jeff
Dont think he is ignoring you. Last I heard he got called away to D.C. Opps, Ive probably said to much.


can someone help me with some code? I will be using 6 pins, 2 for blue, and 4 for white integrated with a rtc.

Sure. What are you wanting to do?
 
Dont think he is ignoring you. Last I heard he got called away to D.C. Opps, Ive probably said to much.

Yea we have missed him on the led mother thread. especially now that soundwave has showed back up.




SWINGRRRR;17184700Sure. What are you wanting to do?[/QUOTE said:
Ok let me try to explain what I want.

I want the blues to come on at a set time and ramp up stay on for as set time then go off. they will be on two channels.

I will have four channels of white

i want them to come on staggered say 30 minutes apart. ramp up , stay on for a set time, then ramp down in the same order.

I would also like to have storm added, but not necessary at this time. would just be fun to play with.

my only real concern is if the power goes out I need the lights to come back on. I'm not sure how to add a check for that. I know I can use the rtc, but not sure how.
 
Ok let me try to explain what I want.
I want the blues to come on at a set time and ramp up stay on for as set time then go off. they will be on two channels.
I will have four channels of white
i want them to come on staggered say 30 minutes apart. ramp up , stay on for a set time, then ramp down in the same order.
I would also like to have storm added, but not necessary at this time. would just be fun to play with.
my only real concern is if the power goes out I need the lights to come back on. I'm not sure how to add a check for that. I know I can use the rtc, but not sure how.
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.
 
Back
Top