Advertisement
Guest User

Untitled

a guest
Sep 27th, 2016
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.79 KB | None | 0 0
  1. //////////////////////
  2. // FastLed constants//
  3. //////////////////////
  4. #include <FastLED.h>
  5. FASTLED_USING_NAMESPACE
  6. #define CHIPSET WS2812B
  7. #define COLOR_ORDER GRB
  8.  
  9. #define DATA_PIN 11
  10. #define NUM_LEDS 165
  11. #define BRIGHTNESS 96
  12. int ledHeight = 0;
  13. uint8_t gHue = 0;
  14. CRGB leds[NUM_LEDS];
  15. #define FRAMES_PER_SECOND 120
  16.  
  17.  
  18. ///////////////////
  19. // bike constants//
  20. ///////////////////
  21.  
  22. int revolutions = 0;
  23. int mappedRevolutions = 0;
  24. unsigned long int lastmillis = 0;
  25. long lastRevolutions = 0;
  26. static unsigned long last_interrupt_time = 0;
  27.  
  28. float rpm = 0;
  29.  
  30. ////////////////////
  31. // game variables //
  32. ////////////////////
  33. int idleSeconds = 0;
  34. unsigned long int celebration_time = 0;
  35. bool just_won = false;
  36.  
  37.  
  38. ////////////
  39. // setup //
  40. ////////////
  41.  
  42. void setup() {
  43. delay(2000);
  44. FastLED.addLeds<WS2812B, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS);
  45. FastLED.setBrightness( BRIGHTNESS );
  46. pinMode(2, INPUT);
  47. attachInterrupt(0, countRotation, RISING);
  48. Serial.begin(9600);
  49. }
  50.  
  51. /////////////
  52. // helpers //
  53. /////////////
  54.  
  55. // List of patterns to cycle through. Each is defined as a separate function below.
  56. typedef void (*SimplePatternList[])();
  57. SimplePatternList gPatterns = { rainbow, rainbowWithGlitter, confetti, juggle, bpm };
  58. uint8_t gCurrentPatternNumber = 0; // Index number of which pattern is current
  59. #define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0]))
  60.  
  61. /////////
  62. // main//
  63. /////////
  64.  
  65. void loop() {
  66. // insert a delay to keep the framerate modest
  67. FastLED.delay(1000 / FRAMES_PER_SECOND);
  68. revolutions = constrain(revolutions, 0, NUM_LEDS);
  69.  
  70. EVERY_N_MILLISECONDS( 20 ) {
  71. gHue++; // slowly cycle the "base color" through the rainbow
  72. }
  73. EVERY_N_SECONDS(1) {
  74. idleSeconds ++; // counts the number of idle seconds
  75.  
  76. }
  77.  
  78. if (revolutions < 1 && idleSeconds > 10) {
  79. // IDLE
  80. sinelon();
  81. } else if (revolutions < NUM_LEDS) {
  82. // IN GAME
  83. countdown();
  84. fill(revolutions);
  85. } else {
  86. // YOU WIN
  87.  
  88. if (!just_won) {
  89. // YOU JUST WON
  90. detachInterrupt(0);
  91. just_won = true;
  92. celebration_time = millis();
  93. nextPattern();
  94. }
  95.  
  96. if (millis() - celebration_time < 10000) {
  97.  
  98. gPatterns[gCurrentPatternNumber]();
  99. } else {
  100. FastLED.clear();
  101. revolutions = 0;
  102. just_won = false;
  103. idleSeconds = 0; // prevent idle animation
  104. attachInterrupt(0, countRotation, RISING);
  105. }
  106. }
  107. }
  108.  
  109. /**
  110. xxx too much instruction: refactor me
  111. */
  112. void countRotation() {
  113. unsigned long interrupt_time = millis();
  114. // If interrupts come faster than 200ms, assume it's a bounce and ignore
  115. if (interrupt_time - last_interrupt_time > 200) {
  116. revolutions += 3;
  117. }
  118. last_interrupt_time = interrupt_time;
  119. idleSeconds = 0; // if interrupt was detected, reset idle count
  120. }
  121.  
  122. /**
  123. sinelon;rainbow; rainbowWithGlitter confetti and juggle
  124. shamelessly stolen from FASTLed Library examples (thank to them)
  125. */
  126. void rainbow()
  127. {
  128. // FastLED's built-in rainbow generator
  129. fill_rainbow( leds, NUM_LEDS, gHue, 7);
  130. }
  131.  
  132. void rainbowWithGlitter()
  133. {
  134. // built-in FastLED rainbow, plus some random sparkly glitter
  135. rainbow();
  136. addGlitter(80);
  137. }
  138.  
  139. void addGlitter( fract8 chanceOfGlitter)
  140. {
  141. if ( random8() < chanceOfGlitter) {
  142. leds[ random16(NUM_LEDS) ] += CRGB::White;
  143. }
  144. }
  145.  
  146. void sinelon()
  147. {
  148. // a colored dot sweeping back and forth, with fading trails
  149. fadeToBlackBy( leds, NUM_LEDS, 20);
  150. int pos = beatsin16(12, 0, NUM_LEDS);
  151. leds[pos] += CHSV( gHue, 255, 192);
  152. }
  153.  
  154. void confetti()
  155. {
  156. // random colored speckles that blink in and fade smoothly
  157. fadeToBlackBy( leds, NUM_LEDS, 20);
  158. int pos = random16(NUM_LEDS);
  159. leds[pos] += CHSV( gHue + random8(64), 200, 255);
  160. }
  161.  
  162. void juggle() {
  163. // eight colored dots, weaving in and out of sync with each other
  164. fadeToBlackBy( leds, NUM_LEDS, 20);
  165. byte dothue = 0;
  166. for ( int i = 0; i < 8; i++) {
  167. leds[beatsin16(i + 7, 0, NUM_LEDS)] |= CHSV(dothue, 200, 255);
  168. dothue += 32;
  169. }
  170. }
  171.  
  172. void bpm()
  173. {
  174. // colored stripes pulsing at a defined Beats-Per-Minute (BPM)
  175. uint8_t BeatsPerMinute = 62;
  176. CRGBPalette16 palette = PartyColors_p;
  177. uint8_t beat = beatsin8( BeatsPerMinute, 64, 255);
  178. for ( int i = 0; i < NUM_LEDS; i++) { //9948
  179. leds[i] = ColorFromPalette(palette, gHue + (i * 2), beat - gHue + (i * 10));
  180. }
  181. }
  182.  
  183. void nextPattern()
  184. {
  185. // add one to the current pattern number, and wrap around at the end
  186. gCurrentPatternNumber = (gCurrentPatternNumber + 1) % ARRAY_SIZE( gPatterns);
  187. }
  188.  
  189.  
  190. void fill(int ledHeight) {
  191. FastLED.clear();
  192.  
  193. for (int i = 0; i < ledHeight; i++) {
  194. int color_mapped = map(i, 0, NUM_LEDS, 0, 255);
  195. int blue = 255 - color_mapped;
  196. leds[i] = CRGB(color_mapped, 0, blue);
  197. }
  198.  
  199. //fill_rainbow(leds, ledHeight, gHue, 7);
  200. //fill_gradient_RGB(leds, ledHeight, CRGB::Blue, CRGB::Red);
  201. FastLED.show();
  202. }
  203.  
  204. void countdown() {
  205. if (millis() - lastmillis >= 1500 && revolutions > 0) {
  206. revolutions --;
  207. lastmillis = millis();
  208. }
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement