hardfork

Arduino w/ Rotary FastLED BM2015

Jun 22nd, 2015
455
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.62 KB | None | 0 0
  1. ///////////////////////////////////////////////////////////
  2. ///////// LED SCOOTER BURNING MAN 2015 w/ ENCODER /////////
  3. ///////////////////////////////////////////////////////////
  4.  
  5. //INCLUDE LIBRARIES////////////////////////////////////////
  6. #include <FastLED.h>                                       //Include FastLED Library
  7. #include <Encoder.h>                                       //Include Rotary Encoder Library
  8. //#include <AdaEncoder.h>                                  //Include Ada Encoder Library *TEST*
  9. //#include <Bounce2.h>                                     //Include Bounce Library *TEST*
  10.  
  11. //LED SETUP////////////////////////////////////////////////
  12. #define LED_PIN 6                                          //LED Strip Data Pin
  13. #define LED_TYPE    WS2811                                 //LED Type
  14. #define COLOR_ORDER GRB                                    //LED Color Order
  15. #define NUM_LEDS 156                                       //Number of LEDs
  16. CRGB leds[NUM_LEDS];                                       //Name of LED Array
  17. CRGBPalette16 currentPalette;                              //Color Palette
  18. TBlendType    currentBlending;                             //Color Blending
  19.  
  20. //PINs for connection of the rotary////////////////////////
  21. //#define BUTTON_PIN 4                                     //Button Pin from Rotary Encoder
  22. #define encoderPinA 2                                      //Encoder A
  23. #define encoderPinB 3                                      //Encoder B
  24. volatile int encoderPos = 0;                               // also negative values
  25. byte count = 0;
  26. byte lastCount = 0;
  27. //Bounce debouncer = Bounce();
  28. //Bounce encoderPos = Bounce( encoderPinA,encoderPinB );
  29. //#define encoderSwitchPin 4                               //Encoder Switch Pin *ALT*
  30. //Encoder myEnc(3, 5);                                     //Encoder Pins *ALT*
  31.  
  32. //BUTTON SETUP/////////////////////////////////////////////
  33. //byte prevKeyState = HIGH;        
  34.  
  35. //MODE VARIABLES///////////////////////////////////////////
  36. int ledMode = 0;                                           //FIRST ACTIVE MODE
  37. int BRIGHTNESS = 255;                                      //0-255.  Lower number saves battery life, higher number is screamingly bright
  38. int SATURATION = 255;                                      //0 is white (no color) and 255 is fully saturated with color
  39. int HUE = 0;                                               //0-255, around the color wheel
  40. int STEPS = 4;                                             //Wider or narrower bands of color
  41. int SPEEDO = 10;                                           //The speed of the animation
  42.  
  43. //SETUP////////////////////////////////////////////////////
  44. void setup() {
  45.   delay( 2000 );                                           // power-up safety delay
  46. //  pinMode(BUTTON_PIN, INPUT);
  47.   pinMode(encoderPinA, INPUT);
  48.   pinMode(encoderPinB, INPUT);
  49.   digitalWrite(encoderPinA, HIGH);                         // use internal pull-ups
  50.   digitalWrite(encoderPinB, HIGH);                         // use internal pull-ups
  51.   attachInterrupt(0, doEncoderA, CHANGE);                  // encoder pin on interrupt 0 (pin  2)
  52.   attachInterrupt(1, doEncoderB, CHANGE);                  // encoder pin on interrupt 1 (pin  3)
  53. //  Serial.begin (9600)
  54.  
  55.  //FASTLED
  56.   FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  57.   FastLED.setBrightness(  BRIGHTNESS );
  58.   currentBlending = BLEND;
  59.  
  60.   Serial.begin(115200);
  61. }
  62.  
  63. //DEFINE NUMBER OF EFFECTS/////////////////////////////////
  64. #define NUM_MODES 13                                       //Change this to max number of effects
  65.  
  66. //MAIN LOOP////////////////////////////////////////////////
  67. void loop() {
  68.     switch (ledMode) {
  69.        case 0: BRIGHTNESS=255; Rainbow(); break;           //Rainbow Colors
  70.        case 1: BRIGHTNESS=255; RainbowStripe(); break;     //Rainbow Stripe
  71.        case 2: BRIGHTNESS=255; PartyColors(); break;       //Party Colors
  72.        case 3: BRIGHTNESS=255; HeatColors(); break;        //Heat Colors
  73.        case 4: BRIGHTNESS=255; Ocean(); break;             //Ocean Colors
  74.        case 5: BRIGHTNESS=255; Forest(); break;            //Forrest Colors
  75.        case 6: BRIGHTNESS=255; Flashlight(); break;        //Solid White
  76.        case 7: BRIGHTNESS=255; WhiteBlink(); break;        //Blinking White
  77.        case 8: BRIGHTNESS=255; Pink(); break;              //Solid Pink
  78.        case 9: BRIGHTNESS=255; Caution(); break;           //Blink Orange Caution
  79.        case 10: BRIGHTNESS=255; FIRE(); break;             //Blink Fire Red White
  80.        case 11: BRIGHTNESS=255; PYRO(); break;             //Blink Pyro Red Orange
  81.        case 12: BRIGHTNESS=255; LEO_Slow(); break;         //Blink LEO Slow
  82. }  
  83.  
  84. /*
  85. //BUTTON MANAGEMENT////////////////////////////////////////
  86.         byte currKeyState = digitalRead(BUTTON_PIN);
  87.      
  88.         if ((prevKeyState == LOW) && (currKeyState == HIGH)) {
  89.             keyRelease();
  90.         }
  91.        
  92.         prevKeyState = currKeyState;
  93. */  
  94.  
  95. //ENCODER MANAGEMENT///////////////////////////////////////
  96.                                                            // set the upper and lower limits for counting
  97.       if (encoderPos > NUM_MODES)                          // this limits the encoder max value
  98.           encoderPos = NUM_MODES;
  99.  
  100.       if (encoderPos < 0)                                  // this limits the encoder max value
  101.           encoderPos = 0;
  102.  
  103.       ledMode = encoderPos;
  104.       bool trigger = 0;
  105.       if (ledMode != lastCount) trigger = 1;               // if it's changed, trigger the different events
  106.    
  107. //    Serial.println(encoderPos);
  108. //    delay(2000);                                         // slow down the output
  109.     }
  110.  
  111.    
  112. //LIGHT PATTERNS///////////////////////////////////////////
  113. //RAINBOW COLORS~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  114. void Rainbow() {
  115.   FastLED.setBrightness(  BRIGHTNESS );
  116.   currentPalette = RainbowColors_p;
  117.  
  118.   static uint8_t startIndex = 0;
  119.   startIndex = startIndex + 1;
  120.  
  121.   FillLEDsFromPaletteColors( startIndex);
  122.    
  123.   FastLED.show();
  124.   FastLED.delay(SPEEDO);  
  125. }
  126.  
  127. //RAINBOW STRIPE~~-~~~~-~~~~-~~~~-~~~~-~~~~-~~~~-~~~~-~~~~~
  128. void RainbowStripe() {
  129.   FastLED.setBrightness(  BRIGHTNESS );
  130.   currentPalette = RainbowStripeColors_p;
  131.  
  132.   static uint8_t startIndex = 0;
  133.   startIndex = startIndex + 1;
  134.  
  135.   FillLEDsFromPaletteColors( startIndex);
  136.    
  137.   FastLED.show();
  138.   FastLED.delay(SPEEDO);  
  139. }
  140.  
  141. //PARTY COLORS~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  142. void PartyColors() {
  143.   FastLED.setBrightness(  BRIGHTNESS );
  144.   currentPalette = PartyColors_p;
  145.  
  146.   static uint8_t startIndex = 0;
  147.   startIndex = startIndex + 1;
  148.  
  149.   FillLEDsFromPaletteColors( startIndex);
  150.    
  151.   FastLED.show();
  152.   FastLED.delay(SPEEDO);  
  153. }
  154.  
  155. //HEAT COLORS~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  156. void HeatColors() {
  157.   FastLED.setBrightness(  BRIGHTNESS );
  158.   currentPalette = HeatColors_p;
  159.  
  160.   static uint8_t startIndex = 0;
  161.   startIndex = startIndex + 1;
  162.  
  163.   FillLEDsFromPaletteColors( startIndex);
  164.    
  165.   FastLED.show();
  166.   FastLED.delay(SPEEDO);  
  167. }
  168.  
  169. //OCEAN COLORS~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  170. void Ocean() {
  171.   FastLED.setBrightness(  BRIGHTNESS );
  172.   currentPalette = OceanColors_p;
  173.  
  174.   static uint8_t startIndex = 0;
  175.   startIndex = startIndex + 1;
  176.  
  177.   FillLEDsFromPaletteColors( startIndex);
  178.    
  179.   FastLED.show();
  180.   FastLED.delay(SPEEDO);  
  181. }
  182.  
  183. //CLOUD COLORS~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  184. void Cloud() {
  185.   FastLED.setBrightness(  BRIGHTNESS );
  186.   currentPalette = CloudColors_p;
  187.  
  188.   static uint8_t startIndex = 0;
  189.   startIndex = startIndex + 1;
  190.  
  191.   FillLEDsFromPaletteColors( startIndex);
  192.    
  193.   FastLED.show();
  194.   FastLED.delay(SPEEDO);  
  195. }
  196.  
  197. //FORREST COLORS~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  198. void Forest() {
  199.   FastLED.setBrightness(  BRIGHTNESS );
  200.   currentPalette = ForestColors_p;
  201.  
  202.   static uint8_t startIndex = 0;
  203.   startIndex = startIndex + 1;
  204.  
  205.   FillLEDsFromPaletteColors( startIndex);
  206.    
  207.   FastLED.show();
  208.   FastLED.delay(SPEEDO);  
  209. }
  210.  
  211. //LAVA COLORS~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  212. void Lava() {
  213.   FastLED.setBrightness(  BRIGHTNESS );
  214.   currentPalette = LavaColors_p;
  215.  
  216.   static uint8_t startIndex = 0;
  217.   startIndex = startIndex + 1;
  218.  
  219.   FillLEDsFromPaletteColors( startIndex);
  220.    
  221.   FastLED.show();
  222.   FastLED.delay(SPEEDO);  
  223. }
  224.  
  225. //FILL COLORS~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  226.  
  227. void FillLEDsFromPaletteColors( uint8_t colorIndex) {
  228.   for( int i = 0; i < NUM_LEDS; i++) {
  229.     leds[i] = ColorFromPalette( currentPalette, colorIndex, BRIGHTNESS, currentBlending);
  230.     colorIndex += STEPS;
  231.   }
  232. }
  233.  
  234. //SOLID WHITE----------------------------------------------
  235. void Flashlight() {
  236.    fill_solid(leds, NUM_LEDS, CRGB::White);  
  237.    FastLED.show();
  238. }
  239.  
  240. //SOLID PINK-----------------------------------------------
  241. void Pink() {
  242.    fill_solid(leds, NUM_LEDS, CRGB::HotPink);  
  243.    FastLED.show();
  244. }
  245.  
  246. //SOLID RED------------------------------------------------
  247. void Redlight() {
  248.    fill_solid(leds, NUM_LEDS, CRGB::Red);  
  249.    FastLED.show();
  250. }
  251.  
  252. //BLINK ORANGE SLOW-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  253. void Caution() {
  254.   fill_solid(leds, NUM_LEDS, CRGB::Orange);
  255.   FastLED.show();
  256.   delay(1000);
  257.   fill_solid(leds, NUM_LEDS, CRGB::Black);
  258.   FastLED.show();
  259.   delay(1000);
  260. }
  261.  
  262. //BLINK WHITE-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  263. void WhiteBlink() {
  264.   fill_solid(leds, NUM_LEDS, CRGB::White);
  265.   FastLED.show();
  266.   delay(250);
  267.   fill_solid(leds, NUM_LEDS, CRGB::Black);
  268.   FastLED.show();
  269.   delay(250);
  270. }
  271.  
  272. //FIRE BLINK RED WHITE-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
  273. void FIRE() {
  274.   fill_solid(leds, NUM_LEDS, CRGB::Red);
  275.   FastLED.show();
  276.   delay(250);
  277.   fill_solid(leds, NUM_LEDS, CRGB::White);
  278.   FastLED.show();
  279.   delay(250);
  280. }
  281.  
  282. //PYRO BLINK RED ORANGE-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  283. void PYRO() {
  284.   fill_solid(leds, NUM_LEDS, CRGB::Red);
  285.   FastLED.show();
  286.   delay(100);
  287.   fill_solid(leds, NUM_LEDS, CRGB::Orange);
  288.   FastLED.show();
  289.   delay(100);
  290. }
  291.  
  292. //LEO BLINK RED BLUE SLOW-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  293. void LEO_Slow() {
  294.   fill_solid(leds, NUM_LEDS, CRGB::Red);
  295.   FastLED.show();
  296.   delay(250);
  297.   fill_solid(leds, NUM_LEDS, CRGB::Blue);
  298.   FastLED.show();
  299.   delay(250);
  300. }
  301.  
  302. //LEO BLINK RED BLUE FAST-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  303. void LEO_Fast() {
  304.   fill_solid(leds, NUM_LEDS, CRGB::Red);
  305.   FastLED.show();
  306.   delay(100);
  307.   fill_solid(leds, NUM_LEDS, CRGB::Blue);
  308.   FastLED.show();
  309.   delay(100);
  310. }
  311.  
  312. /*
  313. //BUTTON CONTROL///////////////////////////////////////////
  314. //-called when button is pressed
  315. void shortKeyPress() {
  316.     Serial.println("short");
  317.     ledMode++;
  318.     if (ledMode > NUM_MODES){
  319.     ledMode=0; }
  320. }
  321.  
  322. // called when key goes from pressed to not pressed
  323. void keyRelease() {
  324.     Serial.println("key release");
  325.         shortKeyPress();
  326.     }
  327. */
  328.  
  329. //ENCODER CONTROL//////////////////////////////////////////
  330. void doEncoderA(){                                         // Encoder A
  331.  
  332. if (digitalRead(encoderPinA) == HIGH) {                    // look for a low-to-high on channel A
  333.    if (digitalRead(encoderPinB) == LOW) {                  // check channel B to see which way encoder is turning
  334.      encoderPos = encoderPos + 1;   }                      // CW
  335.       else {
  336.       encoderPos = encoderPos - 1;  }                      // CCW
  337.    }
  338.  
  339. else  {                                                    // must be a high-to-low edge on channel A                                      
  340.    if (digitalRead(encoderPinB) == HIGH) {                 // check channel B to see which way encoder is turning  
  341.      encoderPos = encoderPos + 1;    }                     // CW
  342.       else {
  343.      encoderPos = encoderPos - 1;    }                     // CCW
  344.       }
  345.   }
  346.  
  347. void doEncoderB(){                                         // Encoder B
  348.  
  349. if (digitalRead(encoderPinB) == HIGH) {                    // look for a low-to-high on channel B
  350.    if (digitalRead(encoderPinA) == HIGH) {                 // check channel A to see which way encoder is turning
  351.      encoderPos = encoderPos + 1;    }                     // CW
  352.       else {
  353.      encoderPos = encoderPos - 1;    }                     // CCW
  354.    }
  355.  
  356. else {                                                     // Look for a high-to-low on channel B
  357.     if (digitalRead(encoderPinA) == LOW) {                 // check channel B to see which way encoder is turning  
  358.      encoderPos = encoderPos + 1;    }                     // CW
  359.       else {
  360.      encoderPos = encoderPos - 1;    }                     // CCW
  361.      }
  362. }
  363. //END//////////////////////////////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment