Hans has done a remarkable work with the LED Effects he has posted (Thank you VERY much, Hans! GREAT work!)
The fire effect has kept me busy for quite a while now. As I´m into marble-runs, I had the funny idea to combine runs with all kind of electronic gizmos like led meteor showers and motors and...
When I came across the Fire-effect, I desperatelywanted to include it in the marble-run I´m working on.
(I have scratched the sketch from different sources and adapted a few things for my purpose.)
Here is the idea:
A marble runs through a lightbeam with an LDR.
My Arduino sketch recognises the break of the beam (Sketch works so far :o)
Then it activates the Fire function (Works, too :o)
But: Fire just runs once and then returns to the main code! :o(
But I need Fire to run for about 10 sec or a number of times until it returns to the main code and the LDR can be triggered again by another marble.
In a different post Hans recommended to use a variable.
for(int i=0; i<25; i++) {
Sparkle(0xff, 0xff, 0xff, 0, 1000); //Need to make 25 times
}
But that was for Sparkle and as my Grandma used to say: "A sparkle doesn´t make a fire!".
I have no idea, where to "int" a variable which counts up (or a timer) and keeps Fire running before returning to the main code.
This is my very first post here and if you have any suggestions to improve my appearence here, you are very welcome.
I would appreciate your help so much!
Here is my code:
/* LDR triggered LED Strip with WS2812B and LED effect "Fire" (by Hans)
* 20.02.2021
*/
#include "FastLED.h"
#define NUM_LEDS 60
CRGB leds[NUM_LEDS];
#define PIN 6
const int analogPin = A0; // pin that the sensor is attached to
const int ledPin1 = A2; // pin that the green LED is attached to
const int ledPin2 = A1; // pin that the yellow LED is attached to
const int threshold = 800; // an arbitrary threshold level that's in the range of the analog input
void setup()
{
FastLED.addLeds<WS2811, PIN, GRB>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
// initialize the LED pin as an output:
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
}
void loop() {
// read the value of the ldr:
int analogValue = analogRead(analogPin);
// if the analog value is high enough, turn on the LED2yellow:
if (analogValue > threshold) {
digitalWrite(ledPin2, HIGH);
} else {
digitalWrite(ledPin2, LOW);
}
// if the analog value is low enough, turn on the LED1green:
if (analogValue < threshold) {
digitalWrite(ledPin1, HIGH);
Fire(55,120,15); // ruft die Funktion FIRE auf, wenn LDR unterbrochen
} else {
digitalWrite(ledPin1, LOW);
} // könnte ich hier die Funktion für FIRE aufrufen?
delay(1); // delay in between reads for stability
}
void Fire(int Cooling, int Sparking, int SpeedDelay) {
static byte heat[NUM_LEDS];
int cooldown;
{
// Step 1. Cool down every cell a little
for( int i = 0; i < NUM_LEDS; i++) {
cooldown = random(0, ((Cooling * 10) / NUM_LEDS) + 2);
if(cooldown>heat[i]) {
heat[i]=0;
} else {
heat[i]=heat[i]-cooldown;
}
}
// Step 2. Heat from each cell drifts 'up' and diffuses a little
for( int k= NUM_LEDS - 1; k >= 2; k--) {
heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2]) / 3;
}
// Step 3. Randomly ignite new 'sparks' near the bottom
if( random(255) < Sparking ) {
int y = random(7);
heat[y] = heat[y] + random(160,255);
//heat[y] = random(160,255);
}
// Step 4. Convert heat to LED colors
for( int j = 0; j < NUM_LEDS; j++) {
setPixelHeatColor(j, heat[j] );
}
showStrip();
delay(SpeedDelay);
}
}
void setPixelHeatColor (int Pixel, byte temperature) {
// Scale 'heat' down from 0-255 to 0-191
byte t192 = round((temperature/255.0)*191);
// calculate ramp up from
byte heatramp = t192 & 0x3F; // 0..63
heatramp <<= 2; // scale up to 0..252
// figure out which third of the spectrum we're in:
if( t192 > 0x80) { // hottest
setPixel(Pixel, 255, 255, heatramp);
} else if( t192 > 0x40 ) { // middle
setPixel(Pixel, 255, heatramp, 0);
} else { // coolest
setPixel(Pixel, heatramp, 0, 0);
}
}
void showStrip() {
#ifdef ADAFRUIT_NEOPIXEL_H
// NeoPixel
strip.show();
#endif
#ifndef ADAFRUIT_NEOPIXEL_H
// FastLED
FastLED.show();
#endif
}
void setPixel(int Pixel, byte red, byte green, byte blue) {
#ifdef ADAFRUIT_NEOPIXEL_H
// NeoPixel
strip.setPixelColor(Pixel, strip.Color(red, green, blue));
#endif
#ifndef ADAFRUIT_NEOPIXEL_H
// FastLED
leds[Pixel].r = red;
leds[Pixel].g = green;
leds[Pixel].b = blue;
#endif
}
void setAll(byte red, byte green, byte blue) {
for(int i = 0; i < NUM_LEDS; i++ ) {
setPixel(i, red, green, blue);
}
showStrip();
}