Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "FastLED.h"
- // Good Reference!
- // http://fastled.io/docs/3.1/group___colorutils.html#gafcc7dac88e25736ebc49a9faf2a1c2e2
- #define DATA_PIN 10
- #define BRIGHTNESS_PIN A1
- #define COLOR_ORDER RGB
- #define LED_TYPE WS2812
- #define NUM_LEDS 100
- #define FRAMES_PER_SECOND 120
- // brightness control added 3/6/2016
- // original limits:
- //#define MASTER_BRIGHTNESS 128 // Set the master brigtness value [should be greater then min_brightness value].
- //#define MIN_BRIGHTNESS 30 // Set a minimum brightness level.
- #define MASTER_BRIGHTNESS 192 // Set the master brigtness value [should be greater then min_brightness value].
- #define MIN_BRIGHTNESS 10 // Set a minimum brightness level.
- int potValA; // Variable to store potentiometer A value (for brightness A)
- uint8_t brightnessa; // Mapped master brightness for color A based on potentiometer reading.
- #define NUM_VIRTUAL_LEDS NUM_LEDS+1
- #define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0]))
- CRGB leds[NUM_VIRTUAL_LEDS];
- // Added November 2, 2016
- // star pattern
- const uint8_t starCount = 70;
- static const uint8_t starArray[] PROGMEM =
- { 0, 1, 2, 3, 4, 5 , 6,
- 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66,
- 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
- 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86,
- 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46,
- 93, 94, 95, 96, 97, 98, 99 };
- const uint8_t innerCount = 30;
- static const uint8_t innerArray[] PROGMEM =
- { 7, 8, 9, 10, 11, 12,
- 67, 68, 69, 70, 71, 72,
- 27, 28, 29, 30, 31, 32,
- 87, 88, 89, 90, 91, 92,
- 47, 48, 49, 50, 51, 52
- };
- uint8_t offset_for_star_rotation = 0;
- uint16_t holdTime = 60; // Milliseconds to hold position before advancing.
- uint8_t spacing = 20; // Sets pixel spacing. [Use 2 or greater]
- int8_t delta = 1; // Sets forward or backwards direction amount. (Can be negative.)
- uint8_t width = 3; // Can increase the number of pixels (width) of the chase. [1 or greater]
- uint8_t hue = 60; // Starting color (marque_v3)
- boolean fadingTail = 1; // Add fading tail? [1=true, 0=falue]
- //uint8_t fadeRate = 150; // How fast to fade out tail. [0-255]
- uint8_t fadeRate = 70; // How fast to fade out tail. [0-255]
- uint8_t hue2_shift = 60; // Hue shift for secondary color. Use 0 for no shift. [0-255]
- boolean DEBUG = 0; // Print some info to serial monitor. [1=true, 0=falue]
- int16_t pos; // Pixel position.
- int8_t advance; // Stores the advance amount.
- uint8_t color; // Stores a hue color.
- // =============================================================================
- // setup routine
- // =============================================================================
- // put your setup code here, to run once:
- void setup() {
- delay(3000); // 3 second delay for recovery
- Serial.begin(57600);
- // tell FastLED about the LED strip configuration
- FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
- }
- // List of patterns to cycle through. Each is defined as a separate function below.
- typedef void (*SimplePatternList[])();
- //SimplePatternList gPatterns = { rainbow, rainbowWithGlitter, confetti, sinelon, juggle, bpm };
- //SimplePatternList gPatterns = { fill_solid_color, trace_inner_star, rainbow, sinelon, trace_star, juggle, bpm };
- //SimplePatternList gPatterns = { fill_solid_color, trace_inner_star, trace_star, gradient_fill };
- //SimplePatternList gPatterns = { gradient_fill_2, sinelon, fill_solid_color, trace_inner_star, juggle, trace_star, rainbow, bpm };
- // I love these 4 patterns -- this is a great base:
- // SimplePatternList gPatterns = { gradient_fill_2, fill_solid_color, trace_inner_star, trace_star };
- // These are 4 patterns are very energetic, they should be used intermittenly within the whole solution, maybe 1 per 2 above?
- // SimplePatternList gPatterns = { rainbow, sinelon, juggle, bpm };
- SimplePatternList gPatterns = { marque_v3, gradient_fill_2, fill_solid_color, trace_inner_star, trace_star };
- uint8_t gCurrentPatternNumber = 0; // Index number of which pattern is current
- uint8_t gHue = 0; // rotating "base color" used by many of the patterns
- // =============================================================================
- // loop routine
- // =============================================================================
- // put your main code here, to run repeatedly:
- void loop() {
- // brightness control
- // potValA = analogRead(BRIGHTNESS_PIN); // Read potentiometer B (for brightness).
- //Serial.println (potValA);
- // brightnessa = map(potValA, 0, 1023, MIN_BRIGHTNESS, MASTER_BRIGHTNESS);
- // Map value between min_brightness and MASTER brightness values.
- // Note: We are limiting the lowest possible brightness value to the
- // min_brightness value assigned up top.
- FastLED.setBrightness(MASTER_BRIGHTNESS);
- //Serial.println (brightnessa);
- // Call the current pattern function once, updating the 'leds' array
- gPatterns[gCurrentPatternNumber]();
- // send the 'leds' array out to the actual LED strip
- FastLED.show();
- // insert a delay to keep the framerate modest
- FastLED.delay(1000/FRAMES_PER_SECOND);
- // do some periodic updates
- EVERY_N_MILLISECONDS( 20 ) { gHue++; } // slowly cycle the "base color" through the rainbow
- EVERY_N_SECONDS( 10 ) { nextPattern(); } // change patterns periodically
- EVERY_N_SECONDS(10){ // Demo: Change the hue every N seconds.
- hue = hue + random8(30,61); // Shift the hue to something new.
- }
- }
- // =============================================================================
- // Sub-Routines
- // =============================================================================
- void nextPattern()
- {
- // add one to the current pattern number, and wrap around at the end
- gCurrentPatternNumber = (gCurrentPatternNumber + 1) % ARRAY_SIZE( gPatterns);
- }
- // ========================================
- // Routines for Pattern Display
- // ========================================
- // 11/6 modified for 4 pixel trace
- void trace_star()
- {
- uint8_t hue1 = random8(255);
- fill_black();
- for (int i=0; i<starCount; i++) {
- uint8_t offset = pgm_read_byte(&starArray[i]);
- leds[offset] = CHSV(hue1, 255, 255);
- uint8_t offset2;
- uint8_t offset3;
- uint8_t offset4;
- if ( (i+1)<starCount ) {
- offset2 = pgm_read_byte(&starArray[i+1]);
- }
- else {
- offset2 = pgm_read_byte(&starArray[i+1-starCount]);
- }
- leds[offset2] = CHSV(hue1, 255, 255);
- if ( (i+2)<starCount ) {
- offset3 = pgm_read_byte(&starArray[i+2]);
- }
- else {
- offset3 = pgm_read_byte(&starArray[i+2-starCount]);
- }
- leds[offset3] = CHSV(hue1, 255, 255);
- if ( (i+3)<starCount ) {
- offset4 = pgm_read_byte(&starArray[i+3]);
- }
- else {
- offset4 = pgm_read_byte(&starArray[i+3-starCount]);
- }
- leds[offset4] = CHSV(hue1, 255, 255);
- FastLED.show();
- leds[offset] = CRGB::Black;
- //delay(60);
- delay(40);
- }
- }
- void trace_inner_star() {
- uint8_t hue1 = random8(255);
- uint8_t offset2;
- for (int i=0; i<innerCount; i++) {
- uint8_t offset = pgm_read_byte(&innerArray[i]);
- //Serial.print ("i=");
- //Serial.print (i);
- //Serial.print (" array location = ");
- //Serial.println (offset);
- leds[offset] = CHSV(hue1, 255, 255);
- if ( (i+1)<starCount ) {
- offset2 = pgm_read_byte(&innerArray[i+1]);
- }
- else {
- offset2 = pgm_read_byte(&innerArray[i+1-innerCount]);
- }
- leds[offset2] = CHSV(hue1, 255, 255);
- FastLED.show();
- leds[offset] = CRGB::Black;
- delay(35);
- }
- }
- void fill_solid_color()
- {
- uint8_t hue1 = random8(255);
- uint8_t hue2 = random8(255);
- for (int i=0; i<NUM_LEDS; i++) {
- Serial.println (i);
- /*
- leds[i] = CRGB::Red;
- leds[i-1] = CRGB::Blue;
- FastLED.show();
- leds[i] = CRGB::Black;
- */
- leds[i] = CHSV( hue1, 255, 255);
- leds[i-1] = CHSV( hue2, 255, 255);
- FastLED.show();
- leds[i] = CRGB::Black;
- //delay(30);
- delay(20);
- }
- }
- void fill_black()
- {
- FastLED.clear();
- //fill_solid(leds, NUM_LEDS, CRGB::Black);
- }
- void rainbow()
- {
- // FastLED's built-in rainbow generator
- fill_rainbow( leds, NUM_LEDS, gHue, 7);
- }
- void sinelon()
- {
- // a colored dot sweeping back and forth, with fading trails
- fadeToBlackBy( leds, NUM_LEDS, 20);
- int pos = beatsin16(13,0,NUM_LEDS);
- leds[pos] += CHSV( gHue, 255, 192);
- }
- void bpm()
- {
- // colored stripes pulsing at a defined Beats-Per-Minute (BPM)
- uint8_t BeatsPerMinute = 62;
- CRGBPalette16 palette = PartyColors_p;
- uint8_t beat = beatsin8( BeatsPerMinute, 64, 255);
- for( int i = 0; i < NUM_LEDS; i++) { //9948
- leds[i] = ColorFromPalette(palette, gHue+(i*2), beat-gHue+(i*10));
- }
- }
- 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)] |= CHSV(dothue, 200, 255);
- dothue += 32;
- }
- }
- // draws a line that fades between 2 random colors
- // TODO: Add logic to rotate the starting point
- void gradient_fill() {
- // uint8_t hue1 = 60;
- // uint8_t hue2 = random8(255);
- uint8_t hue1 = random8(255);
- uint8_t hue2 = hue1 + random8(30,61);
- for( int i = 0; i < NUM_LEDS; i++){
- //fill_gradient (leds, 0, CHSV(0, 255, 255), i, CHSV(96, 255, 255), SHORTEST_HUES);
- fill_gradient (leds, 0, CHSV(hue1, 255, 255), i, CHSV(hue2, 255, 255), SHORTEST_HUES);
- delay(25);
- FastLED.show();
- //FastLED.clear();
- }
- }
- // draws a line that fades between 2 random colors
- // Added logic to rotate the starting corner
- void gradient_fill_2() {
- uint8_t hue1 = random8(255);
- uint8_t hue2 = hue1 + random8(30,61);
- uint8_t offset2;
- CRGB grad[NUM_LEDS]; // A place to save the gradient colors. (Don't edit this)
- fill_gradient (grad, 0, CHSV(hue1, 255, 255), NUM_LEDS, CHSV(hue2, 255, 255), SHORTEST_HUES);
- // display each pixel one at a time
- for( int i = 0; i < NUM_LEDS; i++){
- if ( (i+offset_for_star_rotation) < NUM_LEDS ) {
- offset2 = i+offset_for_star_rotation;
- }
- else {
- offset2 = i+offset_for_star_rotation-NUM_LEDS;
- }
- leds[offset2] = grad[i];
- //delay(25);
- delay(12);
- FastLED.show();
- }
- // rotate star starting point for next loop
- offset_for_star_rotation += 20;
- if (offset_for_star_rotation >= 100) {
- offset_for_star_rotation = 0;
- }
- FastLED.clear();
- }
- // Adapted from code by Marc Miller. Original Header:
- //
- //***************************************************************
- // Marquee fun (v3)
- // Pixel position down the strip comes from this formula:
- // pos = spacing * (i-1) + spacing
- // i starts at 0 and is incremented by +1 up to NUM_LEDS/spacing.
- //
- // Marc Miller, May 2016
- //***************************************************************
- void marque_v3() {
- for (uint8_t i=0; i<(NUM_LEDS/spacing); i++){
- for (uint8_t w = 0; w<width; w++){
- pos = (spacing * (i-1) + spacing + advance + w) % NUM_LEDS;
- if ( w % 2== 0 ){ // Is w even or odd?
- color = hue;
- } else {
- color = hue + hue2_shift;
- }
- leds[pos] = CHSV(color,255,255);
- }
- if (DEBUG==1) { // Print out lit pixels if DEBUG is true.
- Serial.print(" "); Serial.print(pos);
- }
- delay(10);
- }
- if (DEBUG==1) { Serial.println(" "); }
- FastLED.show();
- // Fade out tail or set back to black for next loop around.
- if (fadingTail == 1) {
- fadeToBlackBy(leds, NUM_LEDS,fadeRate);
- } else {
- for (uint8_t i=0; i<(NUM_LEDS/spacing); i++){
- for (uint8_t w = 0; w<width; w++){
- pos = (spacing * (i-1) + spacing + advance + w) % NUM_LEDS;
- leds[pos] = CRGB::Black;
- }
- }
- }
- // Advance pixel postion down strip, and rollover if needed.
- advance = (advance + delta + NUM_LEDS) % NUM_LEDS;
- }
- // END
Advertisement
Add Comment
Please, Sign In to add comment