Can you write an Arduino program that simulates a flame effect using a strip of 22…
- User
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 to fire2012 from the Arduino FastLed library. The program should include 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
Certainly! Here's an example program that simulates a flame effect using an Arduino and a strip of 22 WS2812 LEDs:
#include <Adafruit_NeoPixel.h> #define LED_PIN 6 #define NUM_LEDS 22 #define BUTTON_PIN 2 Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, LED_PIN, NEO_GRB + NEO_KHZ800); const int buttonPin = BUTTON_PIN; int buttonState = 0; int mode = 0; void setup() { strip.begin(); strip.show(); pinMode(buttonPin, INPUT_PULLUP); } void loop() { buttonState = digitalRead(buttonPin); if (buttonState == LOW) { mode++; if (mode > 2) { mode = 0; } delay(200); } switch (mode) { case 0: flameEffect(255, 100, 0); // Normal fire color (Mode A) break; case 1: flameEffect(0, 255, 0); // Green/yellow flame with more green than yellow (Mode B) break; case 2: flameEffect(0, 0, 255); // Blue/white flame (Mode C) break; } } void flameEffect(int red, int green, int blue) { for (int i = 0; i < NUM_LEDS; i++) { int flicker = random(70, 255); strip.setPixelColor(i, flicker * red / 255, flicker * green / 255, flicker * blue / 255); } strip.show(); delay(random(25, 50)); } - User
can you do the same with the fastled library?
- Assistant
Certainly! Here's the modified program that uses the FastLED library to simulate a flame effect:
#include <FastLED.h> #define LED_PIN 6 #define NUM_LEDS 22 #define BUTTON_PIN 2 CRGB leds[NUM_LEDS]; int buttonState = 0; int mode = 0; void setup() { FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS); pinMode(BUTTON_PIN, INPUT_PULLUP); FastLED.setBrightness(200); // Set LED brightness (adjust as needed) } void loop() { buttonState = digitalRead(BUTTON_PIN); if (buttonState == LOW) { mode++; if (mode > 2) { mode = 0; } delay(200); } switch (mode) { case 0: flameEffect(CRGB::OrangeRed, CRGB::Yellow); // Normal fire color (Mode A) break; case 1: flameEffect(CRGB::Green, CRGB::Yellow); // Green/yellow flame with more green than yellow (Mode B) break; case 2: flameEffect(CRGB::LightBlue, CRGB::White); // Blue/white flame (Mode C) break; } } void flameEffect(CRGB color1, CRGB color2) { for (int i = 0; i < NUM_LEDS; i++) { leds[i] = blend(color1, color2, random(128, 255)); } FastLED.show(); delay(random(25, 50)); }