I am working my way through Michael McRoberts great book - Beginning Arduino. I am stuck on working out how to achieve an exercise which is explained below. I have tried variations on every line of code and am stumped. Seeing as it is so early in the book it must be very simple but I just cant work it out. Your assistance is most appreciated, I will finally be able to sleep
The exercise is to:
"Make a bouncing ball effect so that a led starts at the bottom and bounces up to the top 10th led then back to the bottom then only up to the 9th led then back down, then up to the 8th led, then back down and so on to simulate a bouncing ball losing momentum after each bounce"
Here is the original code which I must work with - I know I could achieve the same affect by using a sequence of digitalWrite commands but the purpose is to achieve this by some how modifying the code below:
// Project 5 - LED Chase Effect
byte ledPin[] = {4, 5, 6, 7, 8, 9, 10, 11, 12, 13}; Â // Create array for LED pins
int ledDelay(65); Â // delay between changes
int direction = 1;
int currentLED = 0;
unsigned long changeTime;
void setup() {
 for (int x=0; x<10; x++) {   // set all pins to output
  pinMode(ledPin[x], OUTPUT); }
  changeTime = millis();
}
void loop() {
 if ((millis() - changeTime) > ledDelay) {    // if it has been ledDelay ms since last change
 changeLED();
  changeTime = millis();
 }
}
void changeLED() {
 for (int x=0; x<10; x++) {     // turn off all LED's
 digitalWrite(ledPin[x], LOW);
 }
 digitalWrite(ledPin[currentLED], HIGH); // turn on the current LED
currentLED += direction; // increment by the direction value
 // change direction if we reach the end
 if (currentLED == 9) {direction = -1;}
 if (currentLED == 0) {direction = 1;}
}
I too am having difficulty getting this sketch to work. I got the last 5 (9-5) going back and forth but I can't get the first 5 to play nice. I modified it to add a second led "led2" to make it go back and forth. Anyway here is what I did ..please let me know how to fix it so I can figure it out and make it work and understand where I went wrong.
// Project 5 - LED Chase Effect
byte ledPin[] = {4, 5, 6, 7, 8, 9, 10, 11, 12, 13}; // Create array for LED pins
int ledDelay(65); // delay between changes
int direction = 1;
int direction2 = -1;Â // I added this for the second LED
int currentLED = 0;
int led2 = 9;Â Â Â Â // Make second LED 9
unsigned long changeTime;
void setup() {
for (int x=0; x<10; x++) { // set all pins to output
pinMode(ledPin[x], OUTPUT); }
changeTime = millis();
}
void loop() {
if ((millis() - changeTime) > ledDelay) { // if it has been ledDelay ms since last change
changeLED();
changeTime = millis();
}
}
void changeLED() {
for (int x=0; x<10; x++) { // turn off all LED's
digitalWrite(ledPin[x], LOW);
}
digitalWrite(ledPin[currentLED], HIGH); // turn on the current LED
digitalWrite(ledPin[led2], HIGH);Â //turnon led 2
currentLED += direction; // increment by the direction value
led2 += direction2; // change LED direction
// change direction if we reach the end
if (currentLED == 9) {direction = -1;}
if (currentLED == 5) {direction = 1;}
if (led2==0) {direction2 = 1;} // Change LED2 direction
if (led2==4) {direction2 =1;}Â // Change LED2 direction
}/code]
Thank you.
Andrew
That code is very hard to read. I suggest that you put each { and each } on new lines, and use Tools + Auto Format to properly indent the code. Post the revised code.
Second, "I can't get the first 5 to play nice." doesn't tell us what they actually do, or what you want them to do. Try again.
I was out looking for help myself when I ran into this thread..I didn't find help...but I did figure it out..
The original code stayed mostly in tack. I created a new variable I called ledIndex and initialized it's value at 9. I then expanded the First IF statement in the changeLED function:
int ledIndex = 9;
void changeLED(){
 // If Direction is going back to LED 0 (-1) and currentLed is the ledIndex then
 // do nothing if(expression) ;
 if(direction == -1 && currentLed == ledIndex);
 //Else IF Change Direction and Decrement ledIndex (Index goes 9,8,7,6...)
 else if (currentLed == ledIndex){
 direction = -1;
 --ledIndex;
 }
 //Reset ledIndex back to its original value
 if(ledIndex == 0)
 ledIndex = 9;
}
I hope this helps..and I'm sure there are other ways to solve this..if someone has another way I would love to see the implementation..