const int buttonPin = 2; // GPIO for Button
const int redPin = 9; // GPIO for Red LED
const int greenPin = 8; // GPIO for Green LED
const int bluePin = 7; // GPIO for Blue LED
int buttonState = 0; // Current state of the button
int lastButtonState = 0; // Previous state of the button
int mode = 0; // Mode to track which LED color, fade, or jump sequence is active
bool ledOn = false; // Track if RGB LED is ON or OFF
bool fadeMode = false; // Track if we are in fade sequence mode
bool jumpMode = false; // Track if we are in jump mode
unsigned long buttonPressTime = 0; // Time when the button is pressed
const unsigned long holdTime = 2000; // 2 seconds hold time for fade mode
const unsigned long jumpHoldTime = 3000; // 3 seconds hold time for jump mode
const int fadeDelay = 5; // Delay for smooth fade transitions
////////////////////////////////////////////////////////////////
void setup()
{
Serial.begin(115200);
pinMode(buttonPin, INPUT);
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
turnOffAllLEDs(); // Initially, turn off all LEDs
}
////////////////////////////////////////////////////////////////
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH && lastButtonState == LOW) {
buttonPressTime = millis(); // Record button press time
}
if (buttonState == LOW && lastButtonState == HIGH) {
unsigned long pressDuration = millis() - buttonPressTime;
if (pressDuration < holdTime) {
handleShortPress(); // Short press handling
} else if (pressDuration >= holdTime && pressDuration < jumpHoldTime) {
handleLongPress(); // Long press handling for fade mode
} else if (pressDuration >= jumpHoldTime) {
handleJumpPress(); // 3-second press handling for jump mode
}
delay(50); // Debounce delay
}
// Run the appropriate mode if active
if (fadeMode) {
runFadeSequence(mode);
} else if (jumpMode) {
runJumpMode(mode);
}
lastButtonState = buttonState;
}
////////////////////////////////////////////////////////////////
void handleShortPress() {
if (!fadeMode && !jumpMode) {
if (ledOn) {
cycleLEDs(); // Cycle through LED colors
} else {
ledOn = true;
mode = 1; // Start with the first LED mode
turnOnLED(mode); // Turn on the first LED mode
}
} else if (fadeMode) {
cycleFadeSequences(); // Cycle through fade sequences
} else if (jumpMode) {
cycleJumpModes(); // Cycle through jump modes only when in jump mode
}
}
////////////////////////////////////////////////////////////////
void handleLongPress() {
if (!fadeMode && !jumpMode) {
fadeMode = true;
mode = 1;
} else {
fadeMode = false;
turnOffAllLEDs();
ledOn = false;
mode = 0;
}
}
////////////////////////////////////////////////////////////////
// 3-second button press (enter/exit jump mode)
void handleJumpPress() {
if (!jumpMode) {
jumpMode = true;
fadeMode = false;
mode = 1;
Serial.println("Entering Jump Mode");
} else {
jumpMode = false;
turnOffAllLEDs();
ledOn = false;
mode = 0;
Serial.println("Exiting Jump Mode");
}
}
////////////////////////////////////////////////////////////////
// Function to cycle through solid colors (Red, Green, Blue, etc.)
void cycleLEDs() {
mode++;
if (mode > 10) {
mode = 1; // Wrap around to the first color after the last
}
turnOnLED(mode);
}
////////////////////////////////////////////////////////////////
// Function to cycle through fade sequences on button press
void cycleFadeSequences() {
mode++;
if (mode > 10) {
mode = 1; // Wrap around to the first fade sequence after the last
}
Serial.print("Switching to Fade Sequence: "); Serial.println(mode);
}
////////////////////////////////////////////////////////////////
void cycleJumpModes() {
if (jumpMode) {
mode++;
if (mode > 20) { // Wrap around to the first mode after the last
mode = 1;
}
Serial.print("Switching to Jump Mode: ");
Serial.println(mode);
}
}
////////////////////////////////////////////////////////////////
// Function to turn on the corresponding LED based on mode
void turnOnLED(int mode) {
turnOffAllLEDs(); // Turn off all LEDs before turning on the next one
switch (mode) {
case 1: analogWrite(redPin, 255); Serial.println("RED"); break;
case 2: analogWrite(greenPin, 255); Serial.println("GREEN"); break;
case 3: analogWrite(bluePin, 255); Serial.println("BLUE"); break;
case 4: analogWrite(redPin, 255); analogWrite(greenPin, 255); Serial.println("YELLOW"); break;
case 5: analogWrite(greenPin, 255); analogWrite(bluePin, 255); Serial.println("CYAN"); break;
case 6: analogWrite(redPin, 255); analogWrite(bluePin, 255); Serial.println("MAGENTA"); break;
case 7: analogWrite(redPin, 255); analogWrite(greenPin, 255); analogWrite(bluePin, 255); Serial.println("WHITE"); break;
case 8: analogWrite(redPin, 255); analogWrite(greenPin, 128); Serial.println("ORANGE"); break;
case 9: analogWrite(redPin, 128); analogWrite(bluePin, 128); Serial.println("PURPLE"); break;
case 10: analogWrite(greenPin, 191); analogWrite(bluePin, 255); Serial.println("LIGHT BLUE"); break;
}
}
////////////////////////////////////////////////////////////////
// Function to run a fade sequence indefinitely based on the mode
void runFadeSequence(int mode) {
switch (mode) {
case 1: fadeBetweenColors(255, 0, 0, 0, 255, 0); Serial.println("Fade1"); break;
case 2: fadeBetweenColors(0, 255, 0, 0, 0, 255); Serial.println("Fade2"); break;
case 3: fadeBetweenColors(0, 0, 255, 255, 0, 0); Serial.println("Fade3"); break;
case 4: fadeBetweenColors(255, 0, 255, 255, 255, 0); Serial.println("Fade4"); break;
case 5: fadeBetweenColors(0, 255, 255, 255, 0, 255); Serial.println("Fade5"); break;
case 6: fadeBetweenColors(255, 255, 0, 0, 255, 255); Serial.println("Fade6"); break;
case 7: fadeBetweenColors(255, 255, 255, 128, 0, 128); Serial.println("Fade7"); break;
case 8: fadeBetweenColors(255, 128, 0, 0, 255, 128); Serial.println("Fade8"); break;
case 9: fadeBetweenColors(128, 0, 128, 0, 191, 255); Serial.println("Fade9"); break;
case 10: fadeBetweenColors(0, 191, 255, 255, 0, 0); Serial.println("Fade10"); break;
}
// Check for button press after each fade cycle to change fade mode
if (digitalRead(buttonPin) == LOW) {
delay(50); // Debounce delay
while (digitalRead(buttonPin) == LOW); // Wait for button release
handleShortPress(); // Move to next fade sequence on button press
}
}
void runJumpMode(int mode) {
switch (mode) {
case 1: displayJumpPattern({{255, 0, 0}, {0, 255, 0}, {0, 0, 255}}); Serial.println("Jump1"); break; // Red, Green, Blue
case 2: displayJumpPattern({{255, 255, 0}, {0, 255, 255}, {255, 0, 255}}); Serial.println("Jump2"); break; // Yellow, Cyan, Magenta
case 3: displayJumpPattern({{255, 128, 0}, {128, 255, 128}, {128, 128, 255}}); Serial.println("Jump3"); break; // Orange, Light Green, Light Blue
case 4: displayJumpPattern({{255, 0, 255}, {0, 128, 255}, {255, 255, 128}}); Serial.println("Jump4"); break; // Magenta, Sky Blue, Light Yellow
case 5: displayJumpPattern({{128, 0, 128}, {0, 0, 255}, {0, 255, 0}}); Serial.println("Jump5"); break; // Purple, Blue, Green
case 6: displayJumpPattern({{0, 255, 255}, {255, 165, 0}, {255, 20, 147}}); Serial.println("Jump6"); break; // Cyan, Orange, Deep Pink
case 7: displayJumpPattern({{255, 192, 203}, {128, 0, 0}, {0, 128, 128}}); Serial.println("Jump7"); break; // Pink, Maroon, Teal
case 8: displayJumpPattern({{135, 206, 250}, {255, 255, 255}, {0, 0, 0}}); Serial.println("Jump8"); break; // Light Sky Blue, White, Black
case 9: displayJumpPattern({{255, 69, 0}, {0, 100, 0}, {255, 255, 224}}); Serial.println("Jump9"); break; // Red Orange, Dark Green, Light Yellow
case 10: displayJumpPattern({{128, 128, 0}, {0, 128, 255}, {255, 105, 180}}); Serial.println("Jump10"); break; // Olive, Light Blue, Hot Pink
case 11: displayJumpPattern({{0, 128, 0}, {255, 20, 147}, {255, 140, 0}}); Serial.println("Jump11"); break; // Green, Deep Pink, Dark Orange
case 12: displayJumpPattern({{75, 0, 130}, {240, 230, 140}, {255, 0, 0}}); Serial.println("Jump12"); break; // Indigo, Khaki, Red
case 13: displayJumpPattern({{30, 144, 255}, {173, 216, 230}, {147, 112, 219}}); Serial.println("Jump13"); break; // Dodger Blue, Light Blue, Medium Purple
case 14: displayJumpPattern({{255, 0, 255}, {255, 215, 0}, {128, 0, 128}}); break; Serial.println("Jump14"); // Magenta, Gold, Purple
case 15: displayJumpPattern({{240, 128, 128}, {224, 102, 255}, {255, 255, 0}}); Serial.println("Jump15"); break; // Light Coral, Violet, Yellow
case 16: displayJumpPattern({{100, 149, 237}, {255, 127, 80}, {0, 255, 255}}); Serial.println("Jump16"); break; // Cornflower Blue, Coral, Cyan
case 17: displayJumpPattern({{0, 0, 255}, {255, 20, 147}, {255, 250, 205}}); Serial.println("Jump17"); break; // Blue, Deep Pink, Lemon Chiffon
case 18: displayJumpPattern({{178, 34, 34}, {50, 205, 50}, {255, 69, 0}}); Serial.println("Jump18"); break; // Firebrick, Lime Green, Red Orange
case 19: displayJumpPattern({{65, 105, 225}, {240, 248, 255}, {255, 140, 0}}); Serial.println("Jump19"); break; // Royal Blue, Alice Blue, Dark Orange
case 20: displayJumpPattern({{255, 228, 181}, {139, 69, 19}, {128, 0, 128}}); Serial.println("Jump20"); break; // Misty Rose, Saddle Brown, Purple
}
// Check for button press to exit jump mode
if (digitalRead(buttonPin) == LOW) {
delay(50); // Debounce delay
while (digitalRead(buttonPin) == LOW); // Wait for button release
handleShortPress(); // Move to next fade sequence on button press
}
}
////////////////////////////////////////////////////////////////
// Function to fade smoothly between two colors
void fadeBetweenColors(int redStart, int greenStart, int blueStart, int redEnd, int greenEnd, int blueEnd) {
for (int i = 0; i <= 255; i++) {
int redValue = map(i, 0, 255, redStart, redEnd);
int greenValue = map(i, 0, 255, greenStart, greenEnd);
int blueValue = map(i, 0, 255, blueStart, blueEnd);
analogWrite(redPin, redValue);
analogWrite(greenPin, greenValue);
analogWrite(bluePin, blueValue);
delay(fadeDelay); // Control fade speed
}
}
////////////////////////////////////////////////////////////////
// Display a specific jump pattern
void displayJumpPattern(std::vector<std::array<int, 3>> colors) {
// Loop through each color in the jump pattern
for (int repeat = 0; repeat < 5; repeat++) { // Adjust 'repeat' for the number of cycles per mode
for (const auto& color : colors) {
analogWrite(redPin, color[0]);
analogWrite(greenPin, color[1]);
analogWrite(bluePin, color[2]);
delay(200); // Adjust delay for timing between colors in the pattern
}
}
}
////////////////////////////////////////////////////////////////
// Function to turn off all LEDs
void turnOffAllLEDs() {
analogWrite(redPin, LOW);
analogWrite(greenPin, LOW);
analogWrite(bluePin, LOW);
}
Loading
esp32-c3-devkitm-1
esp32-c3-devkitm-1