Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //***********************
- //### Motion Sensor Stuff
- //***********************
- int motionSensor = 2;
- int state = LOW;
- int val = 0;
- //***********************
- //### WS2912B Stuff #####
- //***********************
- #include "FastLED.h"
- #define NUM_LEDS 150
- CRGB leds[NUM_LEDS];
- #define PIN 6
- #define FRAMES_PER_SECOND 120
- #define BRIGHTNESS 255
- long lastActivation = 0;
- bool showingRainbow = false;
- CRGBPalette16 currentPalette;
- TBlendType currentBlending;
- unsigned int ledLoc = 0;
- uint8_t gHue = 0;
- // Functions
- void rainbow()
- {
- // FastLED's built-in rainbow generator
- fill_rainbow( leds, NUM_LEDS, gHue, 8);
- EVERY_N_MILLISECONDS( 20 ) { gHue++; }
- FastLED.show();
- }
- void curtainEffect() {
- // curtain effect
- for(int k=0, m=NUM_LEDS-1; k<=NUM_LEDS/2 && m>=NUM_LEDS/2; k++, m--) {
- leds[k] = CRGB::Red;
- leds[m] = CRGB::Red;
- FastLED.show();
- delay(30);
- } // the 75th LED will be Blue
- for(int i=NUM_LEDS/2, j=NUM_LEDS/2; i>=0, j<=NUM_LEDS; i--, j++) {
- leds[i] = CRGB::Black;
- leds[j] = CRGB::Black;
- FastLED.show();
- delay(30); // starting from LED #75, they will turn off oneoyone until the start
- }
- //FastLED.show();
- FastLED.delay(1000/FRAMES_PER_SECOND);
- EVERY_N_MILLISECONDS( 20 ) { gHue++; }
- }
- void PhiladdEDGlitter( fract8 chanceOfGlitter)
- {
- fill_rainbow( leds, NUM_LEDS, gHue, 8);
- EVERY_N_MILLISECONDS( 20 ) { gHue++; }
- fadeToBlackBy( leds, NUM_LEDS, 10);
- if( random8() < chanceOfGlitter) {
- leds[ random16(NUM_LEDS) ] += CRGB::White;
- }
- FastLED.show();
- }
- void juggledark() {
- // eight colored dots, weaving in and out of sync with each other
- fadeToBlackBy( leds, NUM_LEDS, 20);
- byte dothue = 0;
- for( int i = 0; i < 8; i++) {
- leds[beatsin16( i+7, 0, NUM_LEDS-1 )] |= CHSV(dothue, 0, 0);
- dothue += 32;
- }
- FastLED.show();
- FastLED.delay(1000/FRAMES_PER_SECOND);
- EVERY_N_MILLISECONDS( 20 ) { gHue++; }
- }
- void juggle() {
- // eight colored dots, weaving in and out of sync with each other
- fadeToBlackBy( leds, NUM_LEDS, 20);
- byte dothue = 0;
- for( int i = 0; i < 8; i++) {
- leds[beatsin16( i+7, 0, NUM_LEDS-1 )] |= CHSV(dothue, 200, 255);
- dothue += 32;
- }
- FastLED.show();
- FastLED.delay(1000/FRAMES_PER_SECOND);
- EVERY_N_MILLISECONDS( 20 ) { gHue++; }
- }
- // Alternative Rainbow effect
- void rainbowAlternative(int SpeedDelay) {
- byte *c;
- uint16_t i, j;
- for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel
- for(i=0; i< NUM_LEDS; i++) {
- c=Wheel(((i * 256 / NUM_LEDS) + j) & 255);
- setPixel(i, *c, *(c+1), *(c+2));
- }
- FastLED.show();
- delay(SpeedDelay);
- }
- }
- void setPixel(int Pixel, byte red, byte green, byte blue) {
- leds[Pixel].r = red;
- leds[Pixel].g = green;
- leds[Pixel].b = blue;
- }
- byte * Wheel(byte WheelPos) {
- static byte c[3];
- if(WheelPos < 85) {
- c[0]=WheelPos * 3;
- c[1]=255 - WheelPos * 3;
- c[2]=0;
- } else if(WheelPos < 170) {
- WheelPos -= 85;
- c[0]=255 - WheelPos * 3;
- c[1]=0;
- c[2]=WheelPos * 3;
- } else {
- WheelPos -= 170;
- c[0]=0;
- c[1]=WheelPos * 3;
- c[2]=255 - WheelPos * 3;
- }
- return c;
- }
- // Start adding stuff from https://github.com/atuline/FastLED-Demos/blob/master/every_n_example/every_n_example.ino
- void everyNMillisI() {
- 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.
- uint8_t timeval = beatsin8(10,5,100); // Create/modify a variable based on the beastsin8() function.
- thisTimer.setPeriod(timeval); // Use that as our update timer value.
- ledLoc = (ledLoc+1) % (NUM_LEDS-1); // A simple routine to just move the active LED UP the strip.
- leds[ledLoc] = ColorFromPalette(currentPalette, ledLoc, 255, currentBlending); // Pick a slightly rotating colour from the Palette
- }
- fadeToBlackBy(leds, NUM_LEDS, 8); // Leave a nice comet trail behind.
- FastLED.show();
- }
- // Functions End
- void setup(){
- //***********************
- //### Motion Sensor Stuff
- //***********************
- pinMode(LED_BUILTIN, OUTPUT);
- pinMode(motionSensor, INPUT);
- //Serial.begin(9600);
- FastLED.setBrightness(BRIGHTNESS);
- //***********************
- //### WS2912B Stuff #####
- //***********************
- FastLED.addLeds<WS2812B, PIN, GRB>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
- currentBlending = LINEARBLEND;
- currentPalette = PartyColors_p;
- }
- void loop()
- {
- long timeSinceLastActivation = millis() - lastActivation;
- if (showingRainbow)
- {
- PhiladdEDGlitter(100);
- if (timeSinceLastActivation > 90000)
- {
- // stop animation
- curtainEffect();
- showingRainbow = false;
- }
- }
- if (digitalRead(motionSensor) == HIGH)
- {
- // start rainbow
- showingRainbow = true;
- lastActivation = millis();
- }
- }
- //***********************
- //### WS2912B Functions #
- //***********************
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement