100 lines
2.8 KiB
Arduino
100 lines
2.8 KiB
Arduino
|
#include <Adafruit_NeoPixel.h>
|
||
|
#define PIN 3
|
||
|
#define NUM_LEDS 150
|
||
|
// Parameter 1 = number of pixels in strip
|
||
|
// Parameter 2 = pin number (most are valid)
|
||
|
// Parameter 3 = pixel type flags, add together as needed:
|
||
|
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
|
||
|
// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
|
||
|
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
|
||
|
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
|
||
|
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRBW + NEO_KHZ800);
|
||
|
|
||
|
void setup() {
|
||
|
strip.begin();
|
||
|
strip.show(); // Initialize all pixels to 'off'
|
||
|
}
|
||
|
|
||
|
// *** REPLACE FROM HERE ***
|
||
|
void loop() {
|
||
|
BouncingBalls(0xff,0,0, 3);
|
||
|
}
|
||
|
|
||
|
void BouncingBalls(byte red, byte green, byte blue, int BallCount) {
|
||
|
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[i] = millis();
|
||
|
Height[i] = StartHeight;
|
||
|
Position[i] = 0;
|
||
|
ImpactVelocity[i] = ImpactVelocityStart;
|
||
|
TimeSinceLastBounce[i] = 0;
|
||
|
Dampening[i] = 0.90 - float(i)/pow(BallCount,2);
|
||
|
}
|
||
|
|
||
|
while (true) {
|
||
|
for (int i = 0 ; i < BallCount ; i++) {
|
||
|
TimeSinceLastBounce[i] = millis() - ClockTimeSinceLastBounce[i];
|
||
|
Height[i] = 0.5 * Gravity * pow( TimeSinceLastBounce[i]/1000 , 2.0 ) + ImpactVelocity[i] * TimeSinceLastBounce[i]/1000;
|
||
|
|
||
|
if ( Height[i] < 0 ) {
|
||
|
Height[i] = 0;
|
||
|
ImpactVelocity[i] = Dampening[i] * ImpactVelocity[i];
|
||
|
ClockTimeSinceLastBounce[i] = millis();
|
||
|
|
||
|
if ( ImpactVelocity[i] < 0.01 ) {
|
||
|
ImpactVelocity[i] = ImpactVelocityStart;
|
||
|
}
|
||
|
}
|
||
|
Position[i] = round( Height[i] * (NUM_LEDS - 1) / StartHeight);
|
||
|
}
|
||
|
|
||
|
for (int i = 0 ; i < BallCount ; i++) {
|
||
|
setPixel(Position[i],red,green,blue);
|
||
|
}
|
||
|
|
||
|
showStrip();
|
||
|
setAll(0,0,0);
|
||
|
}
|
||
|
}
|
||
|
// *** REPLACE TO HERE ***
|
||
|
|
||
|
void showStrip() {
|
||
|
#ifdef ADAFRUIT_NEOPIXEL_H
|
||
|
// NeoPixel
|
||
|
strip.show();
|
||
|
#endif
|
||
|
#ifndef ADAFRUIT_NEOPIXEL_H
|
||
|
// FastLED
|
||
|
FastLED.show();
|
||
|
#endif
|
||
|
}
|
||
|
|
||
|
void setPixel(int Pixel, byte red, byte green, byte blue) {
|
||
|
#ifdef ADAFRUIT_NEOPIXEL_H
|
||
|
// NeoPixel
|
||
|
strip.setPixelColor(Pixel, strip.Color(red, green, blue));
|
||
|
#endif
|
||
|
#ifndef ADAFRUIT_NEOPIXEL_H
|
||
|
// FastLED
|
||
|
leds[Pixel].r = red;
|
||
|
leds[Pixel].g = green;
|
||
|
leds[Pixel].b = blue;
|
||
|
#endif
|
||
|
}
|
||
|
|
||
|
void setAll(byte red, byte green, byte blue) {
|
||
|
for(int i = 0; i < NUM_LEDS; i++ ) {
|
||
|
setPixel(i, red, green, blue);
|
||
|
}
|
||
|
showStrip();
|
||
|
}
|