Trying to figure out relays, buttons, and timing

New to the wonderful world of Arduino here, and I have a few (probably stupid) questions.

The first problem is that when my Arduino boots up, it turns both Pump1 and Pump2 outputs on and keeps them on (I need them to be off); when I actuate button1, it runs the code correctly but proceeds to turn the relays back on until the button is actuated again.

Secondly, after reading through the forums for a bit, I've come to the conclusion that you guys aren't huge fans of the delay() function. What I need to do is have Pump1 run for a set time (2 seconds) and then have Pump2 run for a different amount of time (4 seconds). This portion of the code seems to be working just fine, but I was wondering if there was any way to do the same thing without using the delay() function. I looked at the blink without delay example, but I couldn't see how that could work for me.

Thanks in advance for any help.

const int Pump1 = 8;              // the number of pump 1 pin
const int Pump2 = 9;              // the number of pump 2 pin
const int button1 = 3;            // the number of button 1 pin

int button1State = 0;             // variable for reading the button status
 
void setup() {

  // initialize the pump pins as outputs:
  pinMode(Pump1, OUTPUT);
  pinMode(Pump2, OUTPUT);
  // initialize the button pin as an input:
  pinMode(button1, INPUT_PULLUP);

}


void loop() {
  // read the state of the button value:
  button1State = digitalRead(button1);

  // check if the button is pressed. If so, run both pumps
  if (button1State == LOW) {
    digitalWrite(Pump1, HIGH);    // turn on pump 1
    delay(2000);                  // wait 2 seconds
    digitalWrite(Pump1, LOW);     // turn off pump 1
    delay(50);                    // wait .05 seconds
    digitalWrite(Pump2, HIGH);    // turn on pump 2
    delay(4000);                  // wait 4 seconds
    digitalWrite(Pump2, LOW);     // turn off pump 2
    

  }
  else{
    digitalWrite(Pump1, LOW);
    digitalWrite(Pump2, LOW);
  }


}

looking at the posted code

  1. setup() initialises the pump control pins as outputs
  2. then in loop() if button1State is HIGH writes LOW to pump1 and 2
    digitalWrite(Pump1, LOW);    // turn off pump 1
    then are you sure that a LOW switches off a pump?
    could you gives us the circuit you are using?

ssykes08:
I've come to the conclusion that you guys aren't huge fans of the delay() function.

That's totally inaccurate. We LOVE the delay function. At least I do. It's a great well written function that does exactly what it says it does. All functions should be so nice.

It does block though, so it makes code unresponsive. There are lots of things you can't do with it. But that doesn't mean we're not fans. It just means that it doesn't work for lots of things. Not because we don't like it, just because it doesn't work for lots of things.

Where it works it works just perfectly. Check out the Blink example that we all start with. It uses delay.

ssykes08:
The first problem is that when my Arduino boots up, it turns both Pump1 and Pump2 outputs on and keeps them on (I need them to be off); when I actuate button1, it runs the code correctly but proceeds to turn the relays back on until the button is actuated again.

Do you know whether the Arduino control signal for your relay needs to be set HIGH or LOW to make the relay turn the pump on? Some relays need a HIGH for off.

You can always write the correct state to the relayPin in setup() immediately after setting the pinMode as OUTPUT.

If the pump is being turned on when you don't want it then the code is not correct.

...R