ASK manual - Project 6, Exercise 2... help

  1. Make a bouncing ball effect by making the LED start at one end, [ch699]drop[ch700] toward the other end, bounce back up, but to only go up 9 spaces, bounce, go up 8 spaces, then 7, then 6, etc. To give the effect it is a bouncing ball, getting bouncing up to a lower height on each bounce.

Base Source Code:

byte ledPin = {4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
int ledDelay;
int direction = 1;
int currentLED = 0;
unsigned long changeTime;
int potPin = 2;

void setup() {
for (int x=0; x<10; x++) {
pinMode (ledPin, OUTPUT); }
changeTime = millis();
}

void loop() {
ledDelay = analogRead(potPin);
if ((millis() - changeTime) > ledDelay) {
changeLED();
changeTime = millis();
}
}

void changeLED() {
for (int x=0; x<10; x++) {
digitalWrite(ledPin, LOW);
}
digitalWrite(ledPin[currentLED], HIGH);
currentLED += direction;
if (currentLED == 9) {direction = -1;}
if (currentLED == 0) {direction = 1;}
}


Right, so my idea was to use a variable like so:

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

  • added int bounce = 9 as a global int to start at 9

Having it decay each time the currentLed came up to the top. So my code is wrong and I’m sure I can’t do one of those calculation like I have written them. Somebody please teach me the error of my way.

Thanks

So my code is wrong

Wrong in that it won't compile, won't upload, or won't won't work?

If it doesn't work, what does it do that it is not supposed to do, or not do that it is supposed to do?

If it doesn't compile, what error messages are you seeing?

Your line:-

if (currentLED == bounce) {direction = -1, bounce -= 1;}

should be if (currentLED == bounce) {direction =-1; bounce -= 1;}

note the added ;

Wrong in that it won't compile, won't upload, or won't won't work?

Won't Work.

should be if (currentLED == bounce) {direction =-1; bounce -= 1;}

OK that makes sense, thanks GM. I'll give it a shot when I get home tonight.

Won't Work.

It would be good to say what wouldn't work, in other words how it behaves in contrast to how you would like it to behave.

Also when posting code can you use the # icon in the reply box.

so im working on the same ASKmanual right now from eartshinedesign wich is now offline because he is working on a complete book.

anyhow the bouncing ball exercise.

just like the TS i came to the same conclusion for maken the light decade one led every time it hits 0 but it just doesnt work it keeps shifting to the 10 led.

Can someone pls help me find the problem.

here is the code.

void changeled()
{
for (byte x=0; x<10; x++)
{
digitalWrite(ledpin, LOW);
}
digitalWrite(ledpin[currentled], HIGH);
currentled += dir;

if (currentled == bounce) // bounce is global variable set at 9 to start with
{
dir = -1;
bounce -= 1;
}
if (currentled == 0)
{
dir = 1;
}
if (bounce == 0)
{
bounce = 9;
}
}

so to be precise.

the led start ad led 10 than scrols back to led 0 when it hit zero bounce should be 8 so that it goes back up to led 9 an then back to zero when it hit zero again bounce has to become 8 so it scrols back to led 7 repeat this steps till it hits zero en the ball has no kinectic energy left. When 0 is reached bounce become 9 again and the process starts over and over and over again.

ok put bounce -= 1 under current == 0 instead of under currentled == bounce and now it works flawless ;D