Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Gravball
- //
- // By: Andrew Tuline
- //
- // Date: Oct, 2014
- //
- // Multiple bouncing balls. For whatever reason, I call them gravs.
- //
- // FastLED 2.1 is available at https://github.com/FastLED/FastLED/tree/FastLED2.1
- //
- #include "FastLED.h" // FastLED library. Preferably the latest copy of FastLED 2.1.
- // Fixed definitions cannot change on the fly.
- #define COLOR_ORDER GRB // Are they RGB, GRB or what??
- #define LED_TYPE WS2812B // What kind of strip are you using?
- #define NUM_LEDS 18 // Number of LED's
- // Initialize changeable global variables.
- uint8_t max_bright = 128; // Overall brightness definition. It can be changed on the fly.
- struct CRGB Lleds[NUM_LEDS]; // Initialize our LED array.
- struct CRGB Rleds[NUM_LEDS];
- // Initialize global variables for sequences
- int thisdelayR = 15; // A delay value for the sequence(s)
- int thissatR = 255;
- int thisbrightR = 255;
- int thisdelayL = 15; // A delay value for the sequence(s)
- int thissatL = 255;
- int thisbrightL = 255;
- // GRAVBALL VARIABLES
- int gravityR = -15;
- int dragR = 0; // Not required due to losses in 16 bit math.
- int timeincR = 2;
- int gravityl = -15;
- int dragL = 0; // Not required due to losses in 16 bit math.
- int timeincL = 2;
- #define numgravsR 3 // How many gravs we are using.
- #define numgravsL 3
- typedef struct { // Define a structure for the gravs.
- int distoldR, distoldL;
- int distanceR, distanceL;
- int veloldR, veloldL;
- int velocityR, velocityL;
- int thishueR, thishueL;
- gravsR, gravsL;
- )
- gravsR mygravsR[numgravsR];
- gravsL mygravsl[numgravsL];
- void setup() {
- Serial.begin(57600);
- LEDS.addLeds<LED_TYPE, 13, COLOR_ORDER>(Rleds, NUM_LEDS).setCorrection(TypicalLEDStrip);
- LEDS.addLeds<LED_TYPE, 11, COLOR_ORDER>(Lleds, NUM_LEDS).setCorrection(TypicalLEDStrip);
- FastLED.setBrightness(max_bright);
- set_max_power_in_volts_and_milliamps(5, 500); // FastLED 2.1 Power management set at 5V, 500mA
- } // setup()
- void loop () {
- gravball();
- show_at_max_brightness_for_power();
- delay_at_max_brightness_for_power(thisdelayR*2.5);
- LEDS.countFPS();
- } // loop()
- void gravball() {
- fill_solid(Rleds, NUM_LEDS, CRGB::Black);
- for (int R=0; R < numgravsR; R++) {
- mygravsR[R].velocityR = mygravsR[R].veloldR + gravityR*timeincR; // Split gravity math into two lines for simplicity
- mygravsR[R].distanceR = mygravsR[R].distoldR + mygravsR[R].velocityR*timeincR;
- int Ri = map(mygravsR[R].distanceR, 0, 32767, 0, NUM_LEDS);
- mygravsR[R].veloldR = mygravsR[R].velocityR; // Capture the current velocity/distance
- mygravsR[R].distoldR = mygravsR[R].distanceR;
- if (Ri <= 1 && abs(mygravsR[R].veloldR) < 700 ) {mygravsR[R].veloldR = 0; mygravsR[R].distoldR=random16(0,8000)+26000; mygravsR[R].thishueR=random8(255);} // Reset!!!
- if (Ri <= 1 && mygravsR[R].veloldR<0) {mygravsR[R].veloldR = -mygravsR[R].veloldR;} // Bounce!!!
- Rleds[Ri] += CHSV(mygravsR[R].thishueR, thissatR, thisbrightR); // Let's get ready to display it.
- }
- fill_solid(Lleds, NUM_LEDS, CRGB::Black);
- for (int L=0; L < numgravsL; L++) {
- mygravsL[L].velocityL = mygravsL[L].veloldL + gravityL*timeincL; // Split gravity math into two lines for simplicity
- mygravsL[L].distanceL = mygravsL[L].distoldL + mygravsL[L].velocityL*timeincL;
- int Li = map(mygravsL[L].distanceL, 0, 32767, 0, NUM_LEDS);
- mygravsL[L].veloldL = mygravsL[L].velocityL; // Capture the current velocity/distance
- mygravsL[L].distoldL = mygravsL[L].distanceL;
- if (Li <= 1 && abs(mygravsL[L].veloldL) < 700 ) {mygravsL[L].veloldL = 0; mygravsL[L].distoldL=random16(0,8000)+26000; mygravsL[L].thishueL=random8(255);} // Reset!!!
- if (Li <= 1 && mygravsL[L].veloldL<0) {mygravsL[L].veloldL = -mygravsL[L].veloldL;} // Bounce!!!
- Lleds[Li] += CHSV(mygravsL[L].thishueL, thissatL, thisbrightL); // Let's get ready to display it.
- }
- }
- // gravball()
Advertisement
Add Comment
Please, Sign In to add comment