Alternate CODE02 Program

I enhanced the CODE02 program that came with the starter kit to do some other tricks, figured I'd share. Enjoy!
Code: Select all | TOGGLE FULL SIZE
`/* ----------------------------------------------------------------------------------------------------------------------------------------------- *//*  How many LEDs are in the circuit.*/const int PIN_COUNT = 8;/*  Holds the digital pin numbers that the LEDs are hooked to.  If you add more LEDs to your  circuit, make sure you add the correct pin numbers to the array.*/const int ledPins[] = {2,3,4,5,6,7,8,9};/*  Initialize the pins on the Arduino used for this circuit.*/void setup(){  for (int i = 0; i < PIN_COUNT; i++)  {    pinMode(ledPins[i], OUTPUT);  }}/*  Method called by Arduino to do its work.*/void loop(){  ascend();  descend();  allOnOff();  progression1();  allOnOff();  progression2();  allOnOff();  progression3();  allOnOff();  progression4();  allOnOff();  progression5();  allOnOff();  progression6();  allOnOff();}/*  Flash each LED in ascending order.*/void ascend(){  for (int i = 0; i < PIN_COUNT; i++)  {    digitalWrite(ledPins[i], HIGH);    delay(100);    digitalWrite(ledPins[i], LOW);  }}/*  Flash each LED in descending order.*/void descend(){  int descending[] = {9,8,7,6,5,4,3,2};    for (int i = 0; i < PIN_COUNT; i++)  {    digitalWrite(descending[i], HIGH);    delay(100);    digitalWrite(descending[i], LOW);  }}/*  Progression 1: In ascending order, turn on an LED and  then turn on/off for all LEDs following it.  Finally,  turn off the initial LED and move to the next one  until all are exhausted.  0,1,2,3,4,5,6,7  1,2,3,4,5,6,7  2,3,4,5,6,7  etc*/void progression1(){  for (int i = 0; i < PIN_COUNT; i++)  {    digitalWrite(ledPins[i], HIGH);    for (int j = i + 1; j < PIN_COUNT; j++)    {      digitalWrite(ledPins[j], HIGH);      delay(100);      digitalWrite(ledPins[j], LOW);    }    digitalWrite(ledPins[i], LOW);  }}/*  Progression 2: In ascending order, turn on an LED and  then swing up and back through the remaining lEDs, turning  each on/off in order.  Finally, turn off the initial LED  and move on to the next one until all are exhausted:    0,1,2,3,4,5,6,7,6,5,4,3,2,1,0    1,2,3,4,5,6,7,6,5,4,3,2,1    2,3,4,5,6,7,6,5,4,3,2    etc.*/void progression2(){  for (int i = 0; i < PIN_COUNT; i++)   {    digitalWrite(ledPins[i], HIGH);    for (int j = i + 1; j < PIN_COUNT; j++)     {      digitalWrite(ledPins[j], HIGH);      delay(100);      digitalWrite(ledPins[j], LOW);    }    for (int j = PIN_COUNT - 1; j > i; j--)     {      digitalWrite(ledPins[j], HIGH);      delay(100);      digitalWrite(ledPins[j], LOW);    }    digitalWrite(ledPins[i], LOW);  }}/*  Progression 3: In ascending order, turn on an LED and   then turn on/off all remaining LEDs in order including  the ones that proceed it:    0,1,2,3,4,5,6,7    1,2,3,4,5,6,7,0    2,3,4,5,6,7,0,1    etc.*/void progression3(){  for (int i = 0; i < PIN_COUNT; i++)  {    digitalWrite(ledPins[i], HIGH);    for (int j = i + 1; j < i + PIN_COUNT; j++)     {      digitalWrite(ledPins[j% PIN_COUNT], HIGH);      delay(100);      digitalWrite(ledPins[j%PIN_COUNT], LOW);    }    digitalWrite(ledPins[i], LOW);  }}/*  Progression 4: In ascending order, turn on an LED  and then swing through all the remaining LEDs  including the ones that precede the starting point:    0,1,2,3,4,5,6,7,6,5,4,3,2,1,0    1,2,3,4,5,6,7,0,7,6,5,4,3,2,1    2,3,4,5,6,7,0,1,0,7,6,5,4,3,2    etc.*/void progression4() {  for (int i = 0; i < PIN_COUNT; i++) {    digitalWrite(ledPins[i], HIGH);    for (int j = i + 1; j < i + PIN_COUNT; j++)    {      digitalWrite(ledPins[j % PIN_COUNT], HIGH);      delay(100);      digitalWrite(ledPins[j % PIN_COUNT], LOW);    }    for (int j = i + PIN_COUNT - 2; j > i; j--)    {      digitalWrite(ledPins[j % PIN_COUNT], HIGH);      delay(100);      digitalWrite(ledPins[j % PIN_COUNT], LOW);    }    digitalWrite(ledPins[i], LOW);  }}/*  Progression 5: In ascending order, turn on an LED and then each  following it without turning every one.  Once the loop is complete  turn all LEDs off and move to the next LED in order and repeat    0,1,2,3,4,5,6,7, all off    1,2,3,4,5,6,7, all off    2,3,4,5,6,7, all off    etc.*/void progression5() {  for (int i = 0; i < PIN_COUNT; i++)  {    for (int j = i; j < PIN_COUNT; j++)    {      digitalWrite(ledPins[j], HIGH);      delay(100);    }    delay(100);    allOff();  }}/*  Progression 6: Swing back and forth through the LEDs  moving inwards with each iteration, sort of like a  swing losing its momentum  0,1,2,3,4,5,6,7,6,5,4,3,2,1,0  1,2,3,4,5,6,5,4,3,2,1  2,3,4,5,4,3,2  3,4,3*/void progression6() {  for (int i = 0; i < PIN_COUNT/2; i++) {    for (int j = i; j < PIN_COUNT-i; j++){      digitalWrite(ledPins[j], HIGH);      delay(100 * (i + 1));    }    for (int j = PIN_COUNT - 1 - i; j > i; j--) {      digitalWrite(ledPins[j], LOW);      delay(100 * (i + 1));    }    allOff();  }}/*  Flash all the LEDs on/off once.*/void allOnOff() {    for (int i = 0; i < PIN_COUNT; i++)    {      digitalWrite(ledPins[i], HIGH);    }    delay(200);    for (int i = 0; i < PIN_COUNT; i++)     {      digitalWrite(ledPins[i], LOW);    }}/*  Turn all the LEDs off.*/void allOff() {    for (int i = 0; i < PIN_COUNT; i++)     {      digitalWrite(ledPins[i], LOW);    }}`
scsosna

Posts: 3
Joined: Wed Oct 27, 2010 7:33 pm

Re: Alternate CODE02 Program

Welcome to the forums and thanks for sharing! I took the liberty of editing your post. If you use the "Code" button when posting code, it preserves the formatting and makes it easier to read and copy to the clipboard.

Posts: 28067
Joined: Sat Feb 07, 2009 9:11 am

Re: Alternate CODE02 Program

I just went through the exercise with the Starter Pack; Thanks for the alternate code..

FYI the (original) tutorials had some neat patterns, but completely overlooked the likely "most wanted", which is the Cylon (or Kitt) scanner aka Larson scanner.

Here's a Cylon function that drops right into the original CODE02:

/*
* cylonScan() - Scan left to right to left to right..
*/
void cylonScan(){
int delayTime = 100; //the time (in milliseconds) to pause between LEDs
//make smaller for quicker switching and larger for slower

for(int i = 0; i <= 7; i++){
int offLED = i - 1; //Calculate which LED was turned on last time through
if(i == 0) { //for i = 1 to 7 this is i minus 1 (i.e. if i = 2 we will
offLED = 7; //turn on LED 2 and off LED 1)
} //however if i = 0 we don't want to turn of led -1 (doesn't exist)
//instead we turn off LED 7, (looping around)
digitalWrite(ledPins[i], HIGH); //turn on LED #i
digitalWrite(ledPins[offLED], LOW); //turn off the LED we turned on last time
delay(delayTime);
}

for(int i = 7; i >= 0; i--){
int offLED = i + 1; //Calculate which LED was turned on last time through
if(i == 7) { //for i = 1 to 7 this is i minus 1 (i.e. if i = 2 we will
offLED = 0; //turn on LED 2 and off LED 1)
} //however if i = 0 we don't want to turn of led -1 (doesn't exist)
//instead we turn off LED 7, (looping around)
digitalWrite(ledPins[i], HIGH); //turn on LED #i
digitalWrite(ledPins[offLED], LOW); //turn off the LED we turned on last time
delay(delayTime);
}

}

scottinnh

Posts: 55
Joined: Wed Dec 15, 2010 4:31 pm

Re: Alternate CODE02 Program

Hello! I was hoping some kind soul might lend some assistance. I'm a sculptor and prop maker new to programming. I'm wrapping my brain around the concepts behind the structure and functions of coding, but am having a hard time figuring out how to manipulate the math variables to alter the code to create the animation I want. I've gotten the hang of changing the existing numbers to achieve different effects, but the animation would require writing new equations with different operators entirely, and it has me stumped.

The project I am working on is a steampunk Daft Punk helmet, with a MUCH simpler animation and much fewer LEDs than Guy Manuel's original. I've wired everything up, and I have two rows of four blue LEDs, one row on either side of the helmet. I want them all to light up starting at the bottom, and then all turn off starting at the bottom. I've fiddled around with the inAndOut code, so I have it set to light up and turn off one LED at a time, starting at the bottom and traveling up, then starting back down at the bottom again. But I can't figure out how to alter it in any way without breaking the code other than speeding it up or slowing it down.

Here's a gallery of photos of me in the costume. http://www.cosplay.com/gallery/197985/

Any help would be greatly appreciated!

Cheers,
Danny Ashby
CEO Outland Armour
http://www.OutlandArmour.com
vdantes

Posts: 2
Joined: Thu Mar 03, 2011 1:01 pm

Re: Alternate CODE02 Program

Post the code you have so far.

Posts: 28067
Joined: Sat Feb 07, 2009 9:11 am

Re: Alternate CODE02 Program

I don't have my sketch on this computer, so I just pulled the specific part I'm using from http://ardx.org/src/circ/CIRC02-code.txt.

Code: Select all | TOGGLE FULL SIZE
`/* * inAndOut() - This will turn on the two middle LEDs then the next two out * making an in and out look */void inAndOut(){  int delayTime = 100; //the time (in milliseconds) to pause between LEDs                       //make smaller for quicker switching and larger for slower    //runs the LEDs out from the middle  for(int i = 0; i <= 3; i++){    int offLED = i - 1;  //Calculate which LED was turned on last time through    if(i == 0) {         //for i = 1 to 7 this is i minus 1 (i.e. if i = 2 we will      offLED = 3;        //turn on LED 2 and off LED 1)    }                    //however if i = 0 we don't want to turn of led -1 (doesn't exist)                         //instead we turn off LED 7, (looping around)    int onLED1 = 3 - i;       //this is the first LED to go on ie. LED #3 when i = 0 and LED                              //#0 when i = 3     int onLED2 = 4 + i;       //this is the first LED to go on ie. LED #4 when i = 0 and LED                              //#7 when i = 3     int offLED1 = 3 - offLED; //turns off the LED we turned on last time    int offLED2 = 4 + offLED; //turns off the LED we turned on last time        digitalWrite(ledPins[onLED1], HIGH);    digitalWrite(ledPins[onLED2], HIGH);        digitalWrite(ledPins[offLED1], LOW);        digitalWrite(ledPins[offLED2], LOW);            delay(delayTime);  }`
vdantes

Posts: 2
Joined: Thu Mar 03, 2011 1:01 pm

Re: Alternate CODE02 Program

Assuming that everything is wired the same as in the example, you just need two 'for' loops. One to turn them on in order, and another to turn them off in the same order. Something like this:

Code: Select all | TOGGLE FULL SIZE
`/** bottomUp() - THis will turn the lights on in sequence from the bottom up* then turn them off in the same sequence*/void bottomUp(){  int delayTime = 100; //the time (in milliseconds) to pause between LEDs                       //make smaller for quicker switching and larger for slower    //Turn on from the bottom up  for(int i = 0; i <= 3; i++)  {    int onLED1 = 3 - i;       //this is the first LED to go on ie. LED #3 when i = 0 and LED                              //#0 when i = 3     int onLED2 = 4 + i;       //this is the first LED to go on ie. LED #4 when i = 0 and LED                              //#7 when i = 3                                      digitalWrite(ledPins[onLED1], HIGH);    digitalWrite(ledPins[onLED2], HIGH);        delay(delayTime);  }    //Turn off from the bottom up  for(int i = 0; i <= 3; i++)  {    int offLED1 = 3 - i;       //this is the first LED to go on ie. LED #3 when i = 0 and LED                              //#0 when i = 3     int offLED2 = 4 + i;       //this is the first LED to go on ie. LED #4 when i = 0 and LED                              //#7 when i = 3                                      digitalWrite(ledPins[offLED1], LOW);        digitalWrite(ledPins[offLED2], LOW);            delay(delayTime);  }}`

Posts: 28067
Joined: Sat Feb 07, 2009 9:11 am

Re: Alternate CODE02 Program

To me, the inAndOut() code seemed overly complicated. It took me a few times reading through it to understand what they were doing. I made a more simplified version of the code that does the same thing:
Code: Select all | TOGGLE FULL SIZE
`void betterInAndOut(){    int delayTime = 100;            //Time to wait between lights in ms.    for(int i = 0; i <= 3; i++){          //Going in    digitalWrite(ledPins[i], HIGH);     //Turn on low end    digitalWrite(ledPins[7-i], HIGH);   //Turn on high end    delay(delayTime);                  digitalWrite(ledPins[i], LOW);      //Turn off low end    digitalWrite(ledPins[7-i], LOW);    //Turn off high end  }    for(int i = 2; i >= 1; i--){          //Going out    digitalWrite(ledPins[i], HIGH);     //Turn on low end    digitalWrite(ledPins[7-i], HIGH);   //Turn on high end    delay(delayTime);                  digitalWrite(ledPins[i], LOW);      //Turn off low end    digitalWrite(ledPins[7-i], LOW);    //Turn off high end  }}`

Is there any reason why doing it the original way might be better?
enjo

Posts: 1
Joined: Sun May 01, 2011 2:46 pm

Re: Alternate CODE02 Program

I added two more LEDs, so I had 10. That required only changing PIN_COUNT and manually adding two more addresses when declaring ledPins[]. It worked, except for descend(). Here are some changes I made to descend():

Code: Select all | TOGGLE FULL SIZE
`/*  How many LEDs are in the circuit.*/const int PIN_COUNT = 10; //LED Pin Variablesint ledPins[] = {2,3,4,5,6,7,8,9,10,11}; //An array to hold the pin each LED...`

.....
Code: Select all | TOGGLE FULL SIZE
`/*  Flash each LED in descending order.*/void descend(){//  int descending[] = {11,10,9,8,7,6,5,4,3,2};  // this seemed unnecessary to me, since we already have ledPins[]  for (int i = PIN_COUNT-1; i > 0; i--) //I use PIN_COUNT-1 so as to avoid the                                                       //unsightly repeat lighting of the last LED  {//    digitalWrite(descending[i], HIGH);      digitalWrite(ledPins[i], HIGH);    delay(100);//    digitalWrite(descending[i], LOW);    digitalWrite(ledPins[i], LOW);  }}`

Simple changes, but it's all about refinement, right?
YasBean

Posts: 1
Joined: Thu May 26, 2011 9:14 pm

Re: Alternate CODE02 Program

I just started playing around with this set myself, and i found another fun option to try.
It also introduces a small new (at this stage at least) function you can use for testing.

Here's my code (as it can be inserted in the original code):
Code: Select all | TOGGLE FULL SIZE
`void randomized(){      int delayTime = 10;      int randomLED = random(0,8);      digitalWrite(ledPins[randomLED], HIGH);      delay(delayTime);      digitalWrite(ledPins[randomLED], LOW);}     `

This randomly turns one of your LEDs on, waits the delay, and turns it off again.
With the delay set to around 10 it looks like the leds on a modem or switch :)
Felyrion

Posts: 1
Joined: Tue Jul 05, 2011 5:57 pm

Re: Alternate CODE02 Program

I put all the different functions into one file and cleaned things up a bit.

This version iterates through the sequence using an array of function pointers. It also shows how to use sizeof to get the number of elements in an array without having to have an explicit variable to store it.

I also updated the randomized() function to do the random thing for a specific number of seconds (5).

http://pastebin.com/k0eXpX66

Note: this version uses pins 3-10 instead of 2-9 like the original one.
phil_s_stein

Posts: 1
Joined: Sun Jul 31, 2011 3:19 pm

Re: Alternate CODE02 Program

Here's a version I did for a 5 LED circuit with a push button:

http://pastebin.com/VG7XLiNN

It uses bit fields to set and clear the LED states, so you can set up animations easily. It has 5 animations that you can switch between using the push button.
Adding your own animations should be very easy, each "frame" is 1 byte, each bit is the state of 1 of the LEDs (0 = off, 1 = on).

You should be able to use this with the stock CIRC02 circuit by using 8 bits instead of the 5 I use, and removing the button check.
To switch patterns, you would then have to change the index in the sketch and re-upload it.
zik

Posts: 1
Joined: Tue Sep 13, 2011 3:49 pm

Re: Alternate CODE02 Program

OP, here is the same code but I had it dynamically fill the array so if you add more LEDs it scales with you just changing the pin_count

Code: Select all | TOGGLE FULL SIZE
`/* ----------------------------------------------------------------------------------------------------------------------------------------------- *//*  How many LEDs are in the circuit.*/const int PIN_COUNT = 8;//Int array for pin numberint ledPins[PIN_COUNT];//Dynamically fills the array based on the PIN_COUNT Constant abovevoid fill_array(int ar[], int x){    int y = 2;    for(int i = 0; i < x; i++)    {       ar[i] = y;        y++;    }}/*  Initialize the pins on the Arduino used for this circuit.*/void setup(){  fill_array(ledPins, PIN_COUNT);  for (int i = 0; i < PIN_COUNT; i++)  {    pinMode(ledPins[i], OUTPUT);  }}/*  Method called by Arduino to do its work.*/void loop(){  ascend();  descend();  allOnOff();  progression1();  allOnOff();  progression2();  allOnOff();  progression3();  allOnOff();  progression4();  allOnOff();  progression5();  allOnOff();  progression6();  allOnOff();}/*  Flash each LED in ascending order.*/void ascend(){  for (int i = 0; i < PIN_COUNT; i++)  {    digitalWrite(ledPins[i], HIGH);    delay(100);    digitalWrite(ledPins[i], LOW);  }}/*  Flash each LED in descending order.*/void descend(){  int descending[] = {9,8,7,6,5,4,3,2};    for (int i = 0; i < PIN_COUNT; i++)  {    digitalWrite(descending[i], HIGH);    delay(100);    digitalWrite(descending[i], LOW);  }}/*  Progression 1: In ascending order, turn on an LED and  then turn on/off for all LEDs following it.  Finally,  turn off the initial LED and move to the next one  until all are exhausted.  0,1,2,3,4,5,6,7  1,2,3,4,5,6,7  2,3,4,5,6,7  etc*/void progression1(){  for (int i = 0; i < PIN_COUNT; i++)  {    digitalWrite(ledPins[i], HIGH);    for (int j = i + 1; j < PIN_COUNT; j++)    {      digitalWrite(ledPins[j], HIGH);      delay(100);      digitalWrite(ledPins[j], LOW);    }    digitalWrite(ledPins[i], LOW);  }}/*  Progression 2: In ascending order, turn on an LED and  then swing up and back through the remaining lEDs, turning  each on/off in order.  Finally, turn off the initial LED  and move on to the next one until all are exhausted:    0,1,2,3,4,5,6,7,6,5,4,3,2,1,0    1,2,3,4,5,6,7,6,5,4,3,2,1    2,3,4,5,6,7,6,5,4,3,2    etc.*/void progression2(){  for (int i = 0; i < PIN_COUNT; i++)   {    digitalWrite(ledPins[i], HIGH);    for (int j = i + 1; j < PIN_COUNT; j++)     {      digitalWrite(ledPins[j], HIGH);      delay(100);      digitalWrite(ledPins[j], LOW);    }    for (int j = PIN_COUNT - 1; j > i; j--)     {      digitalWrite(ledPins[j], HIGH);      delay(100);      digitalWrite(ledPins[j], LOW);    }    digitalWrite(ledPins[i], LOW);  }}/*  Progression 3: In ascending order, turn on an LED and   then turn on/off all remaining LEDs in order including  the ones that proceed it:    0,1,2,3,4,5,6,7    1,2,3,4,5,6,7,0    2,3,4,5,6,7,0,1    etc.*/void progression3(){  for (int i = 0; i < PIN_COUNT; i++)  {    digitalWrite(ledPins[i], HIGH);    for (int j = i + 1; j < i + PIN_COUNT; j++)     {      digitalWrite(ledPins[j% PIN_COUNT], HIGH);      delay(100);      digitalWrite(ledPins[j%PIN_COUNT], LOW);    }    digitalWrite(ledPins[i], LOW);  }}/*  Progression 4: In ascending order, turn on an LED  and then swing through all the remaining LEDs  including the ones that precede the starting point:    0,1,2,3,4,5,6,7,6,5,4,3,2,1,0    1,2,3,4,5,6,7,0,7,6,5,4,3,2,1    2,3,4,5,6,7,0,1,0,7,6,5,4,3,2    etc.*/void progression4() {  for (int i = 0; i < PIN_COUNT; i++) {    digitalWrite(ledPins[i], HIGH);    for (int j = i + 1; j < i + PIN_COUNT; j++)    {      digitalWrite(ledPins[j % PIN_COUNT], HIGH);      delay(100);      digitalWrite(ledPins[j % PIN_COUNT], LOW);    }    for (int j = i + PIN_COUNT - 2; j > i; j--)    {      digitalWrite(ledPins[j % PIN_COUNT], HIGH);      delay(100);      digitalWrite(ledPins[j % PIN_COUNT], LOW);    }    digitalWrite(ledPins[i], LOW);  }}/*  Progression 5: In ascending order, turn on an LED and then each  following it without turning every one.  Once the loop is complete  turn all LEDs off and move to the next LED in order and repeat    0,1,2,3,4,5,6,7, all off    1,2,3,4,5,6,7, all off    2,3,4,5,6,7, all off    etc.*/void progression5() {  for (int i = 0; i < PIN_COUNT; i++)  {    for (int j = i; j < PIN_COUNT; j++)    {      digitalWrite(ledPins[j], HIGH);      delay(100);    }    delay(100);    allOff();  }}/*  Progression 6: Swing back and forth through the LEDs  moving inwards with each iteration, sort of like a  swing losing its momentum  0,1,2,3,4,5,6,7,6,5,4,3,2,1,0  1,2,3,4,5,6,5,4,3,2,1  2,3,4,5,4,3,2  3,4,3*/void progression6() {  for (int i = 0; i < PIN_COUNT/2; i++) {    for (int j = i; j < PIN_COUNT-i; j++){      digitalWrite(ledPins[j], HIGH);      delay(100 * (i + 1));    }    for (int j = PIN_COUNT - 1 - i; j > i; j--) {      digitalWrite(ledPins[j], LOW);      delay(100 * (i + 1));    }    allOff();  }}/*  Flash all the LEDs on/off once.*/void allOnOff() {    for (int i = 0; i < PIN_COUNT; i++)    {      digitalWrite(ledPins[i], HIGH);    }    delay(200);    for (int i = 0; i < PIN_COUNT; i++)     {      digitalWrite(ledPins[i], LOW);    }}/*  Turn all the LEDs off.*/void allOff() {    for (int i = 0; i < PIN_COUNT; i++)     {      digitalWrite(ledPins[i], LOW);    }}`
tylertech

Posts: 1
Joined: Mon Feb 27, 2012 4:34 pm