Go Down

Topic: Simple Array Usage (Read 738 times) previous topic - next topic

k2pek2

May 01, 2009, 03:35 am Last Edit: May 01, 2009, 03:36 am by k2pek2 Reason: 1
Hello, I'm coding a line of 14 LEDs (we all love blinking LEDs, right?) and i'm controlling them using an array. That's all great, but for the instances where I individually control the LEDs, the code gets messy (having 14 lines of code for turning them on, 14 for off again). Is there a way to control multiple values in the array using one line of code?

(here's the code i have):

Code: [Select]

int pinArray[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
int count = 0;
int timer = 30;
int cycle = 10;

void setup(){  //This sets up the program
 for (count=0;count<13;count++) {
   pinMode(pinArray[count], OUTPUT);
 }
}

void loop() {  //This section turns all LEDs on in consecutive order, then off in order. Then it goes back the same way
 for (cycle=0;cycle<9;cycle++){
   for (count=0;count<14;count++) {
     digitalWrite(pinArray[count], HIGH);
     delay(timer);
   }
   count=0;
   for (count=0;count<14;count++) {
     digitalWrite(pinArray[count], LOW);
     delay(timer);
   }
   for (count=13;count>=0;count--) {
     digitalWrite(pinArray[count], HIGH);
     delay(timer);
   }
   count=13;
   for (count=13;count>=0;count--) {
     digitalWrite(pinArray[count], LOW);
     delay(timer);
   }
 }
 for (cycle>8;cycle<19;cycle++){  //This section blinks two lit LEDs across the faceplate and back
   for (count=0;count<13;count++) {
     digitalWrite(pinArray[count], HIGH);
     delay(timer/2);
     digitalWrite(pinArray[count + 1], HIGH);
     delay(timer/2);
     digitalWrite(pinArray[count], LOW);
     delay(timer);
   }
   for (count=13;count>-1;count--) {
     digitalWrite(pinArray[count], HIGH);
     delay(timer/2);
     digitalWrite(pinArray[count - 1], HIGH);
     delay(timer/2);
     digitalWrite(pinArray[count], LOW);
     delay(timer/2);
   }
 }
 cycle = 29; //This section blinks on LEDs then off, and waits to blink the next one
 for (cycle>28;cycle<34;cycle++){
   count = 13;
   for (count<14;count>-1;count--){
     digitalWrite(pinArray[count], HIGH);
     delay(timer*2);
     digitalWrite(pinArray[count], LOW);
     delay(timer*10);
   }
 }
 for (cycle>34;cycle<55;cycle++){  //This section scrolls four sets of two lit LEDs across
   digitalWrite(pinArray[2], LOW);
   digitalWrite(pinArray[6], LOW);
   digitalWrite(pinArray[10], LOW);
   digitalWrite(pinArray[0], HIGH);
   digitalWrite(pinArray[4], HIGH);
   digitalWrite(pinArray[8], HIGH);
   digitalWrite(pinArray[12], HIGH);
   delay(timer*6);
   digitalWrite(pinArray[3], LOW);
   digitalWrite(pinArray[7], LOW);
   digitalWrite(pinArray[11], LOW);
   digitalWrite(pinArray[1], HIGH);
   digitalWrite(pinArray[5], HIGH);
   digitalWrite(pinArray[9], HIGH);
   digitalWrite(pinArray[13], HIGH);
   delay(timer*6);
   digitalWrite(pinArray[0], LOW);
   digitalWrite(pinArray[4], LOW);
   digitalWrite(pinArray[8], LOW);
   digitalWrite(pinArray[12], LOW);
   digitalWrite(pinArray[2], HIGH);
   digitalWrite(pinArray[6], HIGH);
   digitalWrite(pinArray[10], HIGH);
   delay(timer*6);
   digitalWrite(pinArray[1], LOW);
   digitalWrite(pinArray[5], LOW);
   digitalWrite(pinArray[9], LOW);
   digitalWrite(pinArray[13], LOW);
   digitalWrite(pinArray[3], HIGH);
   digitalWrite(pinArray[7], HIGH);
   digitalWrite(pinArray[11], HIGH);
   delay(timer*6);
 }
 for (cycle>54;cycle<65;cycle++){  //This section blinks all LEDs on and off together
   digitalWrite(pinArray[0], HIGH);
   digitalWrite(pinArray[1], HIGH);
   digitalWrite(pinArray[2], HIGH);
   digitalWrite(pinArray[3], HIGH);
   digitalWrite(pinArray[4], HIGH);
   digitalWrite(pinArray[5], HIGH);
   digitalWrite(pinArray[6], HIGH);
   digitalWrite(pinArray[7], HIGH);
   digitalWrite(pinArray[8], HIGH);
   digitalWrite(pinArray[9], HIGH);
   digitalWrite(pinArray[10], HIGH);
   digitalWrite(pinArray[11], HIGH);
   digitalWrite(pinArray[12], HIGH);
   digitalWrite(pinArray[13], HIGH);
   delay(timer*10);
   digitalWrite(pinArray[0], LOW);
   digitalWrite(pinArray[1], LOW);
   digitalWrite(pinArray[2], LOW);
   digitalWrite(pinArray[3], LOW);
   digitalWrite(pinArray[4], LOW);
   digitalWrite(pinArray[5], LOW);
   digitalWrite(pinArray[6], LOW);
   digitalWrite(pinArray[7], LOW);
   digitalWrite(pinArray[8], LOW);
   digitalWrite(pinArray[9], LOW);
   digitalWrite(pinArray[10], LOW);
   digitalWrite(pinArray[11], LOW);
   digitalWrite(pinArray[12], LOW);
   digitalWrite(pinArray[13], LOW);
   delay(timer*10);
 }
 if (cycle = 65){  //This section blinks on all LEDs one at a time, then off one at a time.
   digitalWrite(pinArray[3], HIGH);
   delay(timer*10);
   digitalWrite(pinArray[8], HIGH);
   delay(timer*10);
   digitalWrite(pinArray[4], HIGH);
   delay(timer*10);
   digitalWrite(pinArray[10], HIGH);
   delay(timer*10);
   digitalWrite(pinArray[5], HIGH);
   delay(timer*10);
   digitalWrite(pinArray[1], HIGH);
   delay(timer*10);
   digitalWrite(pinArray[9], HIGH);
   delay(timer*10);
   digitalWrite(pinArray[0], HIGH);
   delay(timer*10);
   digitalWrite(pinArray[11], HIGH);
   delay(timer*10);
   digitalWrite(pinArray[2], HIGH);
   delay(timer*10);
   digitalWrite(pinArray[7], HIGH);
   delay(timer*10);
   digitalWrite(pinArray[13], HIGH);
   delay(timer*10);
   digitalWrite(pinArray[6], HIGH);
   delay(timer*10);
   digitalWrite(pinArray[12], HIGH);
   delay(timer*100);
   digitalWrite(pinArray[5], LOW);
   delay(timer*10);
   digitalWrite(pinArray[12], LOW);
   delay(timer*10);
   digitalWrite(pinArray[3], LOW);
   delay(timer*10);
   digitalWrite(pinArray[7], LOW);
   delay(timer*10);
   digitalWrite(pinArray[1], LOW);
   delay(timer*10);
   digitalWrite(pinArray[9], LOW);
   delay(timer*10);
   digitalWrite(pinArray[11], LOW);
   delay(timer*10);
   digitalWrite(pinArray[2], LOW);
   delay(timer*10);
   digitalWrite(pinArray[8], LOW);
   delay(timer*10);
   digitalWrite(pinArray[10], LOW);
   delay(timer*10);
   digitalWrite(pinArray[0], LOW);
   delay(timer*10);
   digitalWrite(pinArray[13], LOW);
   delay(timer*10);
   digitalWrite(pinArray[6], LOW);
   delay(timer*10);
   digitalWrite(pinArray[4], LOW);
   delay(timer*100);
   cycle = 0;
 }
}

Anachrocomputer

Instead of doing this:

Code: [Select]
   digitalWrite(pinArray[0], HIGH);
   digitalWrite(pinArray[1], HIGH);
   digitalWrite(pinArray[2], HIGH);
   digitalWrite(pinArray[3], HIGH);
   digitalWrite(pinArray[4], HIGH);
   digitalWrite(pinArray[5], HIGH);
   digitalWrite(pinArray[6], HIGH);
   digitalWrite(pinArray[7], HIGH);
   digitalWrite(pinArray[8], HIGH);
   digitalWrite(pinArray[9], HIGH);
   digitalWrite(pinArray[10], HIGH);
   digitalWrite(pinArray[11], HIGH);
   digitalWrite(pinArray[12], HIGH);
   digitalWrite(pinArray[13], HIGH);


You can do this:

Code: [Select]

int i;

for (i = 0; i < 14; i++) {
   digitalWrite(pinArray[i], HIGH);
}

Andrew

#2
May 01, 2009, 12:14 pm Last Edit: May 01, 2009, 12:17 pm by Andrew Reason: 1
I'm not sure the "for" lines like this:

Code: [Select]
for (cycle>34;cycle<55;cycle++){  //This section scrolls four sets of two lit LEDs across
   digitalWrite(pinArray[2], LOW);
   digitalWrite(pinArray[6], LOW);
   ...

do what you think. Looking at the whole sketch it probably does work, but those "cycle > 34" bits don't do anything.

Andrew

k2pek2

#3
May 01, 2009, 05:02 pm Last Edit: May 02, 2009, 07:37 am by k2pek2 Reason: 1
Quote

Looking at the whole sketch it probably does work, but those "cycle > 34" bits don't do anything.

Andrew


i'm not at home now so i can't test out a modified code, but i see what you mean there. And thanks for the help, Anachrocomputer, it looks like that could help me.

(as it is now, the code runs fine and does what I want, but looking at the code and explaining it to someone is made more difficult when it's messy like that)

EDIT: turns out i do need the for (cycle>34;cycle<55;cycle++){ parts (Specifically the cycle>34;). If they aren't there, the syntax for the for command is incomplete and does not work properly.

For the part about turning on/off all at once, what was suggested worked, and now the code is a lot nicer. Thanks for your help!

Mik


Vector

#5
May 02, 2009, 12:50 pm Last Edit: May 02, 2009, 12:54 pm by Vector Reason: 1
There has been a solution posted, but a simple introduction on for loops may help you understand how things are working.

Code: [Select]
for( init; condition; increment )
{
   ...
}


init allows you to initialize any variables that you may require for the loop. Usually this will involve something like i = 1.

condition lets you say what will keep the loop running. If condition is not met, the loop ends. A typical example is i < 20.

increment lets you modify variables that the loop depends on. Again, an example of this is i++.

----

Let's take a look at some of the code you've provided.

Code: [Select]
01.  cycle = 29; //This section blinks on LEDs then off, and waits to blink the next one
02.  for (cycle>28;cycle<34;cycle++){
03.    count = 13;
04.    for (count<14;count>-1;count--){
05.      digitalWrite(pinArray[count], HIGH);
06.      delay(timer*2);
07.      digitalWrite(pinArray[count], LOW);
08.      delay(timer*10);
09.    }
10  }


This is almost correct. I'll just point out a few things:
  • You don't *really* need line 1. This can be handled in your for() loop setup. (and you're already [almost] handling it anyway.
  • Change for (cycle>28; to for( cycle = 29;. This change will tell your for loop 'start this loop by setting the value of the variable cycle to 29.'
  • Similarly for lines 3 and 4, you don't need line 3, and start line 4 off with for( count = 13;.
  • A personal touch, instead of using count>-1; on line 4, you can use count >= 0;. I feel this is a nicer way to specify the same thing, as it's easier to read that 'this is what it's doing.' You also have the added benefit of eliminating problems that may occur with float numbers. For example, if you are counting down from 10.5, you go 10.5, 9.5, 8.5, ... 1.5, 0.5, -0.5, -1.5. It won't get picked up until it hits -1.5. So using count >= 0; is also a little more specific.


Add those minor changes throughout your project where applicable and you'll be laughing.  :)

Vector

Quickly scanning through your code again, I also noticed something which is not going to behave as you expect it to.

Code: [Select]
 if (cycle = 65){  //This section blinks on all LEDs one at a time, then off one at a time.

What you're doing there is saying 'set cycle to 65, and see if it worked,' instead of 'see if cycle is 65'. The code that you have there will (should) always return TRUE.

You need to change your assignment operator ( = ) to a comparison operator ( == ) for it to do what you want it to. ie:

Code: [Select]
if( cycle == 65 ) { ...

k2pek2

Quote

This is almost correct. I'll just point out a few things:

   * You don't *really* need line 1. This can be handled in your for() loop setup. (and you're already [almost] handling it anyway.
   * Change for (cycle>28; to for( cycle = 29;. This change will tell your for loop 'start this loop by setting the value of the variable cycle to 29.'
   * Similarly for lines 3 and 4, you don't need line 3, and start line 4 off with for( count = 13;.
   * A personal touch, instead of using count>-1; on line 4, you can use count >= 0;. I feel this is a nicer way to specify the same thing, as it's easier to read that 'this is what it's doing.' You also have the added benefit of eliminating problems that may occur with float numbers. For example, if you are counting down from 10.5, you go 10.5, 9.5, 8.5, ... 1.5, 0.5, -0.5, -1.5. It won't get picked up until it hits -1.5. So using count >= 0; is also a little more specific.


I've gotten rid of line one; that was from changing the amount of times each pattern would repeat and i forgot to actually fix it rather than changing the parameters in the for ()... because i was lazy...

I've done the second and third parts you've suggested, it works fine as well.

And finally changing count > -1 to count >= 0 had everything working properly.

The code beforehand was working great, but i guess it wasn't working how it SHOULD have been. But now everything is in order and squared away. Thanks for your help, everyone!

Go Up