Arduino Forum

Forum 2005-2010 (read only) => Software => Troubleshooting => Topic started by: Darwin74 on Jun 28, 2010, 11:14 pm

Title: Newbie Question
Post by: Darwin74 on Jun 28, 2010, 11:14 pm
I am creating an LED bouncing ball effect with 10 LED's set up as an array, bouncing from 9 - 0 - 8 - 0 ...... and want to use a simple method of coding the last highest LED (-1) as the maximum 'bounce point' and then returning to 0 and so on. I am unsure of how to most effectively use the minus sign.

I did have success using the following code to get the LED to bounce from LED 10 back to 0 and then to 9:

Code: [Select]
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;
 if (currentLED == 0) {direction = 1;}
 if (currentLED == currentLED1) {direction = -1;}


with int currentLED = 9
      int currentLED1 = 8

However, trying to use this principle any futher does not seem to work.

Any help would be appreciated.
Title: Re: Newbie Question
Post by: drhex on Jun 29, 2010, 08:11 am
well, if the limits are 0 and currentLED1 ("maxLED" might be less confusing), then let currentLED start somewhere between those extremes or you might "bounce" outside the allowed range.
Title: Re: Newbie Question
Post by: Darwin74 on Jun 30, 2010, 10:40 pm
Thanks for that, makes sense.

I will give it a shot and report back.

Thanks again for your help