controling multiple LEDs

I'm very new to Ardunio programming and need a little guidance. I'm trying to create a chase light that lights one led then two then three then the first led goes off and a fourth comes on and so fourth. I have a sketch that works but it seem very clumsy. Is there a way to simplify this? I am adding code snippets to the message so hopefully it makes sense what I'm trying to do.

[ch65279]int potpin = 0;
int led3 = 2;
int led4 = 3;
int led5 = 4;
int led6 = 5;
int led7 = 6;
int led8 = 7;
int led9 = 8;
int led10 = 9;
int dly = 25;
void setup()
{

pinMode (led3,OUTPUT);
pinMode (led4,OUTPUT);
pinMode (led5,OUTPUT);
pinMode (led6,OUTPUT);
pinMode (led7,OUTPUT);
pinMode (led8,OUTPUT);
pinMode (led9,OUTPUT);
pinMode (led10,OUTPUT);
}
void loop()
{
dly = analogRead(potpin)/10;
digitalWrite(led3,HIGH);
digitalWrite(led4,LOW);
digitalWrite(led5,LOW);
digitalWrite(led6,LOW);
digitalWrite(led7,LOW);
digitalWrite(led8,LOW);
digitalWrite(led9,LOW);
digitalWrite(led10,LOW);
delay (dly);
dly = analogRead(potpin)/10;
digitalWrite(led3,HIGH);
digitalWrite(led4,HIGH);
digitalWrite(led5,LOW);
digitalWrite(led6,LOW);
digitalWrite(led7,LOW);
digitalWrite(led8,LOW);
digitalWrite(led9,LOW);
digitalWrite(led10,LOW);
delay (dly);
dly = analogRead(potpin)/10;
digitalWrite(led3,HIGH);
digitalWrite(led4,HIGH);
digitalWrite(led5,HIGH);
digitalWrite(led6,LOW);
digitalWrite(led7,LOW);
digitalWrite(led8,LOW);
digitalWrite(led9,LOW);
digitalWrite(led10,LOW);
delay (dly);
dly = analogRead(potpin)/10;
digitalWrite(led3,LOW);
digitalWrite(led4,HIGH);
digitalWrite(led5,HIGH);
digitalWrite(led6,HIGH);
digitalWrite(led7,LOW);
digitalWrite(led8,LOW);
digitalWrite(led9,LOW);
digitalWrite(led10,LOW);
delay (dly);
dly = analogRead(potpin)/10;
digitalWrite(led3,LOW);
digitalWrite(led4,LOW);
digitalWrite(led5,HIGH);
digitalWrite(led6,HIGH);
digitalWrite(led7,HIGH);
digitalWrite(led8,LOW);
digitalWrite(led9,LOW);
digitalWrite(led10,LOW);
delay (dly);
dly = analogRead(potpin)/10;
digitalWrite(led3,LOW);
digitalWrite(led4,LOW);
digitalWrite(led5,LOW);
digitalWrite(led6,HIGH);
digitalWrite(led7,HIGH);
digitalWrite(led8,HIGH);
digitalWrite(led9,LOW);
digitalWrite(led10,LOW);
delay (dly);
dly = analogRead(potpin)/10;
digitalWrite(led3,LOW);
digitalWrite(led4,LOW);
digitalWrite(led5,LOW);
digitalWrite(led6,LOW);
digitalWrite(led7,HIGH);
digitalWrite(led8,HIGH);
digitalWrite(led9,HIGH);
digitalWrite(led10,LOW);
delay (dly);
dly = analogRead(potpin)/10;
digitalWrite(led3,LOW);
digitalWrite(led4,LOW);
digitalWrite(led5,LOW);
digitalWrite(led6,LOW);
digitalWrite(led7,LOW);
digitalWrite(led8,HIGH);
digitalWrite(led9,HIGH);
digitalWrite(led10,HIGH);
delay (dly);
dly = analogRead(potpin)/10;
digitalWrite(led3,LOW);
digitalWrite(led4,LOW);
digitalWrite(led5,LOW);
digitalWrite(led6,LOW);
digitalWrite(led7,LOW);
digitalWrite(led8,LOW);
digitalWrite(led9,HIGH);
digitalWrite(led10,HIGH);
delay (dly);
dly = analogRead(potpin)/10;
digitalWrite(led3,LOW);
digitalWrite(led4,LOW);
digitalWrite(led5,LOW);
digitalWrite(led6,LOW);
digitalWrite(led7,LOW);
digitalWrite(led8,LOW);
digitalWrite(led9,LOW);
digitalWrite(led10,HIGH);

There is more code that goes in the other direction.

any guidance will be appreciated.

Art

dunno if it works, but maybe something like this?

byte pins[]={2,3,4,5,6,7,8,9};
#define numPins (sizeof(pins)/sizeof(byte))
#define numTrails 3
#define potpin 0

void setup(){
for(int x = 0; x < numPins; x++)
pinMode (pins[x],OUTPUT);
}

void loop(){
for(int head = -1 ; head <= numPins + numTrails; head++){
int tail=head-numTrails;
if(head>=0 && head < numPins)
digitalWrite(pins[head],HIGH);//turn on the head
if(tail>=0 && tail < numPins)
digitalWrite(pins[tail],LOW);//erase last part of tail
delay (analogRead(potpin)/10);
}
}

Hi Art,

How about something like this?

[ch65279]int potpin = 0;
int ledcount = 8;
int leds[] = {2, 3, 4, 5, 6, 7, 8, 9};
int position = -1; // position varies from -1 to 8, defining the center of the 3 leds to illuminate

void setup()
{
for (int i=0; i<ledcount; ++i)
pinMode(leds*, OUTPUT);*
}
void loop()
{

  • int dly = analogRead(potpin)/10;*
  • for (int i=0; i<ledcount; ++i) // loop through all the leds*
  • if (i>=position-1 && i<=position+1) // if the led is in the group of 3 surrounding "position"*
    _ digitalWrite(leds*, HIGH); // illuminate it*_
    * else*
    _ digitalWrite(leds*, LOW); // otherwise turn it off*
    * delay (dly);
    if (position++ == ledcount) // increment position; and when it gets to 8, start over.
    position = -1;
    }*
    This code illuminates the three LEDs surrounding "position". I think it works exactly the same way your original code did, but is smaller and more manageable. You should be able to use the same idea for chasing the other way... just decrement "position" instead of increment.
    Good luck,
    Mikal_

Ha! Brilliant solution, dcb. :slight_smile:

LOL :slight_smile:

Art, I think we have concurrence on the structure :slight_smile:

good morning all, What elegant solutions. I'll try them when I get to work later today. It will take me a while to understand the code but thats what learning is all about.

thanks
art

Here is where things stand. dcb, sorry your code didn't work and since my debugging skills aren't even as good as my programming skills :-X I don't know what the problem is. mikalhart, your code worked very well until I tried to get it to decrement. I have not been able to get the lights to flash in the reverse direction. I added another def called ledcount1 = 0 and changed the second for statement to (int i=9; i>ledcount1; --i) then I change the last if statement to (position-- == ledcount). Am I at least in the right ballpark for getting this to reverse directions?

Thanks for the help
art

One more question. Is this language C or a variant of C? perhaps I can find a programming book that will help me get started.

THANKS
Art

Art, this is C++, which is (mostly) a superset of C. Any C or C++ book should give you 95% of what you need.

To change my example to decrement, replace this

if (position++ == ledcount) // increment position; and when it gets to 8, start over.
   position = -1;

with this

if (position-- == -1) // decrement position; and when it gets to -1, start over at 8.
   position = ledcount;

By simply changing the value of "position" however which way you want, you can move the LEDs up and down. There should never be any need to modify the for(i...) loop. It's just setting all eight LEDs to their correct states each time through loop().

Mikal

Thanks again mikalhart, I found a tutorial on the arduino site that describes the command set and syntax. So with that in hand and some other examples I may eventually get a handle on the logic of your code and be able to make intellegent modifications.

Thanks

art