Help with RGB led code for academic fair

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

This code is a low curent arc and rainbow fade.The otherone is a high curent arc and rainbow fade.

// auther is donald178
// Pin definitions
#define RED_PIN 2
#define GREEN_PIN 3
#define BLUE_PIN 4
#define BUTTON_PIN 5

void setup() {
  pinMode(RED_PIN, OUTPUT);
  pinMode(GREEN_PIN, OUTPUT);
  pinMode(BLUE_PIN, OUTPUT);
  pinMode(BUTTON_PIN, INPUT_PULLUP); 
}

void loop() {
  if (digitalRead(BUTTON_PIN) == LOW) { 
    electricityArcEffect(); 
    delay(100); 
  } else {
    rainbowFade();
  }
}

// Rainbow fade effect (optional)
void rainbowFade() {
  for (int i = 0; i < 255; i++) {
    setColor(i / 3, 0, (255 - i) / 3); // Lower brightness in rainbow effect
    delay(8);
  }
  for (int i = 0; i < 255; i++) {
    setColor((255 - i) / 3, i / 3, 0);
    delay(8);
  }
}

// Low-energy electricity arc effect (soft glow)
void electricityArcEffect() {
  for (int i = 0; i < 15; i++) { // Fewer flashes
    int b = random(50, 120);  // Soft blue glow
    int w = random(20, 80);   // Low-intensity white

    setColor(w / 3, w / 3, b); // Dim glow effect
    delay(random(40, 100)); // Longer flash duration
    setColor(0, 0, 0);  // Off between flashes
    delay(random(30, 70));
  }
}

// Function to set LED color
void setColor(int red, int green, int blue) {
  analogWrite(RED_PIN, red);
  analogWrite(GREEN_PIN, green);
  analogWrite(BLUE_PIN, blue);

Post #1 does not compile... and when it is fixed to compile, the "electric effect" is only entered when the button is held. Usually, button presses are momentary. Did you credit the author?

Post #2 does not compile... and when it is fixed to compile, the "electric effect" is only entered when the button is held. The "rainbow effect" only cycles greenish to off.

Yes,I made these codes but the arc effect was made not to hold the effect until button is pressed. I am still working on these codes to run arc effect over and over again until button is pressed then to run rainbow effect over and over again and so on so forth.

this code has 3 settings
MODE 0 = off
MODE 1 = electric arc effect
MODE 2 = rainbow fade effect

// Pin definitions
#define RED_PIN 2
#define GREEN_PIN 3
#define BLUE_PIN 4
#define BUTTON_PIN 5

int mode = 0;                // Tracks the current mode (0 = Off, 1 = Arc, 2 = Rainbow)
bool lastButtonState = HIGH; // To detect button press changes

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() {
  // Check button press
  bool currentButtonState = digitalRead(BUTTON_PIN);
  if (currentButtonState == LOW && lastButtonState == HIGH) { // Detect falling edge (button press)
    mode = (mode + 1) % 3;  // Cycle through modes (0, 1, 2)
    delay(50);              // Debounce delay
  }
  lastButtonState = currentButtonState;

  // Run the selected mode
  switch (mode) {
    case 0:
      turnOffLEDs();        // Turn off LEDs
      break;
    case 1:
      electricityArcEffect(); // Run arc effect
      break;
    case 2:
      rainbowFade();        // Run rainbow fade effect
      break;
  }
}

// Function to turn off all LEDs
void turnOffLEDs() {
  setColor(0, 0, 0); // LEDs off
}

// Rainbow fade effect
void rainbowFade() {
  for (int i = 0; i < 255; i++) {
    setColor(i / 3, 0, (255 - i) / 3); // Lower brightness for subtle effect
    delay(8);
  }
  for (int i = 0; i < 255; i++) {
    setColor((255 - i) / 3, i / 3, 0);
    delay(8);
  }
}

// Electricity arc effect (blue and white)
void electricityArcEffect() {
  for (int i = 0; i < 15; i++) { // Fewer flashes for low energy
    int b = random(50, 120);  // Soft blue glow
    int w = random(20, 80);   // Low-intensity white

    setColor(w / 3, w / 3, b); // Dim glow effect
    delay(random(40, 100));   // Longer flash duration
    setColor(0, 0, 0);         // Off between flashes
    delay(random(30, 70));
  }
}

// Helper function to set LED color
void setColor(int red, int green, int blue) {
  analogWrite(RED_PIN, red);
  analogWrite(GREEN_PIN, green);
  analogWrite(BLUE_PIN, blue);
}

// Pin definitions
#define RED_PIN 9
#define GREEN_PIN 10
#define BLUE_PIN 11
#define BUTTON_PIN 2

int mode = 0;                  // Tracks the current mode (0 = Off, 1 = Arc, 2 = Rainbow, 3 = Random)
bool lastButtonState = HIGH;   // To detect button press changes

unsigned long lastSwitchTime = 0; // Stores the last time an effect was switched
unsigned long switchInterval = 0; // Interval for switching effects in case 3

bool randomEffectState = false; // Tracks which effect is currently active in random mode

void setup() {
  pinMode(RED_PIN, OUTPUT);
  pinMode(GREEN_PIN, OUTPUT);
  pinMode(BLUE_PIN, OUTPUT);
  pinMode(BUTTON_PIN, INPUT_PULLUP); // Internal pull-up resistor
  randomSeed(analogRead(A0));        // Seed random generator for better randomness
}

void loop() {
  // Check button press
  bool currentButtonState = digitalRead(BUTTON_PIN);
  if (currentButtonState == LOW && lastButtonState == HIGH) { // Detect falling edge (button press)
    mode = (mode + 1) % 4;  // Cycle through modes (0, 1, 2, 3)
    delay(50);              // Debounce delay
  }
  lastButtonState = currentButtonState;

  // Run the selected mode
  switch (mode) {
    case 0:
      turnOffLEDs();        // Turn off LEDs
      break;
    case 1:
      electricityArcEffect(); // Run arc effect
      break;
    case 2:
      rainbowFade();        // Run rainbow fade effect
      break;
    case 3:
      randomEffectWithTimer(); // Randomly alternate between effects every 2-3 minutes
      break;
  }
}

// Function to turn off all LEDs
void turnOffLEDs() {
  setColor(0, 0, 0); // LEDs off
}

// Rainbow fade effect
void rainbowFade() {
  for (int i = 0; i < 255; i++) {
    setColor(i / 3, 0, (255 - i) / 3); // Lower brightness for subtle effect
    delay(8);
  }
  for (int i = 0; i < 255; i++) {
    setColor((255 - i) / 3, i / 3, 0);
    delay(8);
  }
}

// Electricity arc effect (blue and white)
void electricityArcEffect() {
  for (int i = 0; i < 15; i++) { // Fewer flashes for low energy
    int b = random(50, 120);  // Soft blue glow
    int w = random(20, 80);   // Low-intensity white

    setColor(w / 3, w / 3, b); // Dim glow effect
    delay(random(40, 100));   // Longer flash duration
    setColor(0, 0, 0);         // Off between flashes
    delay(random(30, 70));
  }
}

// Random effect with timer
void randomEffectWithTimer() {
  unsigned long currentTime = millis(); // Get the current time

  // Check if it's time to switch effects
  if (currentTime - lastSwitchTime > switchInterval) {
    randomEffectState = !randomEffectState; // Toggle between arc and rainbow
    lastSwitchTime = currentTime;          // Reset the timer
    switchInterval = random(120000, 180000); // Random interval between 2-3 minutes
  }

  // Run the current effect
  if (randomEffectState) {
    electricityArcEffect();
  } else {
    rainbowFade();
  }
}

// Helper function to set LED color
void setColor(int red, int green, int blue) {
  analogWrite(RED_PIN, red);
  analogWrite(GREEN_PIN, green);
  analogWrite(BLUE_PIN, blue);
}

And?

It compiles for me. That's as far as my mind reading goes.

arduino-cli compile -b arduino:avr:uno --warnings all --output-dir ~/tmp --no-color (in directory: /home/me/Documents/sketchbook/Uno_R3/test)
already nofloat
Sketch uses 2700 bytes (8%) of program storage space. Maximum is 32256 bytes.
Global variables use 26 bytes (1%) of dynamic memory, leaving 2022 bytes for local variables. Maximum is 2048 bytes.
Compilation finished successfully.

There are 5 steps to solve your problem.
1.
2.
3.
4.
5.
Those answers should solve your questions.

Sometimes when it's in mode 3 randomEffectWithTimer(), and you press the button to go to mode 0, all off, it skips mode 0 and ends up in mode 1.

a7

I have to hold the button for a second or two before it goes to the next effect. And I need some help that so when I press it goes to the next effect without having to hold it for more than a second.

Do you have a theory about why that is the case?

no, I am looking at my right now to see what it would be.

void loop() {
  // Check button press
  bool currentButtonState = digitalRead(BUTTON_PIN);
  if (currentButtonState == LOW && lastButtonState == HIGH) { // Detect falling edge (button press)
    mode = (mode + 1) % 4;  // Cycle through modes (0, 1, 2, 3)
    delay(50);              // Debounce delay
  }

would it be in here?
note I am just abeginer at this stuff.

what are they?

Although you use of delay() there will have to go, that's not the immediate issue.
Think about how often you check your button in each of your effect loops...

A 50 millisecond delay in a debouncing algorithm is entirely tolerable.

The code that delay currently finds itself in, however, is inadequate.

a7

Let's just take this function as an example.

Each loop executes 255 times.

Each iteration of the loop includes an 8 ms delay.

8 x 255 x 2 = 4080 ms = 4.08 seconds

When your code is running rainbowFade, it won't look at your button for up to 4.08 seconds.

Your code in electricityArcEffect will have a similar long delay before looking at your button.

Now I understand it now and why that happens.

I wrote that to you two days ago: