Advertisement
GyroGearloose

LED SCOOTER BURNING MAN 2015 w/ ENCODER_V2

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