Page 1 of 1
Forum

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is no translation for that post!




Bouncy Ball Effect
 
Share:
Notifications
Clear all

[Solved] Bouncy Ball Effect

4 Posts
2 Users
0 Likes
1,598 Views
(@toastycoast)
Active Member
Joined: 7 years ago
Posts: 4
Topic starter  

Ran into a little problem with both the regular and color bouncy ball effect. For whatever reason the process never completes and it loops forever.

A bug or a feature? Would really like it to just run once and then I can drop it in a FOR statement and loop it however many times I need.

Thanks!


   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 11 years ago
Posts: 2663
 

Hi Toastycoast,

it's a feature hahah ... we want the ball to keep bouncing.

To limit the bouncing, change the code where it says "while(true)" (which is an indefinite loop, keeps going and going) to something like this (I added "//" to disable the line and at the end to disable the closing bracket "}" - identified the 2 lines by adding "// <--- change here!").

void loop() {
  byte colors[3][3] = { {0xff, 0,0}, 
                        {0xff, 0xff, 0xff}, 
                        {0 , 0 , 0xff} };
  BouncingColoredBalls(3, colors);
}
void BouncingColoredBalls(int BallCount, byte colors[][3]) {
  float Gravity = -9.81;
  int StartHeight = 1;
  
  float Height[BallCount];
  float ImpactVelocityStart = sqrt( -2 * Gravity * StartHeight );
  float ImpactVelocity[BallCount];
  float TimeSinceLastBounce[BallCount];
  int Position[BallCount];
  long ClockTimeSinceLastBounce[BallCount];
  float Dampening[BallCount];
  
  for (int i = 0 ; i < BallCount ; i++) {   
    ClockTimeSinceLastBounce = millis();
    Height = StartHeight;
    Position = 0; 
    ImpactVelocity = ImpactVelocityStart;
    TimeSinceLastBounce = 0;
    Dampening = 0.90 - float(i)/pow(BallCount,2); 
  }
  // while (true) { // <--- change here!
    for (int i = 0 ; i < BallCount ; i++) {
      TimeSinceLastBounce = millis() - ClockTimeSinceLastBounce;
      Height = 0.5 * Gravity * pow( TimeSinceLastBounce/1000 , 2.0 ) + ImpactVelocity * TimeSinceLastBounce/1000;
  
      if ( Height < 0 ) {                      
        Height = 0;
        ImpactVelocity = Dampening * ImpactVelocity;
        ClockTimeSinceLastBounce = millis();
  
        if ( ImpactVelocity < 0.01 ) {
          ImpactVelocity = ImpactVelocityStart;
        }
      }
      Position = round( Height * (NUM_LEDS - 1) / StartHeight);
    }
  
    for (int i = 0 ; i < BallCount ; i++) {
      setPixel(Position,colors[0],colors[1],colors[2]);
    }
    
    showStrip();
    setAll(0,0,0);
 // } // <--- change here!
}

Hope this helps ... 


   
ReplyQuote
(@toastycoast)
Active Member
Joined: 7 years ago
Posts: 4
Topic starter  

Rather than commenting out the while statement I replaced it with a for statement so it would loop for a bit longer. Any downside to going that route instead?


   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 11 years ago
Posts: 2663
 

Using a for loop is just fine as well ... do not see any downside to that. 


   
ReplyQuote

Like what you see and you'd like to help out? 

The best way to help is of course by assisting others with their questions here in the forum, but you can also help us out in other ways:

- Do your shopping at Amazon, it will not cost you anything extra but may generate a small commission for us,
- send a cup of coffee through PayPal ($5, $10, $20, or custom amount),
- become a Patreon,
- donate BitCoin (BTC), or BitCoinCash (BCH).

Share: