Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <FastLED.h>
- FASTLED_USING_NAMESPACE
- // Credits -Mark Kriegsman, December 2014
- // https://armaizadenwala.com/blog/how-to-create-a-led-rave-mask-using-arduino/
- //LED
- #define DATA_PIN A5
- //#define CLK_PIN 4
- #define LED_TYPE WS2812B
- #define COLOR_ORDER GRB
- #define NUM_LEDS 65
- CRGB leds[NUM_LEDS];
- #include "constants.h"
- #define FRAMES_PER_SECOND 120
- // Push button for Animation Cycle
- #define buttonAnimationPin A1
- uint8_t buttonAnimationState = 0; // current state of the button
- uint8_t lastButtonAnimationState = 0; // previous state of the button
- // Push button for Palette Cycle
- #define buttonPalettePin A2
- uint8_t buttonPaletteState = 0; // current state of the button
- uint8_t lastButtonPaletteState = 0; // previous state of the button
- //Potentiometer for Brightness
- #define potBrightnessPin A0 // analog pin used to connect the potentiometer
- CRGBPalette16 currentPalette;
- TBlendType currentBlending;
- // ----------------------------------------------------------------------------------------- SETUP EXECUTABLE ----------------------
- void setup() {
- delay(1000); // 1 second delay for recovery
- FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip); // tell FastLED about the LED strip configuration
- pinMode(buttonAnimationPin, INPUT); // initialize the pushbuttons pin as an input for animation cycle
- pinMode(buttonPalettePin, INPUT); // initialize the pushbuttons pin as an input for palette cycle
- Serial.begin(9600); // Allow Serial Monitor for debugging
- }
- // ------------------------------------------------------------------------------------------------ END SETUP ------------------------
- // List of Patterns to cycle through. Each is defined as a separate function below.
- typedef void (*SimplePatternList[])();
- // Decleration of all Animation Functions
- SimplePatternList gPatterns = { customBigVPattern, customOneTwoPattern, customTrianglesPattern, customCirclesPattern, customOutwardsWingsPattern, customOutwardsColumnPattern, customScrollingRowPattern, customScrollingColumnPattern, bpm, rainbow, rainbowWithGlitter, sinelon, confetti, juggle };
- #define ARRAY_SIZE(gPatterns) (sizeof(gPatterns) / sizeof((gPatterns)[0]))
- uint8_t gCurrentPatternNumber = 0; // Index number of which pattern is current
- // List of Palettes to cycle through. All defined below.
- extern const TProgmemRGBGradientPalettePtr gGradientPalettes[];
- extern uint8_t gGradientPaletteCount;
- uint8_t gCurrentPaletteNumber = 0; // Current number of available palettes the 'playlist' of color palettes
- uint8_t gHue = 0; // rotating "base color" used by many of the patterns
- // ---------------------------------------------------------------------------------------------MAIN EXECUTABLE ----------------------
- void loop()
- {
- checkAnimationButton(); // Check animation push button for state change
- gPatterns[gCurrentPatternNumber](); // Call gPatterns function and refer to position [GCurrentPatternNumber] to execute the pattern animation function in that position (e.g. 0 is rainbow)
- checkPaletteButton();
- currentPalette = gGradientPalettes[ gCurrentPaletteNumber ];
- potentialBrightness(); // Check potentiometer for brighness change
- //FastLED.setBrightness(255);
- FastLED.show(); // send the 'leds' array out to the actual LED strip
- FastLED.delay(1000 / FRAMES_PER_SECOND); // insert a delay to keep the framerate modest
- EVERY_N_SECONDS( 5 ) {
- gCurrentPaletteNumber = addmod8( gCurrentPaletteNumber, 1, gGradientPaletteCount);
- //TargetPalette = gGradientPalettes[ gCurrentPaletteNumber ];
- }
- EVERY_N_MILLISECONDS( 10 ) { // Change Hue base color every X milliseconds
- gHue++; // slowly cycle the "base color" through the rainbow
- }
- }
- // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX MAIN END XXXXXXXXXXXXXXXXXX
- // --------------------------------------------------------------------- INPUT CONTROLS FUNCTIONS START ----------------------------
- // Animation cycle based on Push Button
- void checkAnimationButton() {
- // read the pushbutton input pin:
- buttonAnimationState = digitalRead(buttonAnimationPin);
- // compare the buttonState to its previous state
- if (buttonAnimationState != lastButtonAnimationState) {
- // if the state has changed, increment the counter
- if (buttonAnimationState == HIGH) {
- gCurrentPatternNumber = (gCurrentPatternNumber + 1) % ARRAY_SIZE( gPatterns);
- //Serial.print("Pattern: ");
- //Serial.println(gCurrentPatternNumber);
- }
- // Delay a little bit to avoid bouncing
- delay(10);
- }
- // save the current state as the last state, for next time through the loop
- lastButtonAnimationState = buttonAnimationState;
- }
- // Palette cycle based on Push Button
- void checkPaletteButton() {
- // read the pushbutton input pin:
- buttonPaletteState = digitalRead(buttonPalettePin);
- // compare the buttonState to its previous state
- if (buttonPaletteState != lastButtonPaletteState) {
- // if the state has changed, increment the counter
- if (buttonPaletteState == HIGH) {
- gCurrentPaletteNumber = (gCurrentPaletteNumber + 1) % gGradientPaletteCount;
- //Serial.print("Palette: ");
- //Serial.println(gCurrentPaletteNumber);
- }
- // Delay a little bit to avoid bouncing
- delay(10);
- }
- // save the current state as the last state, for next time through the loop
- lastButtonPaletteState = buttonPaletteState;
- }
- // Brightness Check using Potentiometer
- void potentialBrightness() {
- FastLED.setBrightness(map(analogRead(potBrightnessPin), 0, 1050, 20, 100)); // scale the numbers from the potential value (20 - 100)
- }
- // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX INPUT CONTROLS FUNCTIONS END XXXXXXXXXXXXXXXXXXXXXX
- // ----------------------------------------------------------------------- ANIMATION FUNCTIONS START ----------------------------
- // Pattern Animation - Preset: Rainbow
- void rainbow()
- {
- // FastLED's built-in rainbow generator
- fill_rainbow( leds, NUM_LEDS, gHue, 5);
- }
- // Pattern Animation - Preset: Rainbow w/ Glitter
- void rainbowWithGlitter()
- {
- // built-in FastLED rainbow, plus some random sparkly glitter
- rainbow();
- addGlitter(80);
- }
- // Pattern Animation Add-on - Preset: Glitter
- void addGlitter( fract8 chanceOfGlitter)
- {
- if ( random8() < chanceOfGlitter) {
- leds[ random16(NUM_LEDS) ] += CRGB::White;
- }
- }
- // Pattern Animation - Preset: Confetti
- void confetti()
- {
- // random colored speckles that blink in and fade smoothly
- fadeToBlackBy( leds, NUM_LEDS, 10);
- int pos = random16(NUM_LEDS);
- leds[pos] += CHSV( gHue + random8(64), 200, 255);
- }
- // Pattern Animation - Preset: Sinelon
- void sinelon()
- {
- // a colored dot sweeping back and forth, with fading trails
- fadeToBlackBy( leds, NUM_LEDS, 20);
- int pos = beatsin16( 13, 0, NUM_LEDS - 1 );
- leds[pos] += CHSV( gHue, 255, 192);
- }
- // Pattern Animation - Preset: Three Step
- void threeStep()
- {
- uint8_t colorIndex = gHue;
- for( int i = 0; i < NUM_LEDS; i++) {
- leds[i] = ColorFromPalette( currentPalette, colorIndex);
- colorIndex += 3;
- }
- }
- // Pattern Animation - Preset: BPM
- void bpm()
- {
- // colored stripes pulsing at a defined Beats-Per-Minute (BPM)
- uint8_t BeatsPerMinute = 300;
- //CRGBPalette16 palette = PartyColors_p;
- uint8_t beat = beatsin8( BeatsPerMinute, 64, 255);
- for ( int i = 0; i < NUM_LEDS; i++) { //9948
- leds[i] = ColorFromPalette(currentPalette, gHue + (i * 2), beat - gHue + (i * 10));
- }
- }
- // Pattern Animation - Preset: Juggle
- 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;
- }
- }
- // ----------------------- Custom animation calls --------------------
- // Pattern Animation - Custom Scrolling Line
- void customScrollingRowPattern() {
- customPattern(scrollingRowPattern, scrollingColors, false, 2, 16);
- }
- // Pattern Animation - Custom Scrolling Column
- void customScrollingColumnPattern() {
- customPattern(scrollingColumnPattern, scrollingColors, false, 2, 16);
- }
- // Pattern Animation - Custom Outwards Columns
- void customOutwardsColumnPattern() {
- customPattern(outwardsColumnPattern, scrollingColors, false, 2, 16);
- }
- // Pattern Animation - Custom Outwards Wings
- void customOutwardsWingsPattern() {
- customPattern(outwardsWingsPattern, scrollingColors, true, 4, 16);
- }
- // Pattern Animation - Custom Circles
- void customCirclesPattern() {
- customPattern(circlesPattern, scrollingColors, false, 2, 16);
- }
- // Pattern Animation - Custom Triangles
- void customTrianglesPattern() {
- customPattern(trianglesPattern, scrollingColors, false, 2, 16);
- }
- void customOneTwoPattern() {
- customPattern(oneTwoPattern, scrollingColors, false, 2, 16);
- }
- // Pattern Animation - Custom Big V
- void customBigVPattern() {
- customPattern(bigVPattern, scrollingColors, false, 3, 265);
- }
- // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX ANIMATION FUNCTIONS END XXXXXXXXXXXXXXXXXXXXX
- // --------------------------------------------------------------- customPattern () START -----------------------------------
- //Line Flipping for customPattern - Function for converting order of LEDs every other row
- uint_least8_t FlipIndex(uint_least8_t nIndex, uint_least8_t nMidpoint)
- {
- return nMidpoint * 2 - nIndex;
- }
- // Line Flipping Check for customPattern - Function for checking if current row needs to be converted or not (runs FlipIndex function)
- uint_least8_t ConvertIndex(uint_least8_t i)
- {
- if (15 <= i && i <= 28) { // Check if currently on 2nd line
- return FlipIndex(i, 21) + 1;
- }
- if (42 <= i && i <= 53) { // Check if currently on 4th line
- return FlipIndex(i, 47) + 1;
- }
- if (65 <= i && i <= 74) { // Check if currently on 6th line
- return FlipIndex(i, 69) + 1;
- }
- return i; // if on 1st, 3rd, 5th or 7th line, don't flip position
- }
- void customPattern(uint_least8_t pattern[NUM_LEDS], uint_least8_t patternColors[], bool reverse, float speed, uint_least8_t max) {
- // Function that creates the pattern animation using these variables:
- // pattern - the selected layout of the animation
- // patternColors - sequence of colors from palette to indexes
- // reverse - choose direction through true / false
- // speed - adjust the run speed of a loop, bigger number is slower
- // max - select the max number of colors from the color palette (jumps of 16 on gradient scale 0 - 255, total of 16 steps)
- currentBlending = LINEARBLEND;
- for (uint_least8_t x = 0; x < max; x++) { // Run through all LED Indexs, until you hit the MAX allowed index number
- potentialBrightness(); // Adjust brightness based on check
- for (uint_least8_t z = 0; z < (4 * speed); z++) { // Speed adjustment
- for (uint_least8_t i = 0; i < NUM_LEDS; i++) { // Run through all the LEDs, until you hit the last LED
- if ((buttonPaletteState != lastButtonPaletteState) || (buttonAnimationState != lastButtonAnimationState)){
- break; // Check for buttoon press and break early
- }
- uint_least8_t convertedI = ConvertIndex(i); // Run through line flipping function, flip if needed to swap LED position
- uint8_t color = (pattern[i] + x) % max; // Color is the index of the current LED, will be used in the next
- // function to map to the color bridge patternColors[], so LEDs with
- // the same index on the pattern get colored the same
- leds[convertedI] = ColorFromPalette(currentPalette, patternColors[color]*16, 255, currentBlending);
- // color the LED using the currently selected pattern, with the
- // selected gradient value based on (color), assuming gradient is in
- // equal jumps of 16.
- // CHANGE TO THE ABOVE: Instead of jumps of 16, run through only the
- // declared colors on the gradient no matter the size, but skip the
- //ranges between them
- } // i Loop end, run through all LEDs
- FastLED.delay(1); // Delay by X milliseconds
- } // z Loop end, speed adjustment
- } // x loop end, run over pattern indexes
- } // customPattern() end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement