McRoberts - Beginning Aruino, Help with Chapter 3 Exercise 2

Hi,

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 :slight_smile:

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;}
}

Moderator edit: Similies eliminated.

Post your code using the # icon then it doesn't get messed up like your code has and we can see it all.

Here is the original code which I must work with

Sounds like homework?

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..

Hi guys,

I'm new in the arduino world and I'd like to as if some of you have the exercise 1 of the chapter 3?

Please help me.

Thanks

byte ledpin[]={4,5,6,7,8};
byte ledpin1[]={13,12,11,10,9};
int direction=1;
int leddelay=300;
int curled1=0;
int curled2=0;
//unsigned long changetime;
void setup(){
for(int x=0;x<5;x++){
pinMode(ledpin[x],OUTPUT);
pinMode(ledpin1[x],OUTPUT);
}
}
void loop(){
delay(leddelay);
changeled();
}
void changeled(){
for(int x=0;x<5;x++){
digitalWrite(ledpin[x],LOW);
digitalWrite(ledpin1[x],LOW);
}
digitalWrite(ledpin[curled1],HIGH);
digitalWrite(ledpin1[curled2],HIGH);
curled1=curled1+direction;
curled2=curled2+direction;
if(curled1==4 && curled2==4){
direction=-1;
}
if(curled1==0 && curled2==0){
direction=1;
}
}

Well, I'm sure that that incorrectly posted code will be just about useless to Fmendes86.