Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- #
- # Gravities
- #
- # By: Andrew Tuline
- # www.tuline.com
- #
- # Date: July, 2014
- #
- */
- /*
- # LICENSE - Open Source.
- */
- /*
- # Introduction:
- #
- # This routine demonstrates bouncing balls using the Arduino UNO, a strand of WS2812B LED's and the FastLED library.
- #
- */
- #include <FastLED.h>
- #define LED_PIN 13
- #define NUM_LEDS 24
- #define BRIGHTNESS 255
- #define LED_TYPE WS2811
- #define COLOR_ORDER GRB
- CRGB leds[NUM_LEDS];
- int thisdelay = 20;
- int thissat = 255;
- int thisbright = BRIGHTNESS;
- int gravity = -15;
- int drag = 0; // Not required due to losses in 16 bit math.
- int timeinc = 2;
- #define numballs 6 // How many balls we are using.
- typedef struct { // Define a structure for the balls.
- int distanceold;
- int distance;
- int velocityold;
- int velocity;
- int thishue;
- } balls;
- balls myballs[numballs]; // Array is called myballs, i.e. myballs[0].thishue = 80;
- void setup() {
- delay(2000); // Power-up safety delay or something like that.
- Serial.begin(9600);
- FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
- FastLED.setBrightness( BRIGHTNESS );
- }
- void loop() {
- gravball();
- }
- void gravball() {
- one_color_all(0,0,0);
- for (int k=0; k < numballs; k++) {
- myballs[k].velocity = myballs[k].velocityold + gravity*timeinc; // Split gravity math into two lines for simplicity
- myballs[k].distance = myballs[k].distanceold + myballs[k].velocity*timeinc;
- int i = map(myballs[k].distance, 0, 32767, 0, NUM_LEDS);
- myballs[k].velocityold = myballs[k].velocity; // Capture the current velocity/distance
- myballs[k].distanceold = myballs[k].distance;
- if (i <= 1 && abs(myballs[k].velocityold) < 700 ) {myballs[k].velocityold = 0; myballs[k].distanceold=random(0,8000)+26000; myballs[k].thishue=random(0,255);} // Reset!!!
- if (i <= 1 && myballs[k].velocityold<0) {myballs[k].velocityold = -myballs[k].velocityold;} // Bounce!!!
- leds[i] = CHSV(myballs[k].thishue, thissat, thisbright); // Let's get ready to display it.
- }
- FastLED.show();
- delay(thisdelay);
- }
- void one_color_all(int cred, int cgrn, int cblu) { // SET ALL LEDS TO ONE COLOR
- for (int i = 0 ; i < NUM_LEDS; i++ ) {
- leds[i].setRGB( cred, cgrn, cblu);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement