Bouncing Ball

This is one of the first sketches I implemented for the Blinkenlight Shield. In some sense this sketch is the reason why I invented the shield at all. Actually I was planning a 100+ LED version but then considered the complex setup with shift registers and the like. Then I decided that I would go for a quick prototype with whatever I find in my junk box and 20 LEDs have to suffice for that.

So here it is. The first small light show for the Blinkenlight Shield.

//
//  www.blinkenlight.net
//
//  Copyright 2011 Udo Klein
//
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program. If not, see http://www.gnu.org/licenses/


void setup() {
    for (uint8_t pin=0; pin<20; ++pin) {
        pinMode(pin, OUTPUT);
    }
}

void blink(const uint8_t pos) {
    digitalWrite(pos, HIGH);
    delay((sqrt(((float)20 - pos)/20) - sqrt((19.0 - pos)/20)) * 500);
    digitalWrite(pos, LOW);
}

void loop() {
    for (uint8_t pos=0; pos<19; ++pos) {
        blink(pos);
    }
    for (uint8_t pos=19; pos>0; --pos) {
        blink(pos);
    }
}

Basically it counts up and down and blinks at the position corresponding to the counter. In order to get a realistic bounce effect the on time of the LEDs is varied. The underlying formula is the well known h = g*t^2/2. Since the program varies h, we have to solve for t: t = sqrt(2*h/g) = sqrt(h)*sqrt(2/g). The important part is that this is a square root function. The constants are not really important. I did not compute them but experimented a little bit to make the result „look natural“.

As the video shows we get a nice bouncing effect for almost no implementation effort. If you like it you might also like the Knight Rider effect.

5 Responses to Bouncing Ball

  1. Paul says:

    This is a better rendition of how my pasted sketch looks like. note in particular “pin<20” which change to “pin>20” when I send it. Very odd.

  2. jim lewis says:

    Hi, this will sound very dumb to an experienced user, but having just received my Arduino and Blinkenlight shield I don’t want to damage anything.
    First, there are jumpers on the blinkenlight. Do I need to set any of these prior to use?
    Second, do I simply mount the shield on the Arduino Uno, (when powered off) and then on power download your sketch?
    Thanks

  3. matulamatete says:

    hallo zusammen, hier ein video des bouncing balls in der disco. 20 dioden eigebohrt in eine pvc doppelstegplatte. das ganze verdrahtet und mit dem blinkenlighty verbunden (pfostenstecker).

Leave a reply to blinkenlightblog Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.