Advertisement
Guest User

Untitled

a guest
Feb 10th, 2020
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.30 KB | None | 0 0
  1. //***********************
  2. //### Motion Sensor Stuff
  3. //***********************
  4. int motionSensor = 2;
  5. int state = LOW;
  6. int val = 0;
  7.  
  8. //***********************
  9. //### WS2912B Stuff #####
  10. //***********************
  11. #include "FastLED.h"
  12. #define NUM_LEDS 150
  13. CRGB leds[NUM_LEDS];
  14. #define PIN 6
  15. #define FRAMES_PER_SECOND  120
  16. #define BRIGHTNESS         255
  17. long lastActivation = 0;
  18. bool showingRainbow = false;
  19.  
  20.  
  21. CRGBPalette16 currentPalette;
  22. TBlendType    currentBlending;
  23. unsigned int  ledLoc = 0;
  24. uint8_t gHue = 0;
  25.  
  26. // Functions
  27.  
  28.  
  29.  
  30.   void rainbow()
  31.   {
  32.     // FastLED's built-in rainbow generator
  33.     fill_rainbow( leds, NUM_LEDS, gHue, 8);
  34.     EVERY_N_MILLISECONDS( 20 ) { gHue++; }
  35.     FastLED.show();
  36.   }
  37.  
  38.  
  39.  
  40.   void curtainEffect() {
  41.   // curtain effect
  42.  
  43.   for(int k=0, m=NUM_LEDS-1; k<=NUM_LEDS/2 && m>=NUM_LEDS/2; k++, m--) {
  44.              leds[k] = CRGB::Red;
  45.              leds[m] = CRGB::Red;
  46.              FastLED.show();
  47.              delay(30);
  48.          }                               // the 75th LED will be Blue
  49.  
  50.   for(int i=NUM_LEDS/2, j=NUM_LEDS/2; i>=0, j<=NUM_LEDS; i--, j++) {
  51.              leds[i] = CRGB::Black;
  52.              leds[j] = CRGB::Black;
  53.              FastLED.show();
  54.              delay(30);               // starting from LED #75, they will turn off oneoyone until the start
  55.          }
  56.          //FastLED.show();
  57.          FastLED.delay(1000/FRAMES_PER_SECOND);
  58.          EVERY_N_MILLISECONDS( 20 ) { gHue++; }
  59.  }
  60.  
  61.  void PhiladdEDGlitter( fract8 chanceOfGlitter)
  62.   {
  63.     fill_rainbow( leds, NUM_LEDS, gHue, 8);
  64.     EVERY_N_MILLISECONDS( 20 ) { gHue++; }
  65.     fadeToBlackBy( leds, NUM_LEDS, 10);
  66.     if( random8() < chanceOfGlitter) {
  67.     leds[ random16(NUM_LEDS) ] += CRGB::White;
  68.     }
  69. FastLED.show();
  70.   }
  71.  
  72.  void juggledark() {
  73.    // eight colored dots, weaving in and out of sync with each other
  74.    fadeToBlackBy( leds, NUM_LEDS, 20);
  75.    byte dothue = 0;
  76.    for( int i = 0; i < 8; i++) {
  77.      leds[beatsin16( i+7, 0, NUM_LEDS-1 )] |= CHSV(dothue, 0, 0);
  78.      dothue += 32;
  79.    }
  80.    FastLED.show();
  81.    FastLED.delay(1000/FRAMES_PER_SECOND);
  82.    EVERY_N_MILLISECONDS( 20 ) { gHue++; }
  83.  }
  84.  
  85.  void juggle() {
  86.    // eight colored dots, weaving in and out of sync with each other
  87.    fadeToBlackBy( leds, NUM_LEDS, 20);
  88.    byte dothue = 0;
  89.    for( int i = 0; i < 8; i++) {
  90.      leds[beatsin16( i+7, 0, NUM_LEDS-1 )] |= CHSV(dothue, 200, 255);
  91.      dothue += 32;
  92.    }
  93.    FastLED.show();
  94.    FastLED.delay(1000/FRAMES_PER_SECOND);
  95.    EVERY_N_MILLISECONDS( 20 ) { gHue++; }
  96.  }
  97.  
  98.  
  99. // Alternative Rainbow effect
  100. void rainbowAlternative(int SpeedDelay) {
  101.   byte *c;
  102.   uint16_t i, j;
  103.  
  104.   for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel
  105.     for(i=0; i< NUM_LEDS; i++) {
  106.       c=Wheel(((i * 256 / NUM_LEDS) + j) & 255);
  107.       setPixel(i, *c, *(c+1), *(c+2));
  108.     }
  109.     FastLED.show();
  110.     delay(SpeedDelay);
  111.   }
  112. }
  113.  
  114. void setPixel(int Pixel, byte red, byte green, byte blue) {
  115.    leds[Pixel].r = red;
  116.    leds[Pixel].g = green;
  117.    leds[Pixel].b = blue;
  118. }
  119.  
  120. byte * Wheel(byte WheelPos) {
  121.   static byte c[3];
  122.  
  123.   if(WheelPos < 85) {
  124.    c[0]=WheelPos * 3;
  125.    c[1]=255 - WheelPos * 3;
  126.    c[2]=0;
  127.   } else if(WheelPos < 170) {
  128.    WheelPos -= 85;
  129.    c[0]=255 - WheelPos * 3;
  130.    c[1]=0;
  131.    c[2]=WheelPos * 3;
  132.   } else {
  133.    WheelPos -= 170;
  134.    c[0]=0;
  135.    c[1]=WheelPos * 3;
  136.    c[2]=255 - WheelPos * 3;
  137.   }
  138.  
  139.   return c;
  140. }
  141.  
  142. // Start adding stuff from https://github.com/atuline/FastLED-Demos/blob/master/every_n_example/every_n_example.ino
  143. void everyNMillisI() {
  144.   EVERY_N_MILLIS_I(thisTimer,100) {                           // This only sets the Initial timer delay. To change this value, you need to use thisTimer.setPeriod(); You could also call it thatTimer and so on.
  145.     uint8_t timeval = beatsin8(10,5,100);                     // Create/modify a variable based on the beastsin8() function.
  146.     thisTimer.setPeriod(timeval);                             // Use that as our update timer value.
  147.     ledLoc = (ledLoc+1) % (NUM_LEDS-1);                       // A simple routine to just move the active LED UP the strip.
  148.     leds[ledLoc] = ColorFromPalette(currentPalette, ledLoc, 255, currentBlending);     // Pick a slightly rotating colour from the Palette
  149.   }
  150.   fadeToBlackBy(leds, NUM_LEDS, 8);                           // Leave a nice comet trail behind.
  151.  
  152.   FastLED.show();
  153. }
  154.  
  155. // Functions  End
  156.  
  157.  
  158.  
  159. void setup(){
  160. //***********************
  161. //### Motion Sensor Stuff
  162. //***********************
  163.   pinMode(LED_BUILTIN, OUTPUT);
  164.   pinMode(motionSensor, INPUT);
  165.   //Serial.begin(9600);
  166.   FastLED.setBrightness(BRIGHTNESS);
  167. //***********************
  168. //### WS2912B Stuff #####
  169. //***********************
  170. FastLED.addLeds<WS2812B, PIN, GRB>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  171.  
  172. currentBlending = LINEARBLEND;  
  173. currentPalette = PartyColors_p;
  174.  
  175. }
  176.  
  177.  
  178.  
  179. void loop()
  180. {
  181.   long timeSinceLastActivation = millis() - lastActivation;
  182.  
  183.   if (showingRainbow)
  184.   {
  185.     PhiladdEDGlitter(100);
  186.     if (timeSinceLastActivation > 90000)
  187.     {
  188.       // stop animation
  189.       curtainEffect();
  190.       showingRainbow = false;
  191.     }
  192.   }
  193.   if (digitalRead(motionSensor) == HIGH)
  194.   {
  195.     // start rainbow
  196.     showingRainbow = true;
  197.     lastActivation = millis();
  198.   }
  199. }
  200.  
  201.  
  202. //***********************
  203. //### WS2912B Functions #
  204. //***********************
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement