Advertisement
TopHatRaver

8 Way Rotary Switch

Mar 27th, 2018
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.65 KB | None | 0 0
  1. #include <FastLED.h>
  2.  
  3. #define NUM_LEDS 30
  4. #define DATA_PIN 6
  5. #define LED_TYPE WS2812B
  6. #define COLOR_ORDER GRB
  7. #define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0]))
  8. #define FRAMES_PER_SECOND 120
  9.  
  10. CRGB leds[NUM_LEDS];
  11.  
  12. uint8_t ButtonPins[] = {0,2};
  13. uint8_t pinCount = 2;
  14. uint8_t *myPointer;
  15.  
  16.  
  17. void setup() {
  18.  
  19.   FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  20.   FastLED.setBrightness(50);
  21.  
  22.   for(uint8_t x = 0; x<pinCount; x++)
  23.     {
  24.       pinMode(ButtonPins[x], INPUT_PULLUP);
  25.     }
  26.  
  27.   Serial.begin(9600);  
  28.  
  29. }
  30.  
  31. typedef void (*SimplePatternList[])();
  32. SimplePatternList gPatterns = { rainbow, confetti, sinelon, juggle, bpm };
  33.  
  34. uint8_t gCurrentPatternNumber = 0; // Index number of which pattern is current
  35. uint8_t gHue = 0; // rotating "base color" used by many of the patterns
  36.  
  37. void loop() {
  38.  
  39.   for(uint8_t x =0; x<pinCount; x++)
  40.   {
  41.     ButtonPins[x] = digitalRead(x);
  42.   }
  43.  
  44.   for(uint8_t pin = 0; pin < pinCount; pin++)
  45.   {
  46.     myPointer = &ButtonPins[pin];
  47.     if (*myPointer == 0)
  48.       {
  49.         gPatterns[pin]();
  50.       }
  51.   }
  52.   FastLED.show();
  53.   Serial.print(*myPointer);  
  54.   Serial.println();
  55.   FastLED.delay(1000/FRAMES_PER_SECOND);
  56.   EVERY_N_MILLISECONDS( 20 ) { gHue++; }
  57. }
  58.  
  59.  
  60. void rainbow()
  61. {
  62.   // FastLED's built-in rainbow generator
  63.   fill_rainbow( leds, NUM_LEDS, gHue, 5);
  64. }
  65.  
  66. void rainbowWithGlitter()
  67. {
  68.   // built-in FastLED rainbow, plus some random sparkly glitter
  69.   rainbow();
  70.   addGlitter(80);
  71. }
  72.  
  73. void addGlitter( fract8 chanceOfGlitter)
  74. {
  75.   if( random8() < chanceOfGlitter) {
  76.     leds[ random16(NUM_LEDS) ] += CRGB::White;
  77.   }
  78. }
  79.  
  80. void confetti()
  81. {
  82.   // random colored speckles that blink in and fade smoothly
  83.   fadeToBlackBy( leds, NUM_LEDS, 10);
  84.   int pos = random16(NUM_LEDS);
  85.   leds[pos] += CHSV( gHue + random8(64), 200, 255);
  86. }
  87.  
  88. void sinelon()
  89. {
  90.   // a colored dot sweeping back and forth, with fading trails
  91.   fadeToBlackBy( leds, NUM_LEDS, 20);
  92.   int pos = beatsin16( 13, 0, NUM_LEDS-1 );
  93.   leds[pos] += CHSV( gHue, 255, 192);
  94. }
  95.  
  96. void bpm()
  97. {
  98.   // colored stripes pulsing at a defined Beats-Per-Minute (BPM)
  99.   uint8_t BeatsPerMinute = 62;
  100.   CRGBPalette16 palette = PartyColors_p;
  101.   uint8_t beat = beatsin8( BeatsPerMinute, 64, 255);
  102.   for( int i = 0; i < NUM_LEDS; i++) { //9948
  103.     leds[i] = ColorFromPalette(palette, gHue+(i*2), beat-gHue+(i*10));
  104.   }
  105. }
  106.  
  107. void juggle() {
  108.   // eight colored dots, weaving in and out of sync with each other
  109.   fadeToBlackBy( leds, NUM_LEDS, 20);
  110.   byte dothue = 0;
  111.   for( int i = 0; i < 8; i++) {
  112.     leds[beatsin16( i+7, 0, NUM_LEDS-1 )] |= CHSV(dothue, 200, 255);
  113.     dothue += 32;
  114.   }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement