Advertisement
atuline

Gravities

Jul 27th, 2014
951
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.45 KB | None | 0 0
  1. /*
  2. #
  3. # Gravities
  4. #
  5. # By: Andrew Tuline
  6. # www.tuline.com
  7. #
  8. # Date: July, 2014
  9. #
  10. */
  11.  
  12. /*
  13. # LICENSE - Open Source.
  14. */
  15.  
  16. /*
  17. # Introduction:
  18. #
  19. # This routine demonstrates bouncing balls using the Arduino UNO, a strand of WS2812B LED's and the FastLED library.
  20. #
  21. */
  22.  
  23. #include <FastLED.h>
  24.  
  25. #define LED_PIN     13
  26. #define NUM_LEDS    24
  27. #define BRIGHTNESS  255
  28. #define LED_TYPE    WS2811
  29. #define COLOR_ORDER GRB
  30. CRGB leds[NUM_LEDS];
  31.  
  32. int thisdelay = 20;
  33. int thissat = 255;
  34. int thisbright = BRIGHTNESS;
  35.  
  36. int gravity = -15;
  37. int drag = 0;                   // Not required due to losses in 16 bit math.
  38. int timeinc = 2;
  39.  
  40. #define numballs 6              // How many balls we are using.
  41.  
  42. typedef struct {                // Define a structure for the balls.
  43.       int distanceold;
  44.       int distance;
  45.       int velocityold;
  46.       int velocity;
  47.       int thishue;
  48.   }  balls;
  49.  
  50. balls myballs[numballs];          // Array is called myballs, i.e. myballs[0].thishue = 80;
  51.  
  52. void setup() {
  53.   delay(2000);                    // Power-up safety delay or something like that.
  54.   Serial.begin(9600);
  55.  
  56.   FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  57.   FastLED.setBrightness( BRIGHTNESS );
  58. }
  59.  
  60.  
  61. void loop() {
  62.   gravball();
  63. }
  64.  
  65.  
  66. void gravball() {
  67.   one_color_all(0,0,0);
  68.   for (int k=0; k < numballs; k++) {
  69.  
  70.     myballs[k].velocity = myballs[k].velocityold + gravity*timeinc;               // Split gravity math into two lines for simplicity
  71.     myballs[k].distance = myballs[k].distanceold + myballs[k].velocity*timeinc;
  72.  
  73.     int i = map(myballs[k].distance, 0, 32767, 0, NUM_LEDS);
  74.    
  75.     myballs[k].velocityold = myballs[k].velocity;                                 // Capture the current velocity/distance
  76.     myballs[k].distanceold = myballs[k].distance;
  77.  
  78.     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!!!
  79.     if (i <= 1 && myballs[k].velocityold<0) {myballs[k].velocityold = -myballs[k].velocityold;}         // Bounce!!!
  80.     leds[i] = CHSV(myballs[k].thishue, thissat, thisbright);          // Let's get ready to display it.
  81.   }
  82.   FastLED.show();
  83.   delay(thisdelay);
  84. }
  85.  
  86. void one_color_all(int cred, int cgrn, int cblu) {      // SET ALL LEDS TO ONE COLOR
  87.   for (int i = 0 ; i < NUM_LEDS; i++ ) {
  88.     leds[i].setRGB( cred, cgrn, cblu);
  89.   }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement