Run a 12V water pump for 10 or 20 seconds after pressing a button

It's missing these guys { }

void loop() {
    if (digitalRead(ButtonPin1) == LOW)  {// If button1 to Ground is closed
        digitalWrite(RelayPin, HIGH);  // Turn on relay
        delay(13000);  // Wait 13 seconds
        digitalWrite(RelayPin, LOW);   // Tun off relay
    }


        if (digitalRead(ButtonPin2) == LOW)  {// If button2 to Ground is closed
        digitalWrite(RelayPin, HIGH);  // Turn on relay
        delay(20000);  // Wait 20 seconds
        digitalWrite(RelayPin, LOW);   // Tun off relay
   }
}

omg that was the mistake? Thank you so much kmin!
I'm just a hardware guy and the programming thing is kind of confusing if you don't do it that often.

now this "two button code" is working fine!
Thanks kmin !

No problem,
You have this twice on your setup, remove the last one.

done, thank you!

"the working code"

const int ButtonPin1 = 2;
const int ButtonPin2 = 4;
const int RelayPin = 3;

void setup() {
    pinMode(ButtonPin1, INPUT_PULLUP);

    digitalWrite(RelayPin, LOW);
    pinMode(RelayPin, OUTPUT);

    pinMode(ButtonPin2, INPUT_PULLUP);

    digitalWrite(RelayPin, LOW);
}

void loop() {
    if (digitalRead(ButtonPin1) == LOW)  {// If button1 to Ground is closed
        digitalWrite(RelayPin, HIGH);  // Turn on relay
        delay(13000);  // Wait 13 seconds
        digitalWrite(RelayPin, LOW);   // Tun off relay
    }


        if (digitalRead(ButtonPin2) == LOW)  {// If button2 to Ground is closed
        digitalWrite(RelayPin, HIGH);  // Turn on relay
        delay(20000);  // Wait 20 seconds
        digitalWrite(RelayPin, LOW);   // Tun off relay
   }
}

... and any code will benefit from being properly formatted. Your original mistake may have leapt off the screen in its obviousity. Yes that is too a word. :expressionless:

Until you have formatting living i your elbows and naturally just write formatted code, avail yourself of the IDE Tool called Autoformat.

It will put your code into one of the several popular formatting styles you will see in well-written code.

a7

Help, there's a problem!

@kmin may you can help here one more time?

It all looked so good, but unfortunately there is a problem and I don't know if it's the code or not. Maybe you can rule it out?

What should be:
If button1 is connected to ground then the pump runs for 13 seconds.
If button2 is connected to ground then the pump runs for 20 seconds.

But what happens:
If button1 is connected to ground then the pump runs for 13 seconds.
If button1 is connected to ground, the pump runs for 13 seconds + 13 seconds.
If button1 is connected to Ground, the pump runs for 13 seconds + 20 seconds.

It just happens irregularly

Like this too:
If button2 is connected to Ground, the pump runs for 20 seconds.
If button2 is connected to ground, the pump runs for 13 seconds.
If button2 is connected to ground, the pump runs for 20 seconds + 20 seconds.
If button2 is connected to Ground, the pump runs for 20 seconds + 20 seconds + 20 seconds.

is this a hardware problem? could it be the code???
Could it have something to do with the fact that both switches switch the same output?
I have no idea

PS: @alto777 - yes, I don't know anything about this automatic code formatting, thank you very much for the tip. I'll take a look at it. I'm learning a lot from you here right now, I'm really super grateful.

You can have as many input for one output as you want.
How is your "button" precisely? Momentary push button or something else?
Are the button wires running all over together with the pump wiring?

Leaving aside that obvious contradiction, I can't see your schematic anywhere?

The buttons are “normally open” (Momentary push button)

Your image presents that you are powering nano at 5V to VIN?
Minimum Vin voltage is ~6.5V, you need to power nano on 5V pin.

Also, is the wiring clean like that in practice? Or some spaghetti style setup?
How long is button wiring?

Ok, thank you very much!
I have swapped from VIN to 5V.

The cables of the switches are 20cm long
It's a bit "spaghetti style" but all connections are screwed or soldered. There should be no problems.

I also tested something:
a code with two outputs for the relay but unfortunately the error remains the same.

const int ButtonPin1 = 2;
const int ButtonPin2 = 4;
const int RelayPin1 = 3;
const int RelayPin2 = 5;

void setup() {
  pinMode(ButtonPin1, INPUT_PULLUP);
  digitalWrite(RelayPin1, LOW);
  pinMode(RelayPin1, OUTPUT);

  pinMode(ButtonPin2, INPUT_PULLUP);
  digitalWrite(RelayPin2, LOW);
  pinMode(RelayPin2, OUTPUT);
}

void loop() {
  if (digitalRead(ButtonPin1) == LOW) {  // If button1 to Ground is closed
    digitalWrite(RelayPin1, HIGH);       // Turn on relay1
    delay(13000);                        // Wait 13 seconds
    digitalWrite(RelayPin1, LOW);        // Tun off relay1
  }


  if (digitalRead(ButtonPin2) == LOW) {  // If button2 to Ground is closed
    digitalWrite(RelayPin2, HIGH);       // Turn on relay2
    delay(20000);                        // Wait 20 seconds
    digitalWrite(RelayPin2, LOW);        // Tun off relay2
  }
}

I have now tested both at least 10 times in succession. 13 seconds and 20 seconds one after the other but also mixed up. now it is running just as it should. i have no idea where the problem was or is.

but before that there was another 20+20 second run. Then I restarted everything, which it had actually done by switching from VIN to 5V.

The “new” code with the two outputs to the relay seems to work.

It may not make sense with the two outputs, but that's how it's wired now:

OK,

There are still 13sec + 13sec cycles when switch1 is pressed once.

:sob:

Surely it can't be that difficult?

But I also have the feeling that the chemotherapy has made me totally stupid and I no longer see deeply enough into all the things, such as that it must not go below 5v on Vin.... why didn't I see that?

Two relay outputs is completely no-sense.

May I suggest you to wire your setup like this:
-5V/GND from buck directly to relay and directly to Nano.
-From nano, button wiring the way it doesn't run with other wires
-If you still have problems, add external 470R - 1K pull-up resistors for buttons
-Add flyback diode across pump terminals.

One trick that could help if you are experiencing interferences from the pump stopping: Add small delay after turning pump off:

digitalWrite(RelayPin, LOW);        // Tun off relay1
delay(1000);
  }

First I changed the code back to one relay output.

This also worked with a short test (13sec and 20sec).

const int ButtonPin1 = 2;
const int ButtonPin2 = 4;
const int RelayPin = 3;

void setup() {
  pinMode(ButtonPin1, INPUT_PULLUP);
  digitalWrite(RelayPin, LOW);
  pinMode(RelayPin, OUTPUT);

  pinMode(ButtonPin2, INPUT_PULLUP);
  digitalWrite(RelayPin, LOW);
}

void loop() {
  if (digitalRead(ButtonPin1) == LOW) {  // If button1 to Ground is closed
    digitalWrite(RelayPin, HIGH);       // Turn on relay
    delay(13000);                        // Wait 13 seconds
    digitalWrite(RelayPin, LOW);        // Tun off relay
  }


  if (digitalRead(ButtonPin2) == LOW) {  // If button2 to Ground is closed
    digitalWrite(RelayPin, HIGH);       // Turn on relay
    delay(20000);                        // Wait 20 seconds
    digitalWrite(RelayPin, LOW);        // Tun off relay
  }
}

You think I should change it like this?

Please give me a few minutes, I have to find the resistors first and then solder them in.

I didn't find the rectifier diodes so quickly, but I can try that too. thanks for your great help at this point.

With the delay after switching off, it should look like this?
It works in any case.
If that's the solution, then I really admire your idea!

const int ButtonPin1 = 2;
const int ButtonPin2 = 4;
const int RelayPin = 3;

void setup() {
  pinMode(ButtonPin1, INPUT_PULLUP);
  digitalWrite(RelayPin, LOW);
  pinMode(RelayPin, OUTPUT);

  pinMode(ButtonPin2, INPUT_PULLUP);
  digitalWrite(RelayPin, LOW);
}

void loop() {
  if (digitalRead(ButtonPin1) == LOW) {  // If button1 to Ground is closed
    digitalWrite(RelayPin, HIGH);       // Turn on relay
    delay(13000);                        // Wait 13 seconds
    digitalWrite(RelayPin, LOW);        // Tun off relay
    delay(1000);
  }


  if (digitalRead(ButtonPin2) == LOW) {  // If button2 to Ground is closed
    digitalWrite(RelayPin, HIGH);       // Turn on relay
    delay(20000);                        // Wait 20 seconds
    digitalWrite(RelayPin, LOW);        // Tun off relay
    delay(1000);
  }
}