LED Chase problems (Mark Anderson Book)

Hello,

I am completely new to Arduino and am currently working through the booklet. I am rather stuck on one exercise at the end of Project 5.
It says that I should tweak only the last few lines of the code so the LEDs do not bounce forward and back, but instead restart each time it reaches the end. Everything I try seems to make the LEDs do strange things.
There was a topic on this forum I saw but it didn’t help much, as the answers on there changed the code significantly. I also did not understand much because I am a beginner at this. Is there any way to do this as it says in the book?

// Project 5 - LED Chase Effect 
// Create array for LED pins 
byte ledPin[] = {4, 5, 6, 7, 8, 9, 10, 11, 12, 13}; 
int ledDelay(65); // time delay between changes 
int direction = 1; 
int currentLED = 0; 
unsigned long changeTime; 
void setup() { 
// set all pins to output 
for (int x=0; x<10; x++) { 
pinMode(ledPin[x], OUTPUT); } 
changeTime = millis(); 
} 
void loop() { 
// 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); 
// increment by the direction value 
currentLED += direction; 
// change direction if we reach the end 
if (currentLED == 9) {direction = -1;} 
if (currentLED == 0) {direction = 1;} }

I did want to solve it myself, but I just end up getting annoyed with it and getting no where in the end.

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

These are the lines of code that test whether the current LED is at the end of the line and if so change the increment so that the effect is to bounce the lit LED backward and forward

If you were to change

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

so that instead of changing the increment it put the current LED number back to zero, I wonder what would happen ? If you did that then the test for the current LED being zero and the change of direction to 1 would not be needed because direction would already equal 1.

This line looks wrong:
int ledDelay(65);

I think you mean:
int ledDelay = 65;

--Michael

It looks wrong but it works the same as the corrected version in that it sets ledDelay to 65

I did not know that. Thanks, UKHeliBob, for the tidbit.
–Michael

UKHeliBob:

 if (currentLED == 9) 

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



These are the lines of code that test whether the current LED is at the end of the line and if so change the increment so that the effect is to bounce the lit LED backward and forward

If you were to change


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



so that instead of changing the increment it put the current LED number back to zero, I wonder what would happen ? If you did that then the test for the current LED being zero and the change of direction to 1 would not be needed because direction would already equal 1.

Brilliant thanks, that worked. I had tried something similar but was coding it wrong in some way - it makes alot of sense now with your explanation.