Akting a motor and a relais simultaniously

Hi people,

I'm working on a arduino-based espresso-machine.

For this I use a DC-Motor (with a H-bridge) and a solenoid valve (controlled by a relay).

the motor should turn up, open the valve while turning up, close the valve and than turn down.

everything works fine, buuut I'd like that the valve turns on while the motor turns in one direction. I know this doesn't work with delay and unfortunately I programmed with delay ...

I read that it's possible to change and to work like in the "blink without led"-example.

Unfortunately it's hard to understand and I really don't know how to continue ...

any help?

here is the actual code:

// pins
int dirPin = 4;
int pwmPin = 10;
int pushButton = 2;
int pressed = digitalRead(pushButton);

// motor up
int powerOnH = 240; // speed of the motor
int durationMovementH = 4000; // time of the rotation
int directionMovementH = HIGH; //direction

// motor down
int powerOnR= 240; // speed of the motor
int durationMovementR = 3785; // ime of the rotation
int directionMovementR = LOW; //direction

int powerOff = 0;

void setup(){
  // pin modes
void loop(){

  // push the button

  int pressed = digitalRead(pushButton);
  if (pressed == HIGH)
    // up

    // start

    // time

    // stop



    // start

    // time

    // stop

Moderator edit: CODE TAGS!

If you want to minimize changes you could split the long delays into several smaller delays.

For example if you wanted to have the valve open for 2 second starting half a second after the motor starts down:

    digitalWrite(dirPin,directionMovementR);  // Motor direction down
    analogWrite(pwmPin,powerOnR);  // Motor start

    delay(500);  // Run motor for 1/2 second before opening valve

    digitalWrite(6,HIGH);  // Valve open
    delay(2000);  // For two seconds
    digitalWrite(6,LOW);  // and then closed

    delay(durationMovementR - 2500);  // The rest of the motor down time
    analogWrite(pwmPin,powerOff); // Stop the motor