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