Arduino Christmas Lights controller - Switching loops.

Hi guys,
I have an Arduino, an 8 port 240v relay and I have this all set up so I can run loops. Very cool so far.

I have made some standard loops that do things like "rainbow" "knightrider" and blink the lights... (you know what I mean) but to program an entire song's worth of christmassy goodness would run me out of memory, plus it would be an atrocity as far as the code goes.

Unfortunately my background in programming is Batch and Powershell.... (I am a qualified Cobol and Pascal programmer, but havent used either in about 20 years).

What I need to do is define my loops (functions?) and then call them.

So for example, i would define my Knightrider loop, my blink loop and then later tell it to play Knighrider twice, and then blink once, and then run rainbow 4 times... etc.. I know I should be using functions but I the examples dont really make sense to me....

I know this is the super basics, but I was hoping someone could have a look a this code... and explain how to turn the main function into a function I can then call lets say 4 times...

#define RELAY1  13                        
#define RELAY2  12                        
#define RELAY3  11                        
#define RELAY4  10
#define RELAY5  9
#define RELAY6  8
#define RELAY7  7
#define RELAY8  6
#define DELAYSPEED 100

void setup()
{    
// Initialise the Arduino data pins for OUTPUT
  pinMode(RELAY1, OUTPUT);       
  pinMode(RELAY2, OUTPUT);
  pinMode(RELAY3, OUTPUT);
  pinMode(RELAY4, OUTPUT);
  pinMode(RELAY5, OUTPUT);
  pinMode(RELAY6, OUTPUT);
  pinMode(RELAY7, OUTPUT);
  pinMode(RELAY8, OUTPUT);
  
}

void loop()
{ 

   digitalWrite(RELAY8,LOW);
   digitalWrite(RELAY1,HIGH);
   delay(DELAYSPEED); 
   digitalWrite(RELAY1,LOW);
   digitalWrite(RELAY2,HIGH);
   delay(DELAYSPEED); 
   digitalWrite(RELAY2,LOW);
   digitalWrite(RELAY3,HIGH);
   delay(DELAYSPEED); 
   digitalWrite(RELAY3,LOW);
   digitalWrite(RELAY4,HIGH);
   delay(DELAYSPEED); 
   digitalWrite(RELAY4,LOW);
   digitalWrite(RELAY5,HIGH);
   delay(DELAYSPEED); 
   digitalWrite(RELAY5,LOW);
   digitalWrite(RELAY6,HIGH);
   delay(DELAYSPEED); 
   digitalWrite(RELAY6,LOW);
   digitalWrite(RELAY7,HIGH);
   delay(DELAYSPEED); 
   digitalWrite(RELAY7,LOW);
   digitalWrite(RELAY8,HIGH);


   digitalWrite(RELAY1,LOW);
   digitalWrite(RELAY8,HIGH);
   delay(DELAYSPEED); 
   digitalWrite(RELAY8,LOW);
   digitalWrite(RELAY7,HIGH);
   delay(DELAYSPEED); 
   digitalWrite(RELAY7,LOW);
   digitalWrite(RELAY6,HIGH);
   delay(DELAYSPEED); 
   digitalWrite(RELAY6,LOW);
   digitalWrite(RELAY5,HIGH);
   delay(DELAYSPEED); 
   digitalWrite(RELAY5,LOW);
   digitalWrite(RELAY4,HIGH);
   delay(DELAYSPEED); 
   digitalWrite(RELAY4,LOW);
   digitalWrite(RELAY3,HIGH);
   delay(DELAYSPEED); 
   digitalWrite(RELAY3,LOW);
   digitalWrite(RELAY2,HIGH);
   delay(DELAYSPEED); 
   digitalWrite(RELAY2,LOW);
   digitalWrite(RELAY1,HIGH);

 }

So for example, i would define my Knightrider loop, my blink loop and then later tell it to play Knighrider twice, and then blink once, and then run rainbow 4 times... etc..

Assuming you would like to loop that sequence, I think you could add this to the bottom of your code:

 void loop() {
   Knightrider();
   Blink();
   //etc. 
 }

This just calls the functions. You can sequence them however you want within the loop.

darn, I posted some code that doesnt work, at the moment it has the word "knightrider" where it said loop, this returned an error...

I guess my question is how do I define "knightrider" and then how do I call "knightrider" to execute...

I recommend a little reading on functions.

http://playground.arduino.cc/Code/Function

I believe that to define and call knightrider, the code should look something like this:

void knightrider() {
  //Put the code for what knightrider does in here
}

void loop() {
  knightrider();
  //call any other functions in that same manner
}

If your function does not have any parameters, then this second piece of code should not apply. If your function has parameters (which I don't believe it should), then the code should look more like this:

As an example parameter, I will use "relayPin", which is called for pin 3

void knightrider(int relayPin) {
  //Put the code for what knightrider does in here
}

void loop() {
  knightrider(3);
  //call any other functions in that same manner
}

If my explanation was ineffective, this link explains it well: http://www.cplusplus.com/doc/tutorial/functions/

I have made some standard loops that do things like "rainbow" "knightrider" and blink the lights... (you know what I mean) but to program an entire song's worth of christmassy goodness would run me out of memory, plus it would be an atrocity as far as the code goes.

It shouldn't take that much memory if you generate the patterns with an algorithm instead of storing patterns in memory.

8 relays? Perfect, a byte is 8 bits long. Look into bit shifting, it will make things easier down the line.

And for large amounts of data or bytes, you can use arrays or even better PROGMEM.

DVDdoug:
It shouldn't take that much memory if you generate the patterns with an algorithm instead of storing patterns in memory.

Very good point...

awdairnner:
I believe that to define and call knightrider, the code should look something like this:

void knightrider() {

//Put the code for what knightrider does in here
}

void loop() {
  knightrider();
  //call any other functions in that same manner
}




If your function does not have any parameters, then this second piece of code should not apply. If your function *has* parameters (which I don't believe it should), then the code should look more like this:

As an example parameter, I will use "relayPin", which is called for pin 3


void knightrider(int relayPin) {
  //Put the code for what knightrider does in here
}

void loop() {
  knightrider(3);
  //call any other functions in that same manner
}




If my explanation was ineffective, this link explains it well: http://www.cplusplus.com/doc/tutorial/functions/

Actually, you explained it great, got the hang of it now. Thanks!

HazardsMind:
8 relays? Perfect, a byte is 8 bits long. Look into bit shifting, it will make things easier down the line.

And for large amounts of data or bytes, you can use arrays or even better PROGMEM.

Thanks! Will look into it.