Water pumps ignoring the code

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);
    }
  }
}

Welcome to the forum

Your topic was MOVED to its current forum category which is more appropriate than the original as it has nothing to do with Installation and Troubleshooting of the IDE

Do you have pull down resistors on your button inputs?

Yes i do:
One side of the button to 5V directly and the other one in the middle of the resistor connected to ground and the digital pin to the arduino (the three are connected in line)

RESISTOR - (ONE LEG TO VCC)
BUTTON
ARDUINO DIGITAL PIN

When sometimes is used several times in describing undesirable behaviour, it can point attention to you power arrangements.

Can you try your code without the motors or anything more challenging than lighting an LED?

Just put LEDs with series limiting resistors in the circuit instead of the motors. So you can see when the motor would have been running if it was in there.

Then tell us it works, doesn't work or sometimes works.

a7

It behaves strange. They are ocassions that the LED goes indefinitly, but they also stay on. I don't know what to think. I rearrange every connection and checked functionality individually and it is OK, but I don't find the problem in the code.

https://learn.sparkfun.com/tutorials/driving-motors-with-arduino/wiring-up-the-circuit

MotorControlCircuit_1

1 Like

Yes it's connected properly, just without the diode. But as far as I know it shouldn´t be necessary.

The diode is required. The transistor has probably already been destroyed.

See Flyback diodes and why you need them - #2 by PerryBebbington

1 Like

True! Checked with new transistor and diode, now it works!
It doesn´t appear it any of the tutorials I watched about DC motors, but you learn something new every day!

Thank you! Problem solved.

You can find arbitrary amounts of bad advice in random tutorials on the web. Do not assume that the poster has the slightest idea what they are doing!

1 Like

So... evidently you never did do a test without motors, just using LEDs instread.

Now you may see why I suggested trying that!

a7

Yes. And I think I assumed problem solved to fast. It works the first time, but never again. Then it works with a strange behaviour just like the LEDs.

I checked the diodes to see if they are correctly orientated and they are, so it seems that that wasn't the (atlest one of) the problem/s.

You may be burning out the transistor with overcurrent or overheating. Use a motor driver instead. Pololu has a good selection.

The startup current of a brushed DC motor is typically 5 to 10 times the free running current.

@danielito1668 can you indulge us and draw the circuit you are making with the switches and pull down resistors?

What value resistors are you using?

Maybe just hand draw the whole thing, everything and how it is all connected with attention to include power, from where it comes and to where yo are feeding it.

Your words sound right, but there is some ambiguity, just wanna make sure you aren't having some button issues.

a7

Okay, I´m gonna hand draw it because it´s easier from me. I will post when I finish.

I put your code into the wokwi simulator, see it here:

Wokwi_badge Simulation of the code


I changed it for pulled up switches, and only wired one switch and LED.

At a glance, this isn't a code problem, its a wires and circuitry problem.

I did see that

    // 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);
    }

will leave an LED on after the flashing. You could just put

      digitalWrite(led1, LOW);
      digitalWrite(led2, LOW);

after the for loop to turn off both.

I'll add (or you can) the other hardware later. Possibly much later and not if you've fixed your stuff there in the meantime. You close I think.

a7

Hello danielito1668

I´v made a small code review.

All below listed calls of the delay() function will block the execution of the sketch.
This code design will inhibit the sketch execution in realtime.

A time keeper derived from the BlinkWithOutDelay could be the base for a time handler.

What to do.
An option is to rearrange the sketch. Based on the IPO model, structure the desired function of the sketch into basic functions. Take a sheet of paper and a pencil and draw a programme structure to identify the required functions, e.g. button, timer and display functions. All these functions can be coded and tested separately. To complete the project, you also need to design a control structure that uses events to call the above functions. In this way keep in mind to design objects and related services thus handle input/output actions.
That is all that needs to be done. Try them out.

Have a nice day and enjoy coding in C++.

    Line  27:   delay(100);
	Line  31:     delay(1000); // Modifica el tiempo aqui, en milisegundos
	Line  36:     delay(1000); // Modifica el tiempo aqui, en milisegundos
	Line  43:       delay(250);
	Line  46:       delay(250);
	Line  52:       delay(250);
	Line  54:       delay(250);
	Line  59:     delay(1000); // Modifica el tiempo aqui, en milisegundos
	Line  64:     delay(1000); // Modifica el tiempo aqui, en milisegundos
	Line  71:       delay(250);
	Line  74:       delay(250);
	Line  80:       delay(250);
	Line  82:       delay(250);
	Line  87:     delay(5000); // Modifica el tiempo aquí, en milisegundos
	Line  92:     delay(5000); // Modifica el tiempo aquí, en milisegundos
	Line  98:       delay(250);
	Line 100:       delay(250);
	Line 105:     delay(5000); // Modifica el tiempo aquí, en milisegundos
	Line 110:     delay(5000); // Modifica el tiempo aquí, en milisegundos
	Line 116:       delay(250);
	Line 118:       delay(250);

I have made a schematic in Fritzing: https://imgur.com/H08iXmf

Nothing?