Go Down

### Topic: Getting 4 LEDs to do different sets of things via a button (Read 4103 times)previous topic - next topic

#### JoshuaZimmerman

#15
##### Nov 18, 2011, 03:28 pmLast Edit: Nov 18, 2011, 03:38 pm by JoshuaZimmerman Reason: 1
Sorry, that was a late night mistake.  I mean case #1.

As for the code... it seems to work well.  I forget the reason why I did it.  Should I not be doing that?  (ok, someone told me I should not be doing that.  I'll change that tomorrow when I have the time.)

#16
##### Nov 18, 2011, 03:36 pm
Quote
Should I not be doing that?

I don't know, I've just never seen that syntax before. Maybe it explains the "weird flickering".

______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

#### JoshuaZimmerman

#17
##### Nov 18, 2011, 03:39 pm
Now I'm guessing that my problems with random flickering LEDs is from my poor poor coding.  Sadness. (Someone else told me not to do that.  Bad me and my coding.)

Anyway to change your "chaser" code to make it go back and forth, as opposed to just one direction?

Right now it's moving 1-2-3-4-5-6-1-2-3-4-5-6 but it would be nice to have it go 1-2-3-4-5-6-5-4-3-2-1.  Not a big deal, but eh.

#18
##### Nov 18, 2011, 04:02 pm
Quote
Now I'm guessing that my problems with random flickering LEDs is from my poor poor coding.

Use a loop like I did in setup

Code: [Select]
`case 0: for (int i = 0; i < Npins; i++) digitalWrite(pins[i], LOW);`

Quote
Anyway to change your "chaser" code to make it go back and forth, as opposed to just one direction?

The quick and dirty way to do it is simply duplicate the code and make some changes so the second loop counts down

Code: [Select]
`for (int i = 0; i < Npins; i++) { digitalWrite(pins[i], HIGH); delay(timer); digitalWrite(pins[i], LOW); delay(timer);}for (int i = Npins-1; i >= 0; i--) { digitalWrite(pins[i], HIGH); delay(timer); digitalWrite(pins[i], LOW); delay(timer);}`

______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

#### nate451

#19
##### Nov 18, 2011, 04:09 pm

Anyway to change your "chaser" code to make it go back and forth, as opposed to just one direction?

Right now it's moving 1-2-3-4-5-6-1-2-3-4-5-6 but it would be nice to have it go 1-2-3-4-5-6-5-4-3-2-1.  Not a big deal, but eh.

Code: [Select]
`case 3: { // Chase Animation // Chase up for (int i = 0; i < Npins; i++) { digitalWrite(pins[i], HIGH); delay(timer); digitalWrite(pins[i], LOW); delay(timer); } // Chase down for (int i = Npins - 1; i >= 0; i--) { digitalWrite(pins[i], HIGH); delay(timer); digitalWrite(pins[i], LOW); delay(timer); }}`

There are (as with all coding tasks) multiple ways to do this, but here's a simple way. The for loop you have for case 3 is accomplishing the chase this way:

• It starts at 0 (int i = 0)

• It will keep going as long as i is less than the number of pins, in other words until it reaches the last pin (i < Npins)

• After each time it runs, it goes to the next pin (i++)

So to have it chase the other direction, just add a for loop that does the reverse of what your first one did.

• It starts at the END of your array of pins (int i = Npins)
• It keeps going until it reaches the beginning, ie 0 (i >= 0)

• After each time it runs, it goes to the previous pin (i--)

Another good habit to start forming is frequent commenting. Leaving comments that let others (and future you) know what everything does will save you tons of time down the road.

#### udoklein

#20
##### Nov 18, 2011, 05:21 pm
This is not a shorthand

Code: [Select]
`pins[3,5,6,9,10,11]`

is a "longhand" for

Code: [Select]
`pins[11]`

Reason: the comma operator will evaluate all operands and delivers the last value as a result.

#21
##### Nov 18, 2011, 05:25 pm
Thanks Udo, I've not seen that before, probably because it serves no useful purpose

______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

#### ryschwith

#22
##### Nov 19, 2011, 12:15 am
Right now it's moving 1-2-3-4-5-6-1-2-3-4-5-6 but it would be nice to have it go 1-2-3-4-5-6-5-4-3-2-1.  Not a big deal, but eh.

I usually do something like this:
Code: [Select]
`int dir = 1; // direction controlled by sign of this variable (positive goes up, negative goes down)int pin = 0; // the pin that should currently be litint pins[] = { 3, 4, 5, 9, 10, 11 }; // array containing pins of LEDsint numPins = 6; // the total number of pins (I think you could also do sizeof pins / sizeof int);for( int i = 0; i < numPins; i++ ){    digitalWrite( pins[ i ], ( i == pin ) ? HIGH : LOW ) ); // sets output to HIGH if matches pin, or LOW if it doesn't}pin += dir; // increments pinif( pin <= 0 || pin >= numPins ) // check to see if pin has reached end of bounds{    dir *= -1; // reverse sign on dir variable}`NOTE: Untested code, so it may contain errors. The general idea is valid, though.

Go Up