#include <iostream>
#include <string>
#include <chrono>
#include <random>
#include "Adafruit_DotStar.h"
// Strip constants
#define DATAPIN 0
#define CLOCKPIN 11
#define LED_COUNT 150
#define BRIGHTNESS 255
// Hue constants
#define RAINBOW_SIZE 12
#define HUE_OFFSET 100
// Color wheel array of RGB values
DWORD colorWheel[RAINBOW_SIZE] = {
0xFF0000, 0xD52A00, 0xAB5500, 0xAB7F00, 0xABAB00,
0x56D500, 0x009657, 0x00ABAB, 0x0056D5, 0x002391,
0x5700FF, 0xAB00AB, 0xAB0057, 0xAB0023, 0xAB0000,
0xFF0000
};
// LED strip instance
Adafruit_DotStar strip(LED_COUNT, DATAPIN, CLOCKPIN, DOTSTAR_BRG);
// Rainbow indices
uint16_t indexA = 0;
uint16_t indexB = 0;
// Color delta
uint8_t deltaHue = 5;
// Calculate color based on HSV
uint32_t hsvToRgb(float h, float s, float v) {
int i = int(h * 6.0f);
float f = (h * 6.0f) - i;
float p = v * (1.0f - s);
float q = v * (1.0f - f * s);
float t = v * (1.0f - (1.0f - f) * s);
i %= 6;
DWORD color = ((i == 0) ? ((uint32_t)v << 16) :
(i == 1) ? ((uint32_t)q << 16) :
(i == 2) ? ((uint32_t)p << 16) :
(i == 3) ? ((uint32_t)p << 8) :
(i == 4) ? ((uint32_t)t << 8) :
(i == 5) ? ((uint32_t)v << 8) : 0);
return color |= (0xFF0000);
}
// Initialize the LED strip
void initStrip() {
strip.begin();
strip.show();
}
// Display rainbow pattern on the LED strip
void displayRainbow() {
for (int i = 0; i < LED_COUNT; i++) {
int index = (i + indexA) % RAINBOW_SIZE;
int hue = (index * deltaHue) % 255;
uint32_t color = hsvToRgb(hue / 255.0f, 1.0f, 1.0f);
strip.setPixelColor(i, color);
}
indexA += 1;
strip.show();
}
// Display exploding rainbow pattern on the LED strip
void displayExplodingRainbow() {
for (int i = 0; i < LED_COUNT; i++) {
int index = (i + indexB) % RAINBOW_SIZE;
int hue = (index * deltaHue + HUE_OFFSET) % 255;
uint32_t color = hsvToRgb(hue / 255.0f, 1.0f, 1.0f);
strip.setPixelColor(i, color);
}
indexB += 2;
strip.show();
}
void setup() {
// Initialize the LED strip
initStrip();
// Set the brightness of the LED strip
strip.setBrightness(BRIGHTNESS);
}
void loop() {
// Uncomment the desired pattern
// displayRainbow();
displayExplodingRainbow();
// Optional delay
// delay(50);
}