Excercise: Two leds bounce back when they meet

Hello everybody and sorry for my english :slight_smile:

I'm trying to solve the first exercise at page 46 of the Eartshine Design Manual for Arduino.

Here's the text of the exercise:

Get the LED始s at BOTH ends of the strip to start as on, then to both move towards each other, appear then move back to the end.

Unfortunatly for now it doesn't work. The leds at both ends light up, they move towards each other, then they go back when they meet but when they both reach the end, they "disappear" and no other led lights up. It's like they do just 1 cicle. The loop doesn't keep going on.

Where's the problem?

Here's the code

// BOUNCING LED


// Create array for LED pins

byte ledPin[] = {4, 5, 6, 7, 8, 9, 10, 

11, 12, 13};

int ledDelay; // delay between changes

int direction = 1;

int currentLED = 0;

int counterDirection = -1;

int secondLED = 9;

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 ms 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);
  
 // turn on the second current LED
  
 digitalWrite (ledPin[secondLED], HIGH);

 // increment by the direction value

 currentLED += direction;
  
 secondLED += counterDirection;

 // change direction if we reach the end

 if (currentLED == 4) {direction = -1;}

 if (currentLED == 0) {direction = 1;}
  
 if (secondLED == 9)  {direction = -1;}
  
 if (secondLED == 5)  {direction = 1;}

}

I attach the schemof the circuit too.

Thanks everyone and sorry for my english

I don't immediately see anything wrong with the code

After

聽currentLED += direction;
聽secondLED += counterDirection;

add

聽 Serial.print("CurLed ");
聽 Serial.print"currentLED);
聽 Serial.print("聽聽聽 SecLed ");
聽 Serial.println(secondLED);

so that you can see what is happening to those variables

...R

Thank you for the answer!
As u can imagine, I'm a newbie of Arduino.
Here's what it prints

CurLed 1 SecLed 8
CurLed 2 SecLed 7 
CurLed 3 SecLed 6 
CurLed 4 SecLed 5 
CurLed 5 SecLed 4 
CurLed 6 SecLed 3 
CurLed 7 SecLed 2 
CurLed 8 SecLed 1 
CurLed 9 SecLed 0 
CurLed 10 SecLed -1 
CurLed 11 SecLed -2 
CurLed 12 SecLed -3

I let the code run for a while and it keeps going this way, whit CurLed with always bigger positive numbers and SecLed with always bigger negative numbers.

I think that what we want to read should be something like this

CurLed 0 SecLed 9
CurLed 1 SecLed 8 
CurLed 2 SecLed 7 
CurLed 3 SecLed 6 
CurLed 4 SecLed 5 
CurLed 3 SecLed 6 
CurLed 2 SecLed 7 
CurLed 1 SecLed 8 
CurLed 0 SecLed 9 
CurLed 1 SecLed 8 
CurLed 2 SecLed 7 
CurLed 3 SecLed 6

etc.

Thanks. That is exactly the required information.

I think the problem is these lines

if (secondLED == 9)聽 {direction = -1;}
聽if (secondLED == 5)聽 {direction = 1;}

should be

if (secondLED == 9)聽 {counterDirection = -1;}
聽if (secondLED == 5)聽 {counterDirection = 1;}

Well, try that and report the results.

...R

Yes! Now it works!
Thank u so much for the help.

It was a really stupid mistake but i wasn't able to find it.

Thank you man

Redocram:
It was a really stupid mistake but i wasn't able to find it.

Been there, done that. :slight_smile:

Glad it's working.
And you have gained some debugging experience.

...R