#include <Adafruit_NeoPixel.h>
#define LED_PIN 2
#define POT_PIN 4
#define BUTTON_PIN 0 // Boot button
#define NUM_LEDS 1
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, LED_PIN, NEO_GRB + NEO_KHZ800);
// Brightness variable (0-255)
int brightness = 5; // Default brightness
bool potentiometerActive = true; // Control logic flag
int currentColorIndex = 0; // Index for button-controlled color
unsigned long buttonPressStart = 0; // For detecting long presses
bool buttonHeld = false; // Tracks if the button is being held
// 15 Colors evenly divided
uint32_t colors[15] = {
strip.Color(255, 0, 0), // Red
strip.Color(255, 64, 0), // Red-Orange
strip.Color(255, 128, 0), // Orange
strip.Color(255, 255, 0), // Yellow
strip.Color(128, 255, 0), // Yellow-Green
strip.Color(0, 255, 0), // Green
strip.Color(0, 255, 128), // Green-Cyan
strip.Color(0, 255, 255), // Cyan
strip.Color(0, 128, 255), // Cyan-Blue
strip.Color(0, 0, 255), // Blue
strip.Color(128, 0, 255), // Blue-Purple
strip.Color(255, 0, 255), // Magenta
strip.Color(255, 0, 128), // Pink
strip.Color(255, 20, 147), // Deep Pink
strip.Color(255, 255, 255) // White
};
void setup() {
strip.begin();
strip.setBrightness(brightness); // Set initial brightness
strip.show();
pinMode(POT_PIN, INPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP); // Boot button with pull-up
Serial.begin(115200);
Serial.println("Enter brightness (0-255):");
}
void loop() {
// Check for serial input to adjust brightness
if (Serial.available()) {
String input = Serial.readStringUntil('\n'); // Read input as a string
int newBrightness = input.toInt(); // Convert input to an integer
// Validate and update brightness
if (newBrightness >= 0 && newBrightness <= 255) {
brightness = newBrightness;
strip.setBrightness(brightness);
strip.show();
Serial.print("Brightness updated to: ");
Serial.println(brightness);
} else {
Serial.println("Invalid input. Enter a number between 0 and 255.");
}
}
// Check the state of the button
if (digitalRead(BUTTON_PIN) == LOW) { // Button pressed
if (!buttonHeld) {
buttonPressStart = millis(); // Record the time the button was pressed
buttonHeld = true;
} else if (millis() - buttonPressStart >= 3000) { // Hold button for 3 seconds
// Button held for 3 seconds
potentiometerActive = true; // Reactivate potentiometer control
Serial.println("Potentiometer control reinstated.");
}
} else {
if (buttonHeld) {
if (millis() - buttonPressStart < 3000) { // Button was released before 3 seconds
potentiometerActive = false; // Deactivate potentiometer control
currentColorIndex = (currentColorIndex + 1) % 15; // Move to the next color
strip.setPixelColor(0, colors[currentColorIndex]);
strip.show();
Serial.print("Button pressed, showing color index: ");
Serial.println(currentColorIndex);
}
buttonHeld = false; // Reset button held state
}
}
// Potentiometer control logic
if (potentiometerActive) {
// Read the potentiometer value (0 to 4095)
int potValue = analogRead(POT_PIN);
// Map the potentiometer value to a range of 0 to 14 (15 colors)
int colorIndex = map(potValue, 0, 4095, 0, 14);
// Set the LED to the corresponding color
strip.setPixelColor(0, colors[colorIndex]);
strip.show();
delay(50); // Short delay for stability
} else {
delay(50); // Prevent rapid looping when potentiometer control is off
}
}