Pages: 1 [2]   Go Down
Author Topic: Getting 4 LEDs to do different sets of things via a button  (Read 1161 times)
0 Members and 1 Guest are viewing this topic.
Milwaukee, WI
Offline Offline
Newbie
*
Karma: 0
Posts: 19
Personal text is personal.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.)

« Last Edit: November 18, 2011, 09:38:49 am by JoshuaZimmerman » Logged

You've not lived until you've watched a middle school student try and swallow his own fist.

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 126
Posts: 8490
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Rob Gray aka the GRAYnomad www.robgray.com

Milwaukee, WI
Offline Offline
Newbie
*
Karma: 0
Posts: 19
Personal text is personal.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

You've not lived until you've watched a middle school student try and swallow his own fist.

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 126
Posts: 8490
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
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
Logged

Rob Gray aka the GRAYnomad www.robgray.com

Washington, DC
Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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.
Logged

0
Offline Offline
Faraday Member
**
Karma: 23
Posts: 3480
20 LEDs are enough
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

This is not a shorthand

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

is a "longhand" for

Code:
pins[11]

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

Check out my experiments http://blog.blinkenlight.net

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 126
Posts: 8490
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks Udo, I've not seen that before, probably because it serves no useful purpose smiley

______
Rob
Logged

Rob Gray aka the GRAYnomad www.robgray.com

Erie, Pa.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 53
Fair warning: I'm new, and I'll say stupid things.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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 lit
int pins[] = { 3, 4, 5, 9, 10, 11 }; // array containing pins of LEDs
int 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 pin
if( 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.
Logged

Pages: 1 [2]   Go Up
Jump to: