#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif
#define LED_PIN 6
#define LED_COUNT 60
#define LED_TURNS 6
#define LEDS_IN_TURN 10
#define BRIGHTNESS 254 // Set NeoPixel brightness
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
// Declare an array for LED brightness values
byte leds[LED_COUNT];
void setup() {
#if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
clock_prescale_set(clock_div_1); // For Adafruit Trinket
#endif
strip.begin(); // Initialize NeoPixel strip
strip.setBrightness(BRIGHTNESS);
strip.show(); // Initialize all pixels to 'off'
// Initialize brightness levels in the `leds[]` array
for (int row = 0; row < LED_TURNS; row++) {
for (int column = 0; column < LEDS_IN_TURN; column++) {
int index = column + row * LEDS_IN_TURN; // Precompute the pixel index
// Set initial brightness based on the row number
if (row == 0) {
leds[index] = 0x7F; // 50% brightness for the first row
} else if (row == 1 || row == 2) {
leds[index] = 0xFF; // 100% brightness for rows 1 and 2
} else {
leds[index] = 0xFF / row; // Dimmer brightness for rows 3 and above
}
}
}
}
void loop() {
// Map analog reads directly to hue and brightness values
int hue = map(analogRead(A1), 0, 1023, 0, 65535); // Directly map to hue range
int val = map(analogRead(A0), 0, 1023, 0, 255); // Directly map to brightness range
fire(hue, val, leds);
delay(1); // Minimal delay to allow for smooth animation
}
void fire(int hue, int val, byte *ledTable) {
for (int row = 0; row < LED_TURNS; row++) {
for (int column = 0; column < LEDS_IN_TURN; column++) {
int index = column + row * LEDS_IN_TURN; // Precompute the pixel index
// Apply a "flicker" effect to the brightness values for rows 3 and above
if ((ledTable[index] > 244) and (2 > row >= 1)) {
ledTable[index] += random(-40, -100); // Reduce brightness randomly
} else if (ledTable[index] < 10) {
ledTable[index] += random(30); // Increase brightness randomly
} else {
ledTable[index] += random(-8, 10); // Minor flicker adjustment
}
// Apply gamma correction and set pixel color with the calculated hue and brightness
uint32_t color = strip.gamma32(strip.ColorHSV(hue, 255, ledTable[index] & val));
strip.setPixelColor(index, color); // Set pixel color
}
}
strip.show(); // Display the updated pixel colors
}