Guest User

NeoCandle for FastLED (without delay)

a guest
Feb 25th, 2016
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.66 KB | None | 0 0
  1. // candle for Adafruit NeoPixel
  2. // 1 pixel version
  3. // by Tim Bartlett, December 2013
  4.  
  5. // current settings for 5v Trinket
  6.  
  7. #include <FastLED.h>
  8. #define LED_PIN 2
  9. #define NUM_LEDS 1
  10. #define BRIGHTNESS 100 // set at 25 for power saving
  11.  
  12. struct CRGB leds[NUM_LEDS];
  13.  
  14. byte onLedNumber = 0;
  15. unsigned long previousMillis = 0;
  16.  
  17. // color variables: mix RGB (0-255) for desired yellow
  18. int redPx = 255;
  19. int grnHigh = 110; //110-120 for 5v, 135 for 3.3v
  20. int bluePx = 10; //10 for 5v, 15 for 3.3v
  21.  
  22. // animation time variables, with recommendations
  23. int burnDepth = 20; //10 for 5v, 14 for 3.3v -- how much green dips below grnHigh for normal burn
  24. int flutterDepth = 30; //25 for 5v, 30 for 3.3v -- maximum dip for flutter
  25. int cycleTime = 120; //120 -- duration of one dip in milliseconds
  26.  
  27. // pay no attention to that man behind the curtain
  28. unsigned int fDelay;
  29. unsigned int fRep;
  30. int flickerDepth;
  31. int burnDelay;
  32. int burnLow;
  33. int flickDelay;
  34. int flickLow;
  35. int flutDelay;
  36. int flutLow;
  37.  
  38.  
  39. void flutter(unsigned int f);
  40. void flicker(unsigned int f);
  41. void burn(unsigned int f);
  42. void on(unsigned int f);
  43. void fire(int grnLow);
  44.  
  45.  
  46. void setup() {
  47. FastLED.addLeds<WS2811, LED_PIN, RGB>(leds, NUM_LEDS);
  48. FastLED.setBrightness( BRIGHTNESS );
  49. flickerDepth = (burnDepth + flutterDepth) / 2.4;
  50. burnLow = grnHigh - burnDepth;
  51. burnDelay = (cycleTime / 2) / burnDepth;
  52. flickLow = grnHigh - flickerDepth;
  53. flickDelay = (cycleTime / 2) / flickerDepth;
  54. flutLow = grnHigh - flutterDepth;
  55. flutDelay = ((cycleTime / 2) / flutterDepth);
  56. }
  57.  
  58. // In loop, call CANDLE STATES, with duration in seconds
  59. // 1. on() = solid yellow
  60. // 2. burn() = candle is burning normally, flickering slightly
  61. // 3. flicker() = candle flickers noticeably
  62. // 4. flutter() = the candle needs air!
  63.  
  64. void loop() {
  65. burn(10);
  66. flicker(5);
  67. burn(8);
  68. flutter(6);
  69. burn(3);
  70. on(10);
  71. burn(10);
  72. flicker(10);
  73. }
  74.  
  75.  
  76. // basic fire function - not called in main loop
  77. void fire(int grnLow) {
  78. for (int grnPx = grnHigh; grnPx > grnLow; grnPx--) {
  79. previousMillis = 0;
  80. unsigned long currentMillis = millis();
  81. if (currentMillis - previousMillis > fRep) {
  82. previousMillis = currentMillis;
  83. leds[onLedNumber] = CRGB(redPx, grnPx, bluePx);
  84. FastLED.show();
  85. }
  86. }
  87. for (int grnPx = grnLow; grnPx < grnHigh; grnPx++) {
  88. previousMillis = 0;
  89. unsigned long currentMillis = millis();
  90. if (currentMillis - previousMillis > fRep) {
  91. previousMillis = currentMillis;
  92. leds[onLedNumber] = CRGB(redPx, grnPx, bluePx);
  93. FastLED.show();
  94. }
  95. }
  96. }
  97.  
  98. // fire animation
  99. void on(unsigned int f) {
  100. previousMillis = 0;
  101. unsigned long currentMillis = millis();
  102. if (currentMillis - previousMillis > fRep) {
  103. previousMillis = currentMillis;
  104. fRep = f * 1000;
  105. int grnPx = grnHigh - 5;
  106. leds[onLedNumber] = CRGB(redPx, grnPx, bluePx);
  107. FastLED.show();
  108. }
  109. }
  110.  
  111. void burn(unsigned int f) {
  112. fRep = f * 8;
  113. fDelay = burnDelay;
  114. for (unsigned int var = 0; var < fRep; var++) {
  115. fire(burnLow);
  116. }
  117. }
  118.  
  119. void flicker(unsigned int f) {
  120. fRep = f * 8;
  121. fDelay = burnDelay;
  122. fire(burnLow);
  123. fDelay = flickDelay;
  124. for (unsigned int var = 0; var < fRep; var++) {
  125. fire(flickLow);
  126. }
  127. fDelay = burnDelay;
  128. fire(burnLow);
  129. fire(burnLow);
  130. fire(burnLow);
  131. }
  132.  
  133. void flutter(unsigned int f) {
  134. fRep = f * 8;
  135. fDelay = burnDelay;
  136. fire(burnLow);
  137. fDelay = flickDelay;
  138. fire(flickLow);
  139. fDelay = flutDelay;
  140. for (unsigned int var = 0; var < fRep; var++) {
  141. fire(flutLow);
  142. }
  143. fDelay = flickDelay;
  144. fire(flickLow);
  145. fire(flickLow);
  146. fDelay = burnDelay;
  147. fire(burnLow);
  148. fire(burnLow);
  149. }
Advertisement
Add Comment
Please, Sign In to add comment