Rgb christmas tree like loop

So I've got this code but it doesn't do whats suppoused to do, it should work this way:
If I press a button (1 to 3) the rgb should loop until I press another button. if I pressed btn1 the loop for that button should start and until I press another button, lets say btn 2 the sequence 2 should interrupt the first and start, the 4th button should loop the sequence 1 to 3 until interrupted and the 5th button should make all the rgbs light down:

// Pines de los botones
const int btn_PIN1 = 15;
const int btn_PIN2 = 16;
const int btn_PIN3 = 17;
const int btn_PIN4 = 18;
const int btn_PIN5 = 19;

bool ejecutarSecuencia1 = false;
bool ejecutarSecuencia2 = false;
bool ejecutarSecuencia3 = false;
bool ejecutarSecuencia4 = false;

void setup() {
  // Declaramos los pines de los LED como salidas
  for(int i = 0; i < 10; i++) {
    pinMode(i, OUTPUT);
  }
  // Configuramos los pines de los botones como entradas
  pinMode(btn_PIN1, INPUT);
  pinMode(btn_PIN2, INPUT);
  pinMode(btn_PIN3, INPUT);
  pinMode(btn_PIN4, INPUT);
  pinMode(btn_PIN5, INPUT);
}

void loop() {
  // Chequear si el botón 1 está presionado
  if(digitalRead(btn_PIN1) == HIGH && !ejecutarSecuencia1) {
    ejecutarSecuencia1 = true;
  }
  // Chequear si el botón 2 está presionado
  if(digitalRead(btn_PIN2) == HIGH && !ejecutarSecuencia2) {
    ejecutarSecuencia2 = true;
  }
  // Chequear si el botón 3 está presionado
  if(digitalRead(btn_PIN3) == HIGH && !ejecutarSecuencia3) {
    ejecutarSecuencia3 = true;
  }
  // Chequear si el botón 4 está presionado
  if(digitalRead(btn_PIN4) == HIGH && !ejecutarSecuencia4) {
    ejecutarSecuencia4 = true;
  }
  // Chequear si el botón 5 está presionado
  if(digitalRead(btn_PIN5) == HIGH) {
    detenerRGB();
    ejecutarSecuencia1 = false;
    ejecutarSecuencia2 = false;
    ejecutarSecuencia3 = false;
    ejecutarSecuencia4 = false;
  }

  // Si alguna secuencia está activa, llamar a la función correspondiente
  if(ejecutarSecuencia1) {
    secuenciaRGB1();
  } else if(ejecutarSecuencia2) {
    secuenciaRGB2();
  } else if(ejecutarSecuencia3) {
    secuenciaRGB3();
  } else if(ejecutarSecuencia4) {
    secuenciaRGB4();
  }
}

void secuenciaRGB1() {
  // Secuencia de encendido y apagado de LEDs
  for(int i = 0; i < 10; i++) {
    digitalWrite(i, HIGH);
    delay(100);
    digitalWrite(i, LOW);
  }
  delay(500); 
}


void secuenciaRGB2() {
  // Encender por hileras
  for (int i = 0; i < 4; i++) { 
    digitalWrite(i, HIGH);
    delay(100); 
  }
  delay(500); 
  
  for (int i = 4; i < 7; i++) {
    digitalWrite(i, HIGH);
    delay(100); 
  }
  delay(500);
  
  for (int i = 7; i < 10; i++) {
    digitalWrite(i, HIGH);
    delay(100); 
  }
  delay(500);
  
  digitalWrite(1, HIGH); 
  delay(100); 

  // Apagar por hileras
  for (int i = 0; i < 4; i++) { 
    digitalWrite(i, LOW);
    delay(100);
  }
  for (int i = 4; i < 7; i++) {
    digitalWrite(i, LOW);
    delay(100);
  }
  for (int i = 7; i < 10; i++) { 
    digitalWrite(i, LOW);
    delay(100);
  }
  digitalWrite(1, LOW);
  delay(100);

  delay(500); 
}

void secuenciaRGB3(){
  // Enciende todos los LEDs
  for(int i = 0; i < 10; i++) {
    digitalWrite(i, HIGH);
  }
  delay(500);
}

void secuenciaRGB4() {
  // Reproducir secuencia 1
  secuenciaRGB1();
  // Reproducir secuencia 2
  secuenciaRGB2();
  // Reproducir secuencia 3
  secuenciaRGB3();
}

void detenerRGB() {
  // Apagar todos los LEDs
  for(int i = 0; i < 10; i++) {
    digitalWrite(i, LOW);
  }
}

This is the simulation

  • Do not use delay( ) in your sketches.
    It can block program code execution until the delay time has expired.

  • Is there a common connection to all the switches going to 5V ?

  • LEDs require a series dropping resistor to prevent damage to the Arduino GPIOs.

Hello @monloca - We would like to see you study the millis() timer... it will be used in many of your future projects. Here is a simulation of making many events happen at their own time. To add another event, you need only add to the arrays.

No I don't think there is a common connection, and about the resistor, yes I had it but idk why the rgbs died after like the first loop because of it

  • Your switches can be wired 3 different ways.

  • Think your’s is as S1 in the schematic below.

  • S3 is a better way of wiring a switch.

  • Your LEDs should have a series resistor as seen for D9 below.

Your buttons are wired through each other (and oddly tied together).

You wired two RGBLEDs through DIO 0 and DIO 1 which are reserved for Serial Communications (the USB cable).

Your button reading needs to detect only one press (and release). It now reads MANY presses with one button press.

Here is a small button reading program that calls functions (as your program does)...

And maybe read about interrupts...

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.