ejspina

PIR array + LED strip array test sketch

Jun 12th, 2016
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.20 KB | None | 0 0
  1. #include "FastLED.h"
  2. #define LED_TYPE NEOPIXEL
  3. #define NUM_STRIPS 5
  4. #define NUM_LEDS 7  
  5. #define BRIGHTNESS  20
  6. #define FRAMES_PER_SECOND  120
  7. #define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0]))
  8.  
  9. int PIR1 = A0;               // Set the input pins for PIR sensors            
  10. int pirState = LOW;             // Start assuming no motion detected
  11. int val1 = 0;                    // Variable for reading the pin status                
  12.  
  13. CRGB leds1[NUM_LEDS];
  14. CRGB leds2[NUM_LEDS];
  15.  
  16. CRGB *strips[] = {leds1, leds2};  
  17. uint8_t chanceOfGlitter = 80;
  18. int stripIndex = 0;
  19.  
  20. //---------------------------------------------------------------
  21. void setup() {
  22.   Serial.begin(9600);  // Allows serial monitor output (check baud rate)
  23.   delay(3000);
  24.   FastLED.addLeds<LED_TYPE, 2>(leds1, NUM_LEDS);  // Assign data pins
  25.   FastLED.addLeds<LED_TYPE, 3>(leds2, NUM_LEDS);
  26.  
  27.   FastLED.setBrightness(  BRIGHTNESS );
  28.  
  29.   pinMode(PIR1, INPUT);     // declare sensor as input
  30. }
  31.  
  32. typedef void (*SimplePatternList[])();
  33. SimplePatternList gPatterns = { confetti, rainbowWithGlitter, rainbow, sinelon2, bpm, juggle };
  34.  
  35. uint8_t gCurrentPatternNumber = 0; // Index number of which pattern is current
  36. uint8_t gHue = 0; // rotating "base color" used by many of the patterns
  37.  
  38. //---------------------------------------------------------------
  39. void loop() {
  40.   val1 = digitalRead(PIR1);  // read input value
  41.   EVERY_N_SECONDS( random8(60) ) {val1 = HIGH;} // Randomly activate this set of lights
  42.   if (val1 == HIGH) {            // check if the input is HIGH
  43.     stripIndex = 0;
  44.     gPatterns[gCurrentPatternNumber]();
  45.     stripIndex = 1;
  46.     gPatterns[gCurrentPatternNumber]();
  47.     FastLED.show();  
  48.     }
  49.     else {
  50.       for( int i = 0; i < NUM_LEDS; i++) {
  51.         leds1[i] %= 64;
  52.         FastLED.show();
  53.         leds1[i].maximizeBrightness();  
  54.       }
  55.   }
  56.    
  57.   // insert a delay to keep the framerate modest
  58.   FastLED.delay(1000/FRAMES_PER_SECOND);
  59.  
  60.   // do some periodic updates
  61.   EVERY_N_MILLISECONDS( 20 ) { gHue++; } // slowly cycle the "base color" through the rainbow
  62.   EVERY_N_SECONDS( 10 ) { nextPattern(); } // change patterns periodically  
  63.   //Periodically activate all lights
  64.   EVERY_N_SECONDS( random8(60) ) {val1 = HIGH;}
  65.   Serial.println(val1);
  66. }//end main loop
  67.  
  68. void nextPattern()
  69. {
  70.   // add one to the current pattern number, and wrap around at the end
  71.   gCurrentPatternNumber = (gCurrentPatternNumber + 1) % ARRAY_SIZE( gPatterns);
  72. }
  73.  
  74. void rainbow()
  75. {
  76.   // FastLED's built-in rainbow generator
  77.   fill_rainbow( strips[stripIndex], NUM_LEDS, gHue, 7);
  78. }
  79.  
  80. void rainbowWithGlitter()
  81. {
  82.   // built-in FastLED rainbow, plus some random sparkly glitter
  83.   rainbow();
  84.   addGlitter(80);
  85. }
  86.  
  87. void addGlitter( fract8 chanceOfGlitter)
  88. {
  89.   if( random8() < chanceOfGlitter) {
  90.     strips[stripIndex][ random16(NUM_LEDS) ] += CRGB::White;
  91.   }
  92. }
  93.  
  94. void confetti()
  95. {
  96.   // random colored speckles that blink in and fade smoothly
  97.   fadeToBlackBy( strips[stripIndex], NUM_LEDS, 10);
  98.   int pos = random16(NUM_LEDS);
  99.   strips[stripIndex][pos] += CHSV( gHue + random8(64), 200, 255);
  100. }
  101.  
  102. void bpm()
  103. {
  104.   // colored stripes pulsing at a defined Beats-Per-Minute (BPM)
  105.   uint8_t BeatsPerMinute = 62;
  106.   CRGBPalette16 palette = PartyColors_p;
  107.   uint8_t beat = beatsin8( BeatsPerMinute, 64, 255);
  108.   for( int i = 0; i < NUM_LEDS; i++) { //9948
  109.     strips[stripIndex][i] = ColorFromPalette(palette, gHue+(i*2), beat-gHue+(i*10));
  110.   }
  111. }
  112.  
  113. // Updated sinelon (no visual gaps)
  114. void sinelon2()
  115. {
  116.   // a colored dot sweeping
  117.   // back and forth, with
  118.   // fading trails
  119.   fadeToBlackBy( strips[stripIndex], NUM_LEDS, 20);
  120.   int pos = beatsin16(13,0,NUM_LEDS);
  121.   static int prevpos = 0;
  122.   if( pos < prevpos ) {
  123.     fill_solid( strips[stripIndex]+pos, (prevpos-pos)+1, CHSV(gHue,220,255));
  124.   } else {
  125.     fill_solid( strips[stripIndex]+prevpos, (pos-prevpos)+1, CHSV( gHue,220,255));
  126.   }
  127.   prevpos = pos;
  128. }
  129.  
  130. void juggle() {
  131.   // eight colored dots, weaving in and out of sync with each other
  132.   uint8_t x = stripIndex;
  133.   fadeToBlackBy( strips[x], NUM_LEDS, 20);
  134.   byte dothue = 0;
  135.   for( int i = 0; i < 8; i++) {
  136.     strips[x][beatsin16(i+7,0,NUM_LEDS)] |= CHSV(dothue, 200, 255);
  137.     dothue += 32;
  138.   }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment