IAN LANG ELECTRONICS

# CIRC-02

I like this circuit. It's a fairly easy construct and it does what it's supposed to, and so you won't get any complaints from me. I do however feel that the accompanying code is very hard to fathom and might present a beginner with problems. It's meant to run a light sequencer  and teach you about using arrays to keep your lines of code down. With that in mind, here's something a little more clear:

Go Back

// "Disco Fever" by Ian Lang 2011

// Light Chasing Sequences

int (uprange []) ={2,3,4,5,6,7,8,9}; // led pin range

int (downrange []) ={9,8,7,6,5,4,3,2}; // led range inverted

int x; //which we shall use for a repeat counter

int y; //which we shall use for a general purpose variable

int z; //which we shall use for a general purpose variable

void setup (){

for (int j=0;j<8;j=j+1){digitalWrite (uprange [j],HIGH);delay (400);digitalWrite (uprange [j],LOW);}// test pattern

}

void loop (){

pingpong ();

sidestomiddle ();

updowneachside ();

pairs ();

boxafrogs();

knockknock ();

}

void alloff(){//marks the beginning of this command routine

// command routine used in MODE 0 and others

for (int j=0;j<8;j=j+1){digitalWrite (uprange [j],LOW);}// turns all LEDS off

}// marks the end of this command routine

void allflash (){//marks the beginning of this subroutine

// MODE 0: All eight LEDs Flash on and then off.Marks beginning of all other modes.

alloff ();//all LEDs off

x=0; //resets the repeat counter

while (x<21){ //repeat counter

alloff();

for (int j=0;j<8;j=j+1){digitalWrite (uprange [j],HIGH);}// turns all LEDS on

delay (50); // sets mark time 5/100ths of a second

alloff(); // all LEDs off

delay(50); //sets space time 5/100ths of a second

x++;}//increment repeat count by 1 & marks the end of the repeat counter

}// marks the end of this subroutine

void boxafrogs(){//marks the beginning of this subroutine

//MODE 1: Random lights

x=0; //resets the repeat counter

allflash();

while (x<100){//repeat counter

y=random (0,7);//choose a random position in led ranges

digitalWrite (uprange[y],HIGH);digitalWrite (downrange [y],HIGH); //switch on range positions

delay (50);//set mark time

alloff();

x++;}//increment repeat count by 1 & marks the end of the repeat counter

}// marks the end of this subroutine

void updowneachside (){//marks the beginning of this subroutine

//MODE 2: goes up and down and down and up

allflash();

for (int j=0;j<8;j=j+1){

digitalWrite (uprange [j],HIGH);delay (125);}//switch on up LEDs in turn

delay (100); //define time between on and off 1/10th sec

for (int j=0;j<8;j=j+1){

digitalWrite (downrange [j],LOW);delay (125);}//switch off down LEDs in turn

for (int j=0;j<8;j=j+1){

digitalWrite (downrange [j],HIGH);delay (125);}//switch on down LEDs in turn

delay (100);//define time between on and off 1/10th sec

for (int j=0;j<8;j=j+1){

digitalWrite (uprange [j],LOW);delay (125);}//switch off up LEDs in turn

}// marks the end of this subroutine

void sidestomiddle () {//marks the beginning of this subroutine

// MODE 3: from sides to middle and back again

allflash();

x=0;

while (x<2){//set repeat counter

for (int j=0;j<4;j=j+1){//half of range

digitalWrite (uprange [j],HIGH);digitalWrite(downrange[j],HIGH);//pairs 0-7,1-6,2-5,3-4 ON....

delay (100);}//set delay time between pairs going on

for (int j=4;j>-1;j=j-1){

digitalWrite (uprange [j],LOW);digitalWrite(downrange[j],LOW);//pairs 0-7,1-6,2-5,3-4 OFF....

delay (100);}//set delay time between pairs going off

delay(100); //set delay between end of this routine and beginning of next

x++;}//increment repeat count by 1 & marks the end of the repeat counter

}// marks the end of this subroutine

void pingpong(){//marks the beginning of this subroutine

//MODE 4: one side to side

allflash();

x=0; //resets the repeat counter

while (x<4){

for (int j=0;j<8;j=j+1){

digitalWrite (uprange [j],HIGH);delay(50);digitalWrite (uprange[j],LOW);}

for (int j=0;j<8;j=j+1){

digitalWrite (downrange [j],HIGH);delay(50);digitalWrite (downrange[j],LOW);}

x++;}

}// marks the end of this subroutine

void knockknock (){//marks the beginning of this subroutine

//MODE5: Three always on

allflash();

x=0;//reset counter

y=3;// position counter

while (x<6){ //use counter as track

for (int j=y-3;j<y+1;j=j+1){

digitalWrite (uprange [j],HIGH);//three on

delay(50);}// set delay

y=y+1; //increment position counter

alloff ();//switch off

x++;}//increment tracker

}// marks the end of this subroutine

void pairs (){// marks the beginning of this subroutine

// MODE 6:  outside pairs flash, outside stay on, inside flash,inside stay on,outside flash

allflash ();

int (z[])={1,2};

x=0; //resets the repeat counter

while (x<75){ //the following four lines flash the outside pairs

for (int j=0;j<2;j=j+1){ //sets range parameters

digitalWrite(uprange[j],HIGH);digitalWrite(downrange [j],HIGH);delay (40);alloff();

x++;}}x=0;//increments the repeat counter & resets it after done

/////////////////////////////////////////////////////////////////////////

for (int j=0;j<2;j=j+1){ //sets range parameters

digitalWrite(uprange[j],HIGH);digitalWrite(downrange [j],HIGH);}/////switch on outside pairs

while (x<75){ //the following four lines flash the inside pairs

for (int j=2;j<4;j=j+1){ //sets range parameters

digitalWrite(uprange[j],HIGH);digitalWrite(downrange [j],HIGH);delay (40); digitalWrite(uprange[j],LOW);digitalWrite(downrange [j],LOW);

x++;}}x=0;//increments the repeat counter & resets it after done

///////////////////////////////////////////////////////////////////

for (int j=2;j<4;j=j+1){ //sets range parameters

digitalWrite(uprange[j],HIGH);digitalWrite(downrange [j],HIGH);}/////switch on inside pairs

while (x<75){ //the following four lines flash the outside pairs

for (int j=0;j<2;j=j+1){ //sets range parameters

digitalWrite(uprange[j],HIGH);digitalWrite(downrange [j],HIGH);delay (40);digitalWrite(uprange[j],LOW);digitalWrite(downrange [j],LOW);

x++;}}x=0;//increments the repeat counter & resets it after done

}// marks the end of this subroutine

I know exactly what you are thinking now. You are thinking "what is that parrot-faced spawny-eyed wazzock talking about? That's not clear at all!"

Fear not because we're going through it step by step, and the first step is to build the circuit exactly as the book says. Here it is:

The construction is an easy one but it's a tad fiddly. My recommendation is that under a good light (I ended up using my 100W directed bench lamp) you put the LEDs in first and then the resistors. Make the anodes and cathodes of the eight LEDs face a uniform way and you won't have any problem knowing where to put the resistors and jump wires. If you follow the paper map they give you you shouldn't have any problems. Remember that the long leg on the LED is the cathode. Once you've built it, upload the code Oomlout recommend and watch it twinkle. Then copy and paste the code I supplied above and we'll start looking at it.

Assuming that at this point you've uploaded my code and all has gone well, this is what your board should now be doing:

1. The LEDs light up and go out one by one slowly. This is the test pattern. You won't see this again until the next time you apply power.

2. All the LEDs flash, then there's an illusion of a moving light up and down four times. This is pingpong ().

3.All the LEDs flash, then the lights light up one by one from each end and go to the middle, the middle two go out and one by one this extends to the sides. This is sidestomiddle ( ).

4..All the LEDs flash, then the lights go on one by one up the range, then off one by one down the range, then they do the same from the other side. This is updowneachside ( ).

5. All the LEDs flash, then the outside 2 LEDs on each side flash and finally stay on.Then the insides flash, then stay on, and the outsides flash again. This is pairs ( )

6.  All the LEDs flash, then the lights jump about randomly. This is boxafrogs ( ).

7. All the LEDs flash, then the lights go up the range in a sneaky manner from one end to the other. This is knockknock ( ).

The code has been tested for copy and paste and it works, so if you're having problems e-mail me the code that appears in your Arduino environment  window (the bit where you see and write the code and then upload it. If your compiler is complaining, let me know what it says.

# Nuts and Bolts

Right, let's break that code down into small bite-sized chunks and see what it's doing. It might be better if you print the code out first and follow it along.  If there's any jargon here you don't understand see the sidebar next to the code above. First off is all the bits before void setup ( ) and they look like this:

// "Disco Fever" by Ian Lang 2011

// Light Chasing Sequences

int (uprange [ ]) ={2,3,4,5,6,7,8,9}; // led pin range

int (downrange [ ]) ={9,8,7,6,5,4,3,2}; // led range inverted

int x; //which we shall use for a repeat counter

int y; //which we shall use for a general purpose variable

int z; //which we shall use for a general purpose variable

void setup (){

Lines 1 & 2 are remark (REM) statements and are denoted so by the // at the beginning. Everything written after // gets ignored by the compiler and is not sent to the Arduino board.

Line 3 sets up what is known as an array. An array is a variable split up into pieces and here there are 8 of them {2,3,4,5,6,7,8,9} which are the pin numbers to which each individual LED is attached.  Array numbers start at 0 So uprange [0] contains the data "2", uprange [1] contains the data "3" and so on until you reach uprange [7] containing the data "9".

Line 4 does exactly the same thing except that it goes the other way round. So downrange [0] contains the data "9" and downrange [7] contains the data "2"

Why is the top number 7 when there's eight pieces of data? Because we start at zero. 0,1,2,3,4,5,6,7 makes eight individual locations. Don't forget that. It's important. I do frequently forget it and spend ten minutes scratching my head and chewing my pencil before I spot the problem.

Lines 5, 6 & 7 declare variables that we are going to use later in the program. If we declare them now, we don't have to declare them in each subsequent subroutine.

1

2

3

4

5

6

7

WHAT IN GOD'S NAME IS.......?

Those of you who don't have any previous form in programming might be all at sea with some of the jargon involved. Here is your guide to some of the terms:

VARIABLE

A variable is a named 'space'  in which we can store a piece of data.  In our example here the data is all numeric, and so for example;

x = 0

x is the variable, and this time it equals zero. But if we say at some point in the program:

x=x+1

we add 1 to x, making it now equal 1. If we did that 12 times, x would equal 12 at the finish, but inbetween it would be 2,3,4, etc all the way up to 12. Its value varies as we tell it to.

ARRAY

An array is a variable that has been subdivided into a row of variables. They all have the same name but each has a number position too. So if we had done that with x above, we'd have x[1], x[2],x[3] and so on. In general we tell an array the data it's going to have beforehand.

If you look at the code here you'll find two arrays; they're called   "uprange"   and "downrange".

For/Next Loop

A For/Next Loop has a numeric value which tells it how many times the loop should go round before going on to the next piece of code after it. All the code inside the For/Next Loop will be processed every time the loop goes round but not once the loop has been exited.

Nested Loop

A loop inside a loop. The inner loop goes round the full count every time the outer loop begins the next count . Multiple nested loops are a bad idea: more than six means it's very complex to work out when something goes wrong.

REM Statement

A remark put there by the programmer to denote what that particular piece of code is doing. It's ignored by the processor.

Subroutine

A set of code to carry out a specific instruction; the next  subdivision down from a program.

More> Copy & Paste Code from Here: