Continuing the discussion from LED strip sequence PWM add pushbutton change sequence:
Would this code be of any help?
/*
If you're using a single RGB LED (not a Neopixel) with an Arduino, here's how you can create a rainbow fade effect and trigger a simulated "electricity arc" effect by pressing a button. This setup uses PWM (Pulse Width Modulation) to control the brightness of each color channel.
Circuit Components:
- 1 RGB LED (common cathode or common anode)
- 3 resistors (e.g., 220Ω for each color pin)
- 1 button
- Arduino board (e.g., Uno)
- Breadboard and wires
Circuit Connection:
- Connect the cathode (or anode) pin of the RGB LED to ground (or +5V for common anode).
- Connect the red, green, and blue pins of the LED to digital PWM pins on the Arduino (e.g., pins 2, 3, and 4) through resistors.
- Connect one leg of the button to a digital pin (e.g., pin 5) and the other to ground.
- Enable the internal pull-up resistor for the button in the code.
Arduino Code:
*/
// Pin definitions
#define RED_PIN 2
#define GREEN_PIN 3
#define BLUE_PIN 4
#define BUTTON_PIN 5
bool arcEffect = false; // To track button press
void setup() {
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP); // Internal pull-up resistor
}
void loop() {
if (digitalRead(BUTTON_PIN) == LOW) { // Button pressed
arcEffect = true;
electricityArcEffect();
delay(100); // Debounce delay
} else {
arcEffect = false;
rainbowFade();
}
}
// Function for rainbow fade effect
void rainbowFade() {
for (int r = 0; r <= 255; r++) { // Red up
setColor(r, 0, 255 - r);
delay(5);
}
for (int g = 0; g <= 255; g++) { // Green up
setColor(255 - g, g, 0);
delay(5);
}
for (int b = 0; b <= 255; b++) { // Blue up
setColor(0, 255 - b, b);
delay(5);
}
}
// Function for electricity arc effect
void electricityArcEffect() {
for (int i = 0; i < 10; i++) { // Flash 10 times
setColor(random(100, 255), random(100, 255), random(100, 255));
delay(random(50, 150)); // Random flash duration
setColor(0, 0, 0); // LED off
delay(random(50, 150));
}
}
// Helper function to set the color of the RGB LED
void setColor(int red, int green, int blue) {
analogWrite(RED_PIN, red);
analogWrite(GREEN_PIN, green);
analogWrite(BLUE_PIN, blue);
}
//How It Works:
//- The rainbowFade() function smoothly transitions through red, green, and blue colors to create the rainbow fade effect.
//- When the button is pressed, the electricityArcEffect() simu