LED Chasing

I am new to Arduino and have ben working through various examples in the Arduino Starter Kit Manual.

I have just completed the ‘Interactive LED chase Effect’

// create array for led's
byte ledPin[] = {4, 5, 6, 7, 8, 9, 10, 11, 12, 13}; 
int ledDelay; // delay between changes
int currentLED = 0;
int direction = 1;
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<10; x++) {
    pinMode(ledPin[x], OUTPUT);}
    changeTime = millis();

}

void loop() {
  // read the value from the pot
  ledDelay = analogRead(potPin);
  // if it has been ledDelay since last change
  if ((millis() - changeTime) >
  ledDelay) {
    changeLED();
    changeTime = millis();
  }
}
void changeLED() {
  // turn off all LED's
  for (int x=0; x<10; 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 == 9) {direction = -1;}
  if (currentLED == 0) {direction = 1;}
  }

and now need to get the LED’s at both ends of the strip to start on, then move towards each other, appear to bounce off each other and then move back to the end.

I have tried additional code to make this happen with varying success but can’t seem to nail it.

Any help would be appreciated.

Below you will find an idea on how to code it.

  • This is untested code mainly designed to show the idea, I don’t have your setup on a board right off.

  • but it may work :slight_smile:

void changeLED() {
  // turn off all LED's
  for (int x=0; x<10; x++) {
    digitalWrite(ledPin[x], LOW);
  }
  // compute the mirror image of the current LED
  //01234
  // - mirror image is -
  //98765
  int opLED = (9-currentLED);
  // turn on the current LED
  digitalWrite(ledPin[currentLED], HIGH);
  // turn on the mirror image of the current LED
  digitalWrite(ledPin[opLED], HIGH);
  // increment by the direction value
  currentLED += direction;
  // change direction if we reach the middle
  if (currentLED == 4) {
    direction = -1;
    //-- this would be a nice place to add a kaboom maybe
  }
  if (currentLED == 0) {
    direction = 1;
  }
}

Hey marklar,

thanks for the code, I managed to sort it out late last night. It does look similar t the code you posted.

Thanks again