Advertisement
atuline

Fire with Encoder

Aug 3rd, 2015
862
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.28 KB | None | 0 0
  1. /*
  2. Control the color of Fire2012 with a rotary encoder  
  3. changed BRIGHTNESS, COOLING, and SPARKLING
  4.         200->64     55->80       120->50
  5.  
  6. Forgot where I found this version (with the encoder).
  7.  
  8. */
  9.  
  10. #include <FastLED.h>
  11.  
  12. #define DATA_PIN 6
  13. #define CLOCK_PIN 8
  14. //#define DATA_PIN_2   7  // this is for a second string
  15. //#define CLOCK_PIN_2  9
  16. /*
  17. #define COLOR_ORDER GRB
  18. #define CHIPSET     WS2811
  19. */
  20. #define NUM_LEDS    44
  21. #define BRIGHTNESS  128    // was 200
  22. #define FRAMES_PER_SECOND 40
  23. CRGB leds[NUM_LEDS];
  24.  
  25. #define encoder0PinA_M1 2                    // Encoder A
  26. #define encoder0PinB_M1 3                    // Encoder B
  27. volatile int encoder0Pos_M1 = 0;             // also negative values
  28.  
  29. // Fire2012 with programmable Color Palette
  30. //
  31. // This code is the same fire simulation as the original "Fire2012",
  32. // but each heat cell's temperature is translated to color through a FastLED
  33. // programmable color palette, instead of through the "HeatColor(...)" function.
  34. //
  35. // Four different static color palettes are provided here, plus one dynamic one.
  36. //
  37. // The three static ones are:
  38. //   1. the FastLED built-in HeatColors_p -- this is the default, and it looks
  39. //      pretty much exactly like the original Fire2012.
  40. //
  41. //  To use any of the other palettes below, just "uncomment" the corresponding code.
  42. //
  43. //   2. a gradient from black to red to yellow to white, which is
  44. //      visually similar to the HeatColors_p, and helps to illustrate
  45. //      what the 'heat colors' palette is actually doing,
  46. //   3. a similar gradient, but in blue colors rather than red ones,
  47. //      i.e. from black to blue to aqua to white, which results in
  48. //      an "icy blue" fire effect,
  49. //   4. a simplified three-step gradient, from black to red to white, just to show
  50. //      that these gradients need not have four components; two or
  51. //      three are possible, too, even if they don't look quite as nice for fire.
  52. //
  53. // The dynamic palette shows how you can change the basic 'hue' of the
  54. // color palette every time through the loop, producing "rainbow fire".
  55.  
  56. CRGBPalette16 gPal;
  57.  
  58. void setup() {
  59.   delay(2000); // sanity delay
  60.  
  61.   pinMode(encoder0PinA_M1, INPUT);
  62.   pinMode(encoder0PinB_M1, INPUT);
  63.   digitalWrite(encoder0PinA_M1, HIGH);       // use internal pull-ups
  64.   digitalWrite(encoder0PinB_M1, HIGH);  
  65.   attachInterrupt(1, doEncoderA_M1, CHANGE); // encoder pin on interrupt 0 (pin  2)
  66.   attachInterrupt(0, doEncoderB_M1, CHANGE); // encoder pin on interrupt 1 (pin  3)
  67.  
  68.  
  69. //  FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  70. FastLED.addLeds<APA102,DATA_PIN,CLOCK_PIN>(leds, NUM_LEDS).setCorrection( Candle );
  71. //FastLED.addLeds<NEOPIXEL,DATA_PIN>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  72.  
  73.   FastLED.setBrightness( BRIGHTNESS );
  74.  
  75.   // This first palette is the basic 'black body radiation' colors,
  76.   // which run from black to red to bright yellow to white.
  77.   gPal = HeatColors_p;
  78.  
  79.   // These are other ways to set up the color palette for the 'fire'.
  80.   // First, a gradient from black to red to yellow to white -- similar to HeatColors_p
  81.   //   gPal = CRGBPalette16( CRGB::Black, CRGB::Red, CRGB::Yellow, CRGB::White);
  82.  
  83.   // Second, this palette is like the heat colors, but blue/aqua instead of red/yellow
  84.   //   gPal = CRGBPalette16( CRGB::Black, CRGB::Blue, CRGB::Aqua,  CRGB::White);
  85.  
  86.   // Third, here's a simpler, three-step gradient, from black to red to white
  87.   //   gPal = CRGBPalette16( CRGB::Black, CRGB::Red, CRGB::White);
  88. Serial.begin(115200);
  89. }
  90.  
  91. void loop()
  92. {
  93.   // Add entropy to random number generator; we use a lot of it.
  94.   random16_add_entropy( random());
  95.  
  96.      static uint8_t hue = encoder0Pos_M1;     // assign the encoder reading to the color wheel
  97.      hue = encoder0Pos_M1 * 10;               // multiply if you need not so fine steps
  98.       Serial.println(hue);                    // amazing! unit_8 hue is automatically max 255 and then starts back at 0, super !!  
  99.      CRGB darkcolor  = CHSV(hue,255,192);     // pure hue, three-quarters brightness
  100.      CRGB lightcolor = CHSV(hue,128,255);     // half 'whitened', full brightness
  101.      gPal = CRGBPalette16( CRGB::Black, darkcolor, lightcolor, CRGB::White);
  102. //       }
  103.  
  104.  
  105.  
  106.  
  107.   // Fourth, the most sophisticated: this one sets up a new palette every
  108.   // time through the loop, based on a hue that changes every time.
  109.   // The palette is a gradient from black, to a dark color based on the hue,
  110.   // to a light color based on the hue, to white.
  111.   //
  112. /* start extra  
  113.      static uint8_t hue = 0;
  114.      hue++;
  115.      CRGB darkcolor  = CHSV(hue,255,192); // pure hue, three-quarters brightness
  116.      CRGB lightcolor = CHSV(hue,128,255); // half 'whitened', full brightness
  117.      gPal = CRGBPalette16( CRGB::Black, darkcolor, lightcolor, CRGB::White);
  118. // end extra -----------
  119. */
  120.   Fire2012WithPalette(); // run simulation frame, using palette colors
  121.  
  122.   FastLED.show(); // display this frame
  123.   FastLED.delay(1000 / FRAMES_PER_SECOND);
  124. }
  125.  
  126.  
  127. // Fire2012 by Mark Kriegsman, July 2012
  128. // as part of "Five Elements" shown here: http://youtu.be/knWiGsmgycY
  129. ////
  130. // This basic one-dimensional 'fire' simulation works roughly as follows:
  131. // There's a underlying array of 'heat' cells, that model the temperature
  132. // at each point along the line.  Every cycle through the simulation,
  133. // four steps are performed:
  134. //  1) All cells cool down a little bit, losing heat to the air
  135. //  2) The heat from each cell drifts 'up' and diffuses a little
  136. //  3) Sometimes randomly new 'sparks' of heat are added at the bottom
  137. //  4) The heat from each cell is rendered as a color into the leds array
  138. //     The heat-to-color mapping uses a black-body radiation approximation.
  139. //
  140. // Temperature is in arbitrary units from 0 (cold black) to 255 (white hot).
  141. //
  142. // This simulation scales it self a bit depending on NUM_LEDS; it should look
  143. // "OK" on anywhere from 20 to 100 LEDs without too much tweaking.
  144. //
  145. // I recommend running this simulation at anywhere from 30-100 frames per second,
  146. // meaning an interframe delay of about 10-35 milliseconds.
  147. //
  148. // Looks best on a high-density LED setup (60+ pixels/meter).
  149. //
  150. //
  151. // There are two main parameters you can play with to control the look and
  152. // feel of your fire: COOLING (used in step 1 above), and SPARKING (used
  153. // in step 3 above).
  154. //
  155. // COOLING: How much does the air cool as it rises?
  156. // Less cooling = taller flames.  More cooling = shorter flames.
  157. // Default 55, suggested range 20-100
  158. #define COOLING  75
  159.  
  160. // SPARKING: What chance (out of 255) is there that a new spark will be lit?
  161. // Higher chance = more roaring fire.  Lower chance = more flickery fire.
  162. // Default 120, suggested range 50-200.
  163. #define SPARKING 50
  164.  
  165.  
  166. void Fire2012WithPalette()
  167. {
  168. // Array of temperature readings at each simulation cell
  169.   static byte heat[NUM_LEDS];
  170.  
  171.   // Step 1.  Cool down every cell a little
  172.     for( int i = 0; i < NUM_LEDS; i++) {
  173.       heat[i] = qsub8( heat[i],  random8(0, ((COOLING * 10) / NUM_LEDS) + 2));
  174.     }
  175.  
  176.     // Step 2.  Heat from each cell drifts 'up' and diffuses a little
  177.     for( int k= NUM_LEDS - 1; k >= 2; k--) {
  178.       heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2] ) / 3;
  179.     }
  180.    
  181.     // Step 3.  Randomly ignite new 'sparks' of heat near the bottom
  182.     if( random8() < SPARKING ) {
  183.       int y = random8(7);
  184.       heat[y] = qadd8( heat[y], random8(160,255) );
  185.     }
  186.  
  187.     // Step 4.  Map from heat cells to LED colors
  188.     for( int j = 0; j < NUM_LEDS; j++) {
  189.       // Scale the heat value from 0-255 down to 0-240
  190.       // for best results with color palettes.
  191.       byte colorindex = scale8( heat[j], 240);
  192.       leds[j] = ColorFromPalette( gPal, colorindex);
  193.     }
  194. }
  195.  
  196.  //-----------------------------------------------------------------------------------------------------
  197. void doEncoderA_M1(){
  198.  
  199. if (digitalRead(encoder0PinA_M1) == HIGH) {      // look for a low-to-high on channel A
  200.    if (digitalRead(encoder0PinB_M1) == LOW) {    // check channel B to see which way encoder is turning
  201.      encoder0Pos_M1 = encoder0Pos_M1 + 1;   }    // CW
  202.       else {
  203.       encoder0Pos_M1 = encoder0Pos_M1 - 1;  }    // CCW
  204.    }
  205.  
  206. else  {                                          // must be a high-to-low edge on channel A                                      
  207.    if (digitalRead(encoder0PinB_M1) == HIGH) {   // check channel B to see which way encoder is turning  
  208.      encoder0Pos_M1 = encoder0Pos_M1 + 1;    }   // CW
  209.       else {
  210.      encoder0Pos_M1 = encoder0Pos_M1 - 1;    }   // CCW
  211.       }
  212.   }
  213. void doEncoderB_M1(){
  214.  
  215.  
  216. if (digitalRead(encoder0PinB_M1) == HIGH) {      // look for a low-to-high on channel B
  217.    if (digitalRead(encoder0PinA_M1) == HIGH) {   // check channel A to see which way encoder is turning
  218.      encoder0Pos_M1 = encoder0Pos_M1 + 1;    }   // CW
  219.       else {
  220.      encoder0Pos_M1 = encoder0Pos_M1 - 1;    }   // CCW
  221.    }
  222.  
  223.  
  224.  
  225. else {                                           // Look for a high-to-low on channel B
  226.     if (digitalRead(encoder0PinA_M1) == LOW) {   // check channel B to see which way encoder is turning  
  227.      encoder0Pos_M1 = encoder0Pos_M1 + 1;    }   // CW
  228.       else {
  229.      encoder0Pos_M1 = encoder0Pos_M1 - 1;    }   // CCW
  230.      }
  231. }
  232. //-------------------------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement