Hi, I'm making a proyect that haves 4 buttons, 2 led, 1 buzzer and 2 water pumps.
The behaviour should be that when a button is pressed, one water pump will work for 1 second, then the other one will go for another second and then stop, the leds will flash and the buzzer will make a note. But when I upload the code and give it a run pressing a button, it works strange. Sometimes the water pump will start and never stop and sometimes the pump 1 will work for the accurate time but the second pump will never stop.
This is the code, I think it´s all connected properly and it´s just a problem with the code.
The two water pumps are connected to the arduino by a PN2222 and they are are supplied by a external 5V power supply (water pumps are rated to 3.3 to 6V)
Thanks for the help!
// Define los pines a los que estan conectados los componentes
const int bomba1 = 10;
const int bomba2 = 7;
const int boton1 = 2;
const int boton2 = 3;
const int boton3 = 4;
const int boton4 = 5;
const int led1 = 6;
const int led2 = 9;
const int buzzer = 11;
void setup() {
// Configura los pines como entradas o salidas
pinMode(bomba1, OUTPUT);
pinMode(bomba2, OUTPUT);
pinMode(boton1, INPUT);
pinMode(boton2, INPUT);
pinMode(boton3, INPUT);
pinMode(boton4, INPUT);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(buzzer, OUTPUT);
}
void loop() {
// Comprueba si se ha pulsado algun boton
delay(100);
if (digitalRead(boton1) == HIGH) {
// Enciende la bomba 1 durante 5 segundos
digitalWrite(bomba1, HIGH);
delay(1000); // Modifica el tiempo aqui, en milisegundos
digitalWrite(bomba1, LOW);
// Enciende la bomba 2 durante 5 segundos
digitalWrite(bomba2, HIGH);
delay(1000); // Modifica el tiempo aqui, en milisegundos
digitalWrite(bomba2, LOW);
// Parpadea los leds durante 5 segundos
for (int i = 0; i < 10; i++) {
digitalWrite(led1, HIGH);
digitalWrite(led2, LOW);
delay(250);
digitalWrite(led1, LOW);
digitalWrite(led2, HIGH);
delay(250);
}
// Activa el buzzer durante 3 segundos
for (int i = 0; i < 6; i++) {
digitalWrite(buzzer, HIGH);
delay(250);
digitalWrite(buzzer, LOW);
delay(250);
}
} else if (digitalRead(boton2) == HIGH) {
// Enciende la bomba 2 durante 5 segundos
digitalWrite(bomba2, HIGH);
delay(1000); // Modifica el tiempo aqui, en milisegundos
digitalWrite(bomba2, LOW);
// Enciende la bomba 1 durante 5 segundos
digitalWrite(bomba1, HIGH);
delay(1000); // Modifica el tiempo aqui, en milisegundos
digitalWrite(bomba1, LOW);
// Parpadea los leds durante 5 segundos
for (int i = 0; i < 10; i++) {
digitalWrite(led1, HIGH);
digitalWrite(led2, LOW);
delay(250);
digitalWrite(led1, LOW);
digitalWrite(led2, HIGH);
delay(250);
}
// Activa el buzzer durante 3 segundos
for (int i = 0; i < 6; i++) {
digitalWrite(buzzer, HIGH);
delay(250);
digitalWrite(buzzer, LOW);
delay(250);
}
} else if (digitalRead(boton3) == HIGH) {
// Enciende la bomba 1 durante 5 segundos
digitalWrite(bomba1, HIGH);
delay(5000); // Modifica el tiempo aquí, en milisegundos
digitalWrite(bomba1, LOW);
// Enciende el led 1 durante 5 segundos
digitalWrite(led1, HIGH);
delay(5000); // Modifica el tiempo aquí, en milisegundos
digitalWrite(led1, LOW);
// Activa el buzzer durante 3 segundos
for (int i = 0; i < 6; i++) {
digitalWrite(buzzer, HIGH);
delay(250);
digitalWrite(buzzer, LOW);
delay(250);
}
} else if (digitalRead(boton4) == HIGH) {
// Enciende la bomba 2 durante 5 segundos
digitalWrite(bomba2, HIGH);
delay(5000); // Modifica el tiempo aquí, en milisegundos
digitalWrite(bomba2, LOW);
// Enciende el led 2 durante 5 segundos
digitalWrite(led2, HIGH);
delay(5000); // Modifica el tiempo aquí, en milisegundos
digitalWrite(led2, LOW);
// Activa el buzzer durante 3 segundos
for (int i = 0; i < 6; i++) {
digitalWrite(buzzer, HIGH);
delay(250);
digitalWrite(buzzer, LOW);
delay(250);
}
}
}