Certainly! Here's an example Arduino program that simulates a flame effect using a strip of 22 WS2812 LEDs:
#include <Adafruit_NeoPixel.h>
#define LED_PIN 6
#define NUM_LEDS 22
#define BUTTON_PIN 2
Adafruit_NeoPixel leds = Adafruit_NeoPixel(NUM_LEDS, LED_PIN, NEO_GRB + NEO_KHZ800);
enum FlameMode { ModeA, ModeB, ModeC };
FlameMode currentMode = ModeA;
int buttonState = 0;
int lastButtonState = 0;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
void setup() {
leds.begin();
pinMode(BUTTON_PIN, INPUT_PULLUP);
}
void loop() {
readButton();
switch (currentMode) {
case ModeA:
drawFlame(255, 95, 0); // normal fire color
break;
case ModeB:
drawFlame(0, 255, 0); // green/yellow flame with more green than yellow
break;
case ModeC:
drawFlame(0, 0, 255); // blue/white flame
break;
}
}
void drawFlame(uint8_t r, uint8_t g, uint8_t b) {
static byte heat[NUM_LEDS];
for (int i = 0; i < NUM_LEDS; i++) {
heat[i] = qsub8(heat[i], random8(0, ((NUM_LEDS * 8) / 22) + 2));
}
for (int k = NUM_LEDS - 1; k >= 2; k--) {
heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2]) / 3;
}
for (int j = 0; j < NUM_LEDS; j++) {
CRGB color = HeatColor(heat[j]);
color.red = color.red * r / 255;
color.green = color.green * g / 255;
color.blue = color.blue * b / 255;
leds.setPixelColor(j, color);
}
leds.show();
}
void readButton() {
int reading = digitalRead(BUTTON_PIN);
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == LOW) {
switchFlameMode();
}
}
}
lastButtonState = reading;
}
void switchFlameMode() {
switch (currentMode) {
case ModeA:
currentMode = ModeB;
break;
case ModeB:
currentMode = ModeC;
break;
case ModeC:
currentMode = ModeA;
break;
}
}
Make sure to install the Adafruit_NeoPixel library and the FastLED library before uploading the program to your Arduino board. You can find them in the Arduino Library Manager.
In this program, the flame effect is implemented in the drawFlame function. It uses the heat array to store the "heat" values for each LED, and the CRGB class from the FastLED library to set the color for each LED based on the heat value. The qsub8 function is used to subtract from the heat value, and the HeatColor function is used to convert the heat value to a color.
The button is wired to digital pin 2 and is set as an input with internal pull-up resistor enabled (INPUT_PULLUP). The button state is read in the readButton function, and the flame mode is switched when the button is pressed (from ModeA to ModeB, ModeB to ModeC, and ModeC to ModeA).
You can modify the RGB values in the drawFlame function to adjust the desired flame colors for each mode.