Guest User

Dual gravball

a guest
Oct 25th, 2014
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.44 KB | None | 0 0
  1.  
  2. // Gravball
  3. //
  4. // By: Andrew Tuline
  5. //
  6. // Date: Oct, 2014
  7. //
  8. // Multiple bouncing balls. For whatever reason, I call them gravs.
  9. //
  10. // FastLED 2.1 is available at https://github.com/FastLED/FastLED/tree/FastLED2.1
  11. //
  12.  
  13.  
  14. #include "FastLED.h" // FastLED library. Preferably the latest copy of FastLED 2.1.
  15.  
  16. // Fixed definitions cannot change on the fly.
  17. #define COLOR_ORDER GRB // Are they RGB, GRB or what??
  18. #define LED_TYPE WS2812B // What kind of strip are you using?
  19. #define NUM_LEDS 18 // Number of LED's
  20.  
  21. // Initialize changeable global variables.
  22. uint8_t max_bright = 128; // Overall brightness definition. It can be changed on the fly.
  23.  
  24. struct CRGB Lleds[NUM_LEDS]; // Initialize our LED array.
  25. struct CRGB Rleds[NUM_LEDS];
  26.  
  27. // Initialize global variables for sequences
  28. int thisdelayR = 15; // A delay value for the sequence(s)
  29. int thissatR = 255;
  30. int thisbrightR = 255;
  31.  
  32. int thisdelayL = 15; // A delay value for the sequence(s)
  33. int thissatL = 255;
  34. int thisbrightL = 255;
  35.  
  36. // GRAVBALL VARIABLES
  37. int gravityR = -15;
  38. int dragR = 0; // Not required due to losses in 16 bit math.
  39. int timeincR = 2;
  40.  
  41. int gravityl = -15;
  42. int dragL = 0; // Not required due to losses in 16 bit math.
  43. int timeincL = 2;
  44.  
  45.  
  46. #define numgravsR 3 // How many gravs we are using.
  47. #define numgravsL 3
  48.  
  49. typedef struct { // Define a structure for the gravs.
  50. int distoldR, distoldL;
  51. int distanceR, distanceL;
  52. int veloldR, veloldL;
  53. int velocityR, velocityL;
  54. int thishueR, thishueL;
  55. gravsR, gravsL;
  56. )
  57.  
  58. gravsR mygravsR[numgravsR];
  59. gravsL mygravsl[numgravsL];
  60.  
  61.  
  62.  
  63. void setup() {
  64. Serial.begin(57600);
  65. LEDS.addLeds<LED_TYPE, 13, COLOR_ORDER>(Rleds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  66. LEDS.addLeds<LED_TYPE, 11, COLOR_ORDER>(Lleds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  67. FastLED.setBrightness(max_bright);
  68. set_max_power_in_volts_and_milliamps(5, 500); // FastLED 2.1 Power management set at 5V, 500mA
  69. } // setup()
  70.  
  71.  
  72.  
  73. void loop () {
  74. gravball();
  75. show_at_max_brightness_for_power();
  76. delay_at_max_brightness_for_power(thisdelayR*2.5);
  77. LEDS.countFPS();
  78. } // loop()
  79.  
  80.  
  81.  
  82. void gravball() {
  83. fill_solid(Rleds, NUM_LEDS, CRGB::Black);
  84. for (int R=0; R < numgravsR; R++) {
  85.  
  86. mygravsR[R].velocityR = mygravsR[R].veloldR + gravityR*timeincR; // Split gravity math into two lines for simplicity
  87. mygravsR[R].distanceR = mygravsR[R].distoldR + mygravsR[R].velocityR*timeincR;
  88.  
  89. int Ri = map(mygravsR[R].distanceR, 0, 32767, 0, NUM_LEDS);
  90.  
  91. mygravsR[R].veloldR = mygravsR[R].velocityR; // Capture the current velocity/distance
  92. mygravsR[R].distoldR = mygravsR[R].distanceR;
  93.  
  94. 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!!!
  95. if (Ri <= 1 && mygravsR[R].veloldR<0) {mygravsR[R].veloldR = -mygravsR[R].veloldR;} // Bounce!!!
  96. Rleds[Ri] += CHSV(mygravsR[R].thishueR, thissatR, thisbrightR); // Let's get ready to display it.
  97. }
  98.  
  99. fill_solid(Lleds, NUM_LEDS, CRGB::Black);
  100. for (int L=0; L < numgravsL; L++) {
  101.  
  102. mygravsL[L].velocityL = mygravsL[L].veloldL + gravityL*timeincL; // Split gravity math into two lines for simplicity
  103. mygravsL[L].distanceL = mygravsL[L].distoldL + mygravsL[L].velocityL*timeincL;
  104.  
  105. int Li = map(mygravsL[L].distanceL, 0, 32767, 0, NUM_LEDS);
  106.  
  107. mygravsL[L].veloldL = mygravsL[L].velocityL; // Capture the current velocity/distance
  108. mygravsL[L].distoldL = mygravsL[L].distanceL;
  109.  
  110. 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!!!
  111. if (Li <= 1 && mygravsL[L].veloldL<0) {mygravsL[L].veloldL = -mygravsL[L].veloldL;} // Bounce!!!
  112. Lleds[Li] += CHSV(mygravsL[L].thishueL, thissatL, thisbrightL); // Let's get ready to display it.
  113. }
  114.  
  115. }
  116.  
  117. // gravball()
Advertisement
Add Comment
Please, Sign In to add comment