Turning on a valve for 5 sec and wait for 5 sec before turning it on again

Hello,

I want a valve to be open if certain condition is satisfied for 5 sec and then close it , wait for 5 sec before opening it again for 5 sec.

dpoornima:
Hello,

I want a valve to be open if certain condition is satisfied for 5 sec and then close it , wait for 5 sec before opening it again for 5 sec.

With this code, am able to open the valve for 5 sec but am not able to close it for 5 sec before opening it again

void loop()

const long interval = 5000;
  static unsigned long int startTime = millis();
  static unsigned long int offtime=0;

if (solstate==false && Pdiff >= 20)
      {
        digitalWrite(4, HIGH);
        startTime = millis();  //saves the time it is open
        solstate = true;
      }
    }
    //turns it on for 5 sec
    if (solstate == true && millis() - startTime >= interval)
    {
      digitalWrite(4, LOW);
      offtime += interval;  // ? turn it off for 5 sec ?
      solstate = false;
    }

Are you sure the sketch will compile? :wink:

Are you sure the sketch will compile? :wink:

This is just the part of the sketch am trying to keep the solenoid open for 5 sec and wait for 5 sec.
It complies and even opens for 5 sec.

dpoornima:
This is just the part of the sketch am trying to keep the solenoid open for 5 sec and wait for 5 sec.
It complies and even opens for 5 sec.

Don't tease us. Post the complete program.

You have only shown the code for the first part and it seems to be going in the right direction ...

Have a look at how millis() is used to manage timing in Several things at a time

...R

The code in Reply #4 is still not complete.

...R

I'm confused. You have this piece of code

    if (solstate == true && millis() - startTime >= interval)
    {
      digitalWrite(4, LOW);
      offtime = + interval;
      solstate = false;
    }

but you don't seem to be making use of offtime

...R

but you don't seem to be making use of offtime

I am confused as to how to use the off time.
Can you please help me figure it out.

Hello,

I figured it out!
Thanks for all the insights!