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 or fade 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
unsigned long buttonPressTime = 0; // Time when the button is pressed
const unsigned long holdTime = 2000; // 2 seconds hold time for fade mode
const int fadeDelay = 5; // Delay for smooth fade transitions
void setup()
{
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)
{
handleLongPress(); // Long press handling (switch to fade mode)
}
delay(50); // Debounce delay
}
// If fade mode is active, continuously run fade sequences
if (fadeMode)
{
cycleFadeSequences();
}
lastButtonState = buttonState;
}
// Function to handle short button press (less than hold time)
void handleShortPress()
{
if (!fadeMode)
{
// Solid color mode
if (ledOn)
{
cycleLEDs(); // Cycle through solid colors
}
else
{
ledOn = true;
mode = 1; // Start with Red
turnOnLED(mode);
}
}
else
{
// Fade sequence mode
cycleFadeSequences(); // Cycle through fade sequences
}
}
// Function to handle long button press (hold for holdTime)
void handleLongPress()
{
if (!fadeMode)
{
fadeMode = true; // Enter fade sequence mode
mode = 1; // Start with first fade sequence
}
else
{
fadeMode = false; // Exit fade mode to solid color mode
turnOffAllLEDs();
ledOn = false;
mode = 0; // Reset 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 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 cycle through 10 fade sequences
void cycleFadeSequences()
{
if (!fadeMode) return; // Exit if fade mode is off
mode++;
if (mode > 10)
{
mode = 1; // Wrap around to the first fade sequence after the last
}
runFadeSequence(mode);
// Small delay between each fade sequence (optional)
delay(500);
}
// Function to execute fade sequences based on the mode
void runFadeSequence(int mode)
{
switch (mode)
{
case 1: fadeBetweenColors(255, 0, 0, 0, 255, 0); Serial.println("Fade1");break; // Fade Red to Green
case 2: fadeBetweenColors(0, 255, 0, 0, 0, 255); Serial.println("Fade2"); break; // Fade Green to Blue
case 3: fadeBetweenColors(0, 0, 255, 255, 0, 0); Serial.println("Fade3"); break; // Fade Blue to Red
case 4: fadeBetweenColors(255, 0, 255, 255, 255, 0); Serial.println("Fade4"); break; // Fade Magenta to Yellow
case 5: fadeBetweenColors(0, 255, 255, 255, 0, 255); Serial.println("Fade5"); break; // Fade Cyan to Magenta
case 6: fadeBetweenColors(255, 255, 0, 0, 255, 255); Serial.println("Fade6"); break; // Fade Yellow to Cyan
case 7: fadeBetweenColors(255, 255, 255, 128, 0, 128); Serial.println("Fade7"); break; // Fade White to Purple
case 8: fadeBetweenColors(255, 128, 0, 0, 255, 128); Serial.println("Fade8"); break; // Fade Orange to Light Blue
case 9:
fadeBetweenColors(128, 0, 128, 0, 191, 255);
Serial.println("Fade9");
break; // Fade Purple to Light Blue
case 10:
fadeBetweenColors(0, 191, 255, 255, 0, 0);
Serial.println("Fade10");
break; // Fade Light Blue to Red
}
}
// 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
}
}
// Function to turn off all LEDs
void turnOffAllLEDs()
{
analogWrite(redPin, LOW);
analogWrite(greenPin, LOW);
analogWrite(bluePin, LOW);
}