Can you write an Arduino program that simulates a flame effect using a strip of 22 WS2812 LEDs LED1 = base of the flame and LED22 = top of the flame, similar ro … The program includes three flame modes: normal fire color (Mode A), green/yellow flame with more green than yellow (Mode B), and blue/white flame (Mode C). You can toggle between A, B and C by pressing a button.
Assistant
Sure! Here's an example Arduino program that simulates a flame effect using a strip of 22 WS2812 LEDs and includes three flame modes (Mode A, Mode B, and Mode C). You can toggle between the modes by pressing a button.
#include <Adafruit_NeoPixel.h>
#define LED_PIN 6
#define NUM_LEDS 22
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, LED_PIN, NEO_GRB + NEO_KHZ800);
int buttonPin = 2;
int buttonState = 0;
int currentMode = 0;
void setup() {
strip.begin();
strip.show(); // Initialize all pixels to off
pinMode(buttonPin, INPUT_PULLUP);
// Uncomment the following line if you want to manually set the brightness
// strip.setBrightness(50);
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == LOW) {
currentMode++;
if (currentMode > 2) {
currentMode = 0;
}
delay(200); // debounce delay
}
switch (currentMode) {
case 0:
normalFire();
break;
case 1:
greenYellowFire();
break;
case 2:
blueWhiteFire();
break;
}
}
void normalFire() {
for (int i = 0; i < NUM_LEDS; i++) {
int flicker = random(0, 30); // Adjust the flickering effect
strip.setPixelColor(i, 255, 147 - flicker, 41); // Orange flame color
}
strip.show();
delay(100);
}
void greenYellowFire() {
for (int i = 0; i < NUM_LEDS; i++) {
int flicker = random(0, 20); // Adjust the flickering effect
int green = 255 - flicker;
int yellow = 128 - flicker;
strip.setPixelColor(i, green, yellow, 0); // Green-yellow flame color
}
strip.show();
delay(100);
}
void blueWhiteFire() {
for (int i = 0; i < NUM_LEDS; i++) {
int flicker = random(0, 20); // Adjust the flickering effect
strip.setPixelColor(i, 0, 0, 255 - flicker); // Blue-white flame color
}
strip.show();
delay(100);
}
In this code, the flame effect is simulated by changing the color of each LED in the strip randomly to create a flickering effect. The normalFire(), greenYellowFire(), and blueWhiteFire() functions control the color of the flame based on the selected mode. The buttonState variable is used to detect when the button is pressed, and the currentMode variable is used to keep track of the current flame mode. By default, the normalFire() mode is selected.