Help with LED sketch

I'm sorry if this is in the wrong spot, but Im new here. I am looking for help with a sketch for my LED array to go over a fish tank. What I would like:
9-11am Dim up E to W
11-1pm Random changing of intensity, (clouds)
1-4pm Full power lights
4-7pm more clouds
7-10pm dim back down
Also, I would like a pushbutton to be able to go off, whites only, blues only, full on, and back to program.
I am having 2 problems really. I plan to use a DS1307 RTC to set the time for the dimming and all. If I push the button and cycle through the different lights, how can I come back to the program at the specific time it was when I left it? Also, I can't figure out how to get random duty cycles to show up for the clouds portion.
I have read a lot, but am still learning. Any help is much appreciated.
I am using a Seeeduino Mega, if it matters, and I have 8 channels of LEDs, some with 10 LED per channel, some with 11 per.

That's a good overall plan.... now try to break it into "bite sized" pieces, and develop them separately.

I'd start with three toggle switches standing in for the timer. And a function to read the three switches, and return a number, 0-7, depending on the combination of the switches being on and off. Call it SwitchState

Then the "loop" function can be...

bTmp=SwitchState;
if bTmp=0 {AllLEDsOn};
if bTmp=7 {AllOff};
if bTmp=1 {DimToBright};
if bTmp=2 {RandomClouds};
... etc.

Yes, there are "clever" ways to do that... but the above is more clear for neophytes, isn't it?!)

You then write routines to accomplish the different requirements, e.g. a "AllLEDsOn", "AllOff", etc.

It will probably be best to start with requirements for static conditions. Once you can give a command for a particular static condition, you can build "scripts" to accomplish your dynamic scenarios out of the already created static elements.

Once that's working, converting it to have the different states triggered in sync with what the RTC chip is reporting isn't as big a task.

The pushbutton would be used to change states in a state machine.

http://www.arduino.cc/playground/Code/FiniteStateMachine

You could have a state for off, one for white, one for blue, and one for timed.

If you (re)enter the timed state, you should, I think, (re)enter it based on now, not on when you left the state.

If the timed state is running, it's 10 am, and you push the button to switch to the white state (for maintenance, maybe), and three hours later, you finish and push the button again to get to the timed state, you would want it to resume at 1 pm, not 10 am.

For the random intensity, seed the random number generator in setup (using the RTC?). Then, choose a random number in some range. There is some light even when there are dark black clouds everywhere, so the minimum value should be greater than 0.

Here's what I have so far. Remember, I am new. :wink:
Can anyone tell me how to use the random code? I just need one line or so, and I can figure it out from there. I can don't know how to get it started, but I have read the tutorial several times. Also, yes, I do want to place upper and lower limits on the RNG. I think I have the dim in protion understood, just have to get it on papaer. I may have some questions on the RTC though. After that is the clouds so thats where I need the most help. But yes you are correct in what I was looking for. Say I leave it at 10a and come back at 1pm, then yes, I want it to resume at 1pm.
Thanks for all your help.

int switchPin = 2; // switch is connected to pin 2
const int led1Pin = 13; // White 1 LED, etc, etc
const int led2Pin = 12;
const int led3Pin = 11;
const int led4Pin = 10;
const int led5Pin = 09;
const int led6Pin = 08;

int val; // variable for reading the pin status
int val2; // variable for reading the delayed status
int buttonState; // variable to hold the button state

int lightMode = 0; // What mode is the light in?

void setup() {
pinMode(switchPin, INPUT); // Set the switch pin as input
pinMode(led1Pin, OUTPUT); // LEDs to output
pinMode(led2Pin, OUTPUT);
pinMode(led3Pin, OUTPUT);
pinMode(led4Pin, OUTPUT);
pinMode(led5Pin, OUTPUT);
pinMode(led6Pin, OUTPUT);

Serial.begin(9600); // Set up serial communication at 9600bps
buttonState = digitalRead(switchPin); // read the initial state
}

void loop(){
val = digitalRead(switchPin); // read input value and store it in val
delay(10); // 10 milliseconds is a good amount of time
val2 = digitalRead(switchPin); // read the input again to check for bounces
if (val == val2) { // make sure we got 2 consistent readings!
if (val != buttonState) { // the button state has changed!
if (val == LOW) { // check if the button is pressed
if (lightMode == 0) { // if its off
lightMode = 1; // turn whites on
} else {
if (lightMode == 1) { // if whites are on
lightMode = 2; // then tuen blues on
} else {
if (lightMode == 2) { // if blues are on
lightMode = 3; // make then turn all on
} else {
if (lightMode == 3) { // if all are on
lightMode = 0; // turn light off!
}
}
}
}
}
}
buttonState = val; // save the new state in our variable
}

// Now do whatever the lightMode indicates
if (lightMode == 3) { // All off
digitalWrite(led1Pin, LOW);
digitalWrite(led2Pin, LOW);
digitalWrite(led3Pin, LOW);
digitalWrite(led4Pin, LOW);
digitalWrite(led5Pin, LOW);
digitalWrite(led6Pin, LOW);
}

if (lightMode == 1) { // Whites on
digitalWrite(led1Pin, HIGH);
digitalWrite(led2Pin, HIGH);
digitalWrite(led3Pin, HIGH);
digitalWrite(led4Pin, LOW);
digitalWrite(led5Pin, LOW);
digitalWrite(led6Pin, LOW);
}

if (lightMode == 2) { // Blues on
digitalWrite(led1Pin, LOW);
digitalWrite(led2Pin, LOW);
digitalWrite(led3Pin, LOW);
digitalWrite(led3Pin, HIGH);
digitalWrite(led4Pin, HIGH);
digitalWrite(led5Pin, HIGH);

}
if (lightMode == 0) { // All On
digitalWrite(led1Pin, HIGH);
digitalWrite(led2Pin, HIGH);
digitalWrite(led3Pin, HIGH);
digitalWrite(led4Pin, HIGH);
digitalWrite(led5Pin, HIGH);
digitalWrite(led6Pin, HIGH);

}

I forgot to add, I plan to add one more lightmode == 4. This will be the Day program.

I think I am jumping around a little, but will this random work:
randNumber = random(128, 256); //Create a random number from 128to255
analogWrite(led1Pin, randNumber); //led1pin will go to that level
delay (15000); // wait for 15 seconds and go to another one
I dont have the brackets and all that, but its just what I came up with off the top of my head.
Also, I think this will cuase a sharp change, like from low dim to high dim without a real fade up and down. I may scrap this whole random idea and just have them fade up and down for the clouds.

if (lightMode == 0) {          // if its off
         lightMode = 1;               // turn whites on
       } else {
         if (lightMode == 1) {        // if whites are on
           lightMode = 2;             // then tuen blues on
         } else {
           if (lightMode == 2) {      // if blues are on
             lightMode = 3;           // make then turn all on
           } else {
        if (lightMode == 3) {     //  if all are on
               lightMode = 0;         // turn light off!
             }
                 }

Take a look at the switch statement. It will make this code much easier to read, and shorter, too.

You're on the right track with the random number stuff.

Another suggestion would be to look at using an array to hold the LED pin numbers. If you did that, you could use a for loop to turn the LEDs off or on.

I was trying to take bits and pieces from the timer thread but its not working? I keep getting a "ERROR: Alarm was not declared in this scope." when I try to complie. I cant figure it out. Is it because the code includes the addition of "#include <Time.h>
#include <TimeAlarms.h>" I took them out because it wouldnt complie past that at first. (Im using compile to check my work, unless someone has a better idea.) My first couple complied with no problem. But I started taking out the stuff that didnt work, and this is what I was left with.
int switchPin = 2; // switch is connected to pin 2
const int led1Pin = 13; // White 1 LED, etc, etc
const int led2Pin = 12;
const int led3Pin = 11;
const int led4Pin = 10;
const int led5Pin = 9;
const int led6Pin = 8;

int val; // variable for reading the pin status
int val2; // variable for reading the delayed status
int buttonState; // variable to hold the button state

void setup() {
int lightMode = 0; // What mode is the light in?

pinMode(switchPin, INPUT); // Set the switch pin as input
pinMode(led1Pin, OUTPUT); // LEDs to output
pinMode(led2Pin, OUTPUT);
pinMode(led3Pin, OUTPUT);
pinMode(led4Pin, OUTPUT);
pinMode(led5Pin, OUTPUT);
pinMode(led6Pin, OUTPUT);

Alarm.alarmRepeat(11,00,0, morningAlarm); // Sun starts to rise
Alarm.alarmRepeat(13,00,0, amcloudsAlarm); // Morning clouds
Alarm.alarmRepeat(15,00,0, noonAlarm); // Full Noon
Alarm.alarmRepeat(17,00,0, pmcloudsAlarm); // Evening clouds
Alarm.alarmRepeat(18,30,0, eveningAlarm); // Sun starts to set
Alarm.alarmRepeat(20,00,0, moonAlarm); // Moon cycle
}

Serial.begin(9600); // Set up serial communication at 9600bps
buttonState = digitalRead(switchPin); // read the initial state
}

void loop()
{
void morningAlarm(){
for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5)
// fade in from min to max in increments of 5 points:
// sets the value (range from 0 to 255):
analogWrite(led1Pin, fadeValue);
delay(84000);
// Delay for 14 minutes. I think my math is right. 51 steps, going up every 14 minutes will get me full light after 3 hours.
{
if (lightMode == 0) { //its off
lightMode = 1; // turn whites on
} else {
if (lightMode == 1) { // if whites are on
lightMode = 2; // then tuen blues on
} else {
if (lightMode == 2) { // if blues are on
lightMode = 3; // make then turn all on
} else {
if (lightMode == 3) { // if all are on
lightMode = 0; // turn light off!
}
}
buttonState = val; // save the new state in our variable
}

// Now do whatever the lightMode indicates
if (lightMode == 3) { // All off
digitalWrite(led1Pin, LOW);
digitalWrite(led2Pin, LOW);
digitalWrite(led3Pin, LOW);
digitalWrite(led4Pin, LOW);
digitalWrite(led5Pin, LOW);
digitalWrite(led6Pin, LOW);
}

if (lightMode == 1) { // Whites on
digitalWrite(led1Pin, HIGH);
digitalWrite(led2Pin, HIGH);
digitalWrite(led3Pin, HIGH);
digitalWrite(led4Pin, LOW);
digitalWrite(led5Pin, LOW);
digitalWrite(led6Pin, LOW);
}

if (lightMode == 2) { // Blues on
digitalWrite(led1Pin, LOW);
digitalWrite(led2Pin, LOW);
digitalWrite(led3Pin, LOW);
digitalWrite(led3Pin, HIGH);
digitalWrite(led4Pin, HIGH);
digitalWrite(led5Pin, HIGH);
}
if (lightMode == 0) { // All On
digitalWrite(led1Pin, HIGH);
digitalWrite(led2Pin, HIGH);
digitalWrite(led3Pin, HIGH);
digitalWrite(led4Pin, HIGH);
digitalWrite(led5Pin, HIGH);
digitalWrite(led6Pin, HIGH);
}

Also, how do yall put the code in those boxes?

Also, I dont have the morning dimming done correctly. I need away to delay the other dimming up. I guess I need some kind of loop, but I dont know how to do it.
I wanna keep the same steps, +10 every 14 minutes, but I want each channel to be staggered.
Blue=B and the channel # and W=White
B1>36 minutes later B2/W1 come up at same time> 36 minutes later B3/W2 etc...>etc...B4/W3 etc...>Finally W4

To put the code in the gray boxes (which is the way we prefer that you post it), click the # button on the top row of buttons (above where you type to post a message. Then, paste the code.

Or, paste the code, then select it all, then press the # button.

If you code fails to compile with the #include <Time.h> and #include <TimeAlarm.h> statements in it, it's a pretty good bet that you didn't install the Time and TimeAlarm libraries, or didn't install them in the right place.

As for making things happen at the right time, there are two ways to make that happen. One is to set a timer, and do nothing until the timer goes off. That's what delay does.

The other is to check your watch occasionally, and see if it's time to do something. Calling millis() is how you check your watch on the Arduino.

The millis function returns a value that indicates how long it has been since the Arduino was reset.

Look at the Blink Without Delay example for an example of how to get rid of delay.

84000 milliseconds is 1.4 minutes, by the way, not 14 minutes.

I've read your last two posts 3 times now, and I'm still not sure what problem(s) you are having now. Can you try (again) to explain?

I'm sorry. My mind is going everywhere trying to get this done. I've got 2 things checked off my list so far, I think.
9-11am Dim up E to W ***Done. (I think)
11-1pm Random changing of intensity, (clouds)
1-4pm Full power lights *** Can do, just gotta get the timing
4-7pm more clouds
7-10pm dim back down *** Done. Just reverse 9-11am
Also, I would like a pushbutton to be able to go off, whites only, blues only, full on, and back to program.***Done pretty much. Just got to figure the timing thing.

I'm trying to get the LEDs to start dimming on the left side, and slowly ramp up to the right side. Here's what I can up with so far, I just gotta change the delay. I also want to only run this from 9-11am and then from 7-10pm. That's what I meant by B1 LED> then B2 and W1 LED so on so on. I think I've got it figured out. I followed the 3 LED cross fade project. I gotta add more states, but pretty much at this point its repetition.
The pushbutton part is posted above and here is the 9-11am ramp up. Again, I gotta add some more sections, but its just repetition and adding the pins now.

// Output
int blue1Pin = 13;     // 1st Blue LED pin
int blue2Pin = 12;     // 2nd Blue LED pin
int blue3Pin = 11;     // 3rd Blue LED pin
int blue4Pin = 10;     // 4th Blue LED pin
int white1Pin = 9;     // 1st White LED pin
int white2Pin = 8;     // 2nd White LED pin
int white3Pin = 7;     // 3rd White LED pin
int white4Pin = 6;     // 4th White LED pin


// Program variables set as integer (number) type

int blue1value = 1;     // Variables to store the values to send to the pins
int blue2value = 1;      
int blue3value = 1;      // These values get passed to the analogWrite() function
int blue4value = 1;
int white1value = 1;
int white2value = 1;
int white3value = 1;
int white4value = 1;
int i = 0;             // Loop counter

//setup the pins/ inputs & outputs

void setup()
{
pinMode(blue1Pin, OUTPUT);     // Sets LED as OUTPUTs
pinMode(blue2Pin, OUTPUT);      
pinMode(blue3Pin, OUTPUT);      
pinMode(blue4Pin, OUTPUT);
pinMode(white1Pin, OUTPUT);
pinMode(white2Pin, OUTPUT);
pinMode(white3Pin, OUTPUT);
pinMode(white4Pin, OUTPUT);
}

// Main program, count to 2040
void loop()
{
i += 1;      // Increment counter
if(i<255) // First phase of fades
{
blue1value += 1;
blue2value = 1;
blue3value = 1;
blue4value = 1;
white1value = 1;
white2value = 1;
white3value = 1;
white4value = 1;
}
else if (i < 509) // Second phase of fades
{
blue1value = 255;
blue2value += 1;      
blue3value = 1; 
blue4value = 1;
white1value += 1;
white2value = 1;
white3value = 1;
white4value = 1;

}
else if (i < 763) // Third phase of fades
{
blue1value = 255;
blue2value = 255;      
blue3value = 1; 
blue4value = 1;
white1value = 255;
white2value += 1;
white3value = 1;
white4value = 1;
}
else
{
i = 1;
}
// analogWrite() expects 2 parameters, the object/pin and a value between 0 (off) and 255 (full on)
{
analogWrite(blue1Pin, blue1value);   // Write current values to LED pins
analogWrite(blue2Pin, blue2value);  
}
{
delay (660000);
}}

When you get around to hooking up the switches, this

delay (660000);

is going to kill you. The delay call means that nothing else happens (except interrupts, which you're not using).

Every 11 minutes, you'll be able to check to see if the switch is being pressed. That's a long time to have to hold a switch down, waiting for the computer to respond to a switch press.

Imagine if your computer was that slow.

I urge you, again, to look at the Blink Without Delay example, and to consider using the TimedAction library.