#include <Adafruit_NeoPixel.h>
#define neoPin 33 // Neopixel GPIO pin
#define buttonPin 34 // Push button GPIO pin
#define pixelNumber 8 // Number of pixels on the LED led
Adafruit_NeoPixel led = Adafruit_NeoPixel(pixelNumber, neoPin, NEO_GRB + NEO_KHZ800);
volatile bool press = false; // Button control variable
static int c = 0; // Mode control variable
// Function prototypes
bool button();
void check();
void rainbowEffects();
void wiper();
void chaser();
bool button() {
if (digitalRead(buttonPin) == HIGH) {
press = true;
c++;
} else {
press = false;
}
return press;
}
void check() {
while (c == 0) {
chaser(led.Color(255, 255, 255), 50); // White chaser
}
while (c == 1) {
chaser(led.Color(255, 0, 0), 50); // Red chaser
}
while (c == 2) {
chaser(led.Color(0, 0, 255), 50); // Blue chaser
}
while (c == 3) {
chaser(led.Color(0, 200, 0), 50); // Green chaser
}
while (c == 4) {
wiper(led.Color(0, 0, 255), 50); // Blue wiper
}
while (c == 5) {
wiper(led.Color(0, 255, 0), 50); // Green wiper
}
while (c == 6) {
wiper(led.Color(255, 0, 0), 50); // Red wiper
}
while (c == 7) {
rainbowEffects(); // Rainbow effects
}
while (c == 8) {
led.clear();
chaser(led.Color(0, 0, 0), 50); // Off
}
}
void setup() {
// LED initialization
led.begin();
led.setBrightness(150);
led.show();
// Button pin initialization
pinMode(buttonPin, INPUT);
}
void loop() {
chaser(led.Color(255, 255, 255), 50); // White chaser
}
// Rainbow LED effects mode
void rainbowEffects() {
for (long color1 = 0; color1 < 3 * 65536; color1 += 256) {
for (int i = 0; i < led.numPixels(); i++) {
int currentColor = color1 + (i * 65536L / led.numPixels());
led.setPixelColor(i, led.gamma32(led.ColorHSV(currentColor)));
}
led.show(); // Update led strip
if (button)
check();
delay(10);
}
}
// Wiper mode
void wiper(uint32_t c, uint8_t d) {
for (uint16_t i = 0; i < led.numPixels(); i++) {
led.setPixelColor(i, c);
led.show();
if (button)
check();
delay(d);
}
}
// Chaser mode
void chaser(uint32_t c, uint8_t d) {
for (int j = 0; j < 10; j++) {
for (int k = 0; k < 3; k++) {
for (uint16_t i = 0; i < led.numPixels(); i = i + 3) {
led.setPixelColor(i + k, c);
}
led.show();
if (button)
check();
delay(d);
for (uint16_t i = 0; i < led.numPixels(); i = i + 3) {
led.setPixelColor(i + k, 0);
}
if (button)
check();
}
}
}