Guest User

lavalampidea

a guest
May 22nd, 2021
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.71 KB | None | 0 0
  1. // Fire2012WithPaletteCycle
  2. // Version 0.12 Revision 3
  3.  
  4. // Portions of this code originated in the FastLED library, and was
  5. // modified & amended by Charles Gantt on 7/1/2017. Visit Charles' website
  6. // at http://www.themakersworkbench.com
  7.  
  8. // Origial Fire2012 Code by:
  9. // Mark Kriegsman, July 2012
  10. // as part of "Five Elements" shown here: http://youtu.be/knWiGsmgycY
  11.  
  12. // This code is the same fire simulation as the original "Fire2012",
  13. // but each heat cell's temperature is translated to color through a FastLED
  14. // programmable color palette, instead of through the "HeatColor(...)" function
  15. // with additional custom defined color palettes.
  16. // The different palettes can be cycled through by a push of a button
  17. // connected to pin 2, and 5V.
  18.  
  19. #include <FastLED.h>
  20.  
  21. #define LED_PIN 6 // the pin which attaches to the neopixel data pin
  22. #define COLOR_ORDER GRB // sets the color order in which the LEDs are designed for
  23. #define CHIPSET WS2812B // the chipset that the neopixels use
  24. #define NUM_LEDS 10 // how many leds are being adderessed
  25.  
  26. #define BRIGHTNESS 150 // sets the overall brightness of the leds
  27. #define FRAMES_PER_SECOND 60 // how fast should the led animation render
  28. #define COOLING 55 // defines the level at which the lighting effect fades before a new "flame" generates
  29. #define SPARKING 120 // defines the rate of flicker which we will see from the flame animation
  30.  
  31. int switchPin = 12; // switch is connected to pin 2
  32. int val; // variable for reading the pin status
  33. int buttonState; // variable to hold the button state
  34. int buttonPresses = 0; // how many times the button has been pressed
  35.  
  36. bool gReverseDirection = false; // set this to true if you need to invert the animation direction
  37.  
  38. CRGB leds[NUM_LEDS]; // sets up an array that we can manipulate to set/clear led data.
  39.  
  40. CRGBPalette16 currentPalette; // sets a variable for CRGBPalette16 which allows us to change this value later
  41.  
  42. void setup() {
  43. delay(1500); // sets a delay to insure that everything has initalized before proceeding with the setup
  44.  
  45. // Informs the library that a strand of NEOPIXEL's on pin 6 and those leds will use the led array "leds", and there are NUM_PIXELS (aka 8) of them.
  46. FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  47.  
  48. FastLED.setBrightness( BRIGHTNESS ); // sets the brightness to the predetermined levels
  49. pinMode(switchPin, INPUT_PULLUP); // Set the switch pin as input
  50. Serial.begin(9600); // Set up serial communication at 9600bps
  51. buttonState = digitalRead(switchPin); // read the initial state of the button
  52. }
  53.  
  54. void loop()
  55. {
  56. random16_add_entropy( random()); // Add entropy to random number generator; we use a lot of it.
  57.  
  58. // The following if statements read the number of times the button has been pressed
  59. // and then sets the currentPalette variable to the defined CRGBPalette16 color palette.
  60. // for example: if the button has been pressed 1 time, set the palette to HeatColors. If
  61. // the button has been pressed 4 times, set the palette to a custom defined palette found in
  62. // that statement. The else statement at the end helps us return to the top of the if statments.
  63.  
  64. if (buttonPresses == 0) {
  65. currentPalette = CRGB(20,20,8);
  66. }
  67. if (buttonPresses == 1) {
  68. currentPalette = HeatColors_p;
  69. }
  70. if (buttonPresses == 2) {
  71. currentPalette = CRGBPalette16( CRGB::Black, CRGB::Blue, CRGB::Aqua, CRGB::White);
  72. }
  73. if (buttonPresses == 3) {
  74. currentPalette = CRGBPalette16( CRGB::Black, CRGB::Green, CRGB::GreenYellow, CRGB::Yellow);
  75. }
  76. if (buttonPresses == 4) {
  77. currentPalette = CRGBPalette16( CRGB::Black, CRGB::Green, CRGB::Purple, CRGB::Orange);
  78. }
  79. if (buttonPresses == 5) {
  80. currentPalette = CRGBPalette16( CRGB::Black, CRGB::Blue, CRGB::Purple, CRGB::Red);
  81. }
  82. if (buttonPresses == 6) {
  83. currentPalette = CRGB(255,220,70);
  84. }
  85. if (buttonPresses == 7) {
  86. currentPalette = CRGB(255,70,000);
  87. }
  88. if (buttonPresses == 8) {
  89. currentPalette = CRGBPalette16( CRGB::Maroon);
  90. }
  91. if (buttonPresses == 9) {
  92. currentPalette = CRGBPalette16( CRGB::Blue);
  93. }
  94. if (buttonPresses == 10) {
  95. static uint8_t hue = 0;
  96. hue++;
  97. CRGB darkcolor = CHSV(hue, 255, 192); // pure hue, three-quarters brightness
  98. CRGB lightcolor = CHSV(hue, 128, 255); // half 'whitened', full brightness
  99. currentPalette = CRGBPalette16( CRGB::Black, darkcolor, lightcolor, CRGB::White);
  100. }
  101. else {
  102. }
  103.  
  104.  
  105. Fire2012WithPalette(); // calls the function defined below this loop & runs simulation frames, using palette colors
  106.  
  107. FastLED.show(); // display this frame
  108. FastLED.delay(1000 / FRAMES_PER_SECOND); // sets a delay using the number 1000 devided by the predetermined frames per second.
  109.  
  110. val = digitalRead(switchPin); // read input value and store it in val
  111. if (val != buttonState) { // the button state has changed!
  112. if (val == LOW) { // check if the button is pressed
  113. buttonPresses++; // increment the buttonPresses variable
  114. Serial.print("Button has been pressed "); // prints the statement between "" in the serial terminal
  115. Serial.print(buttonPresses); // reads the value stored in the buttonPresses variable.
  116. Serial.println(" times"); // prints the statement between "" in the serial terminal
  117. }
  118. }
  119. if (buttonPresses > 10) { // reads the number of button presses, and if that number is greater than 10 then
  120. buttonPresses = 0; // reset the number of button presses to 0
  121. }
  122. buttonState = val; // save the new state in our variable
  123.  
  124. }
  125.  
  126. void Fire2012WithPalette() // defines a new function
  127. {
  128. // Array of temperature readings at each simulation cell
  129. static byte heat[NUM_LEDS];
  130.  
  131. // Step 1. Cool down every cell a little
  132. for ( int i = 0; i < NUM_LEDS; i++) {
  133. heat[i] = qsub8( heat[i], random8(0, ((COOLING * 10) / NUM_LEDS) + 2));
  134. }
  135.  
  136. // Step 2. Heat from each cell drifts 'up' and diffuses a little
  137. for ( int k = NUM_LEDS - 1; k >= 2; k--) {
  138. heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2] ) / 3;
  139. }
  140.  
  141. // Step 3. Randomly ignite new 'sparks' of heat near the bottom
  142. if ( random8() < SPARKING ) {
  143. int y = random8(7);
  144. heat[y] = qadd8( heat[y], random8(160, 255) );
  145. }
  146.  
  147. // Step 4. Map from heat cells to LED colors
  148. for ( int j = 0; j < NUM_LEDS; j++) {
  149. // Scale the heat value from 0-255 down to 0-240
  150. // for best results with color palettes.
  151. byte colorindex = scale8( heat[j], 240);
  152. leds[j] = ColorFromPalette( currentPalette, colorindex);
  153. }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment