I need help seuencing 6 LED's with Potentiometer on Arduino to have lights move to end of line then return to the start?

OK - I have an arduino duemilanove. I am a novice trying to learn new tricks. I have connected up  a 6 LED chaser with a potentiometer controlling its speed. I don't want the light to go back and forth accross the line of LEDs knightrider style - I have tried this and it works no problem. No instead I want it to go to the end, then return to first LED. Can anyone help me with the code? I have the ASK Manual code which I want to alter for it?
this is the ASK manual code that makes the LED chaser move back and forth - what do I need to change / add?

 // Create array for LED pins
byte ledPin[] = {8, 9, 10, 11, 12, 13};
int ledDelay; // delay between changes
int direction = 1;
int currentLED = 0;
unsigned long changeTime;
int potPin = 2;    // select the input pin for the potentiometer
void setup() {
  // set all pins to output
  for (int x=0; x<6; x++) {
    pinMode(ledPin[x], OUTPUT); }
    changeTime = millis();
}
void loop() {
// read the value from the pot
ledDelay = analogRead(potPin);   
  // if it has been ledDelay ms since last change
  if ((millis() - changeTime) >
ledDelay) {
    changeLED();
    changeTime = millis();
  }
}
void changeLED() {
  // turn off all LED's
  for (int x=0; x<6; x++) {
    digitalWrite(ledPin[x], LOW);
  }
  // turn on the current LED
  digitalWrite(ledPin[currentLED], HIGH);
  // increment by the direction value
  currentLED += direction;
  // change direction if we reach the end
  if (currentLED == 5) {direction = -1;}
  if (currentLED == 0) {direction = 1;}
}


sort by: active | newest | oldest
SaiomkarK2 years ago

can any one provide me a code such that when potentiometer is in range of 0-250 there should be a sequence of led's....and when potentiometer is in range of 250 -500 the another sequence of led's and for rest of potentiometer voltages there should be another sequence of led's

gmoon7 years ago
I haven't examined the rest of the code, but here's how to alter the for loop with "C" code, so it counts down from 5 to 0:

for (int x=5; x<0; x--) {

// insert code here

}

This loop will begin at 5, loop down to 0, then exit.

A for loop has three statements:
for (start value for the variable, a conditional test, a modifier for the variable )

The above modifier (x--) decrements the x variable, rather than increment it.

When the conditional statement is true, the for loop exits.
frollard7 years ago
Lemonie has the simplest modification of the existing code, but really sean's pseudocode has it exactly right and a much simpler code:

use a single for loop in the changeled function:
changeled(){
for x = 0 to 5
 <I would recommend making a function that checks the pot and adjusts delay, calling it here> checkpot;
turn on x   'enable led x
delay(delayamount) 'wait while led x is on
turn off x   'disable led x
next x
}

void checkpot();
 delay = analogReadblablabla 'set the delay variable to the pot value
end checkpot

The main loop already takes care of doing it lots of times.  Checking for the new delay every iteration may or may not be a good thing - for extremely slow delays its good to abort out of a delay so you dont have to wait a long time...
seandogue7 years ago
in pseudo code

x = 0
while x == 0 {
    For i = 0 to 5
        turn on i
        wait time
        turn off i
    next i
}

change x to a non zero number to stop

Sorry I can't quote exact code.. I'm not an arduino user
lemonie7 years ago
Looking at the code I'd say you need to take out the change direction bit, and replace it with "start again". I.e. if (current LED == 5) -reset current LED

(I don't know this code but it reads easily enough - you'd want a currentLED == 0 in there or something)

L

lemonie lemonie7 years ago
Guessing, replace:
  if (currentLED == 5) {direction = -1;}
  if (currentLED == 0) {direction = 1;}
With:
  if (currentLED == 5) {currentLED = 0;}