Using delay in programming is bad but cannot understand how to use millis

Hi All,

I have the below code that will run for short delays, a couple of minutes, but not the 20min-1 hr delays I want to be able to program in. I have read how bad delays are, and also how to write the millis() in but this just leaves me more confused. I have also read other posts, examples on delays/millis to no avail. As you will see from the below code I am wanting longer delays and when

When I run the program it just faults to the first line of code every 20 mins, pressurising the container, and does not run the complete cycle.

There is no issues with wiring, hardware etc as it all works with short delay. It is just the delay code that is making the program fault.

As discussed in other forums delays are not good as it offsets the timer etc and stuffs the program. What would be the best way to get the time delay I am after without using delay() code?

Many Thanks for your input and expertise Ladies and Gents.

 int pressure = 2;
int drain = 4;
int vacuum = 3;
int pump = 5;

void setup() {
  pinMode (pressure, OUTPUT); //   
  pinMode (drain, OUTPUT); //
  pinMode (vacuum, OUTPUT); //
  pinMode (pump, OUTPUT); //
}

void loop() {
  digitalWrite (pressure, HIGH) ;// open pressure solenoid to let pressure into the container
  delay (60000); // needs to stay open for for 1 min to get to target pressure~40 psi
  digitalWrite (pressure, LOW) ;// close pre4ssure solenoid
  delay (1200000) ;// hold the designated pressure for 20 mins in this case but up to an hour with all solenoids closed mantaining pressure
  digitalWrite (drain, HIGH) ;// open drain solenoid to release pressure and bring to atmospheric pressure
  delay (60000) ;// hold drain open for 1 minute to let pressure out
  digitalWrite (drain, LOW) ;// close drain solenoid
  digitalWrite (vacuum, HIGH) ;
  digitalWrite (pump, HIGH) ;// open vacuum pump solenoid and turn vacuum pump on
  delay (120000) ;// have solenoid and vacuum pump on for 2 mins to pull a vacuum in container
  digitalWrite (vacuum, LOW) ;
  digitalWrite (pump, LOW) ;// close vacuum solenoid and pump solenoid
  delay (1200000) ;// hold vacuum in container for 20 mins in this case but up to 1 hour
  digitalWrite (drain, HIGH) ;// open drain to let in atmospheric air to equalise pressure
  delay (60000) ;// hold drain open for 1 min to equalise pressure
  digitalWrite (drain, LOW) ;// close drain solenoid and repeat code from start
}

The tutorial Using millis() for timing. A beginners guide at the top of this forum should be helpful for you.

The first post in this forum is pin in place at the top of the screen for people like yourself.

Read it.

Here is a short sketch to illustrate one way of doing what you want. It uses an array of times, the millis() function (ala blink without delay) and a simple state machine. This was written to light 1 of 4 LEDs in sequence. Adapt to your use as necessary.

const unsigned long intervals[] = {5000, 10000, 3000, 10000}; // an array of times
const byte outputApin = 3;
const byte outputBpin = 4;
const byte outputCpin = 5;
const byte outputDpin = 6;

unsigned long timer = 0; // keep track of time
byte state = 0; // keep track of state

void setup()
{
   Serial.begin(115200);
   pinMode(outputApin, OUTPUT);
   pinMode(outputBpin, OUTPUT);
   pinMode(outputCpin, OUTPUT);
   pinMode(outputDpin, OUTPUT);
}

void loop()
{
  if(millis() - timer >= intervals[state])
  {
   timer = millis();
   state ++;
   if(state > 3)  // there are 4 states 0,1,2,3
   {
      state = 0;  // reset state to start over
   }
  }
  switch(state)
  {
   case 0:
   digitalWrite(outputApin, LOW);
   digitalWrite(outputBpin, HIGH);
   digitalWrite(outputCpin, HIGH);
   digitalWrite(outputDpin, HIGH);   
   break;
   case 1:
   digitalWrite(outputApin, HIGH);
   digitalWrite(outputBpin, LOW);
   digitalWrite(outputCpin, HIGH);
   digitalWrite(outputDpin, HIGH);
   break;
   case 2:
   digitalWrite(outputApin, HIGH);
   digitalWrite(outputBpin, HIGH);
   digitalWrite(outputCpin, LOW);
   digitalWrite(outputDpin, HIGH);
   break;
   case 3:
   digitalWrite(outputApin, HIGH);
   digitalWrite(outputBpin, HIGH);
   digitalWrite(outputCpin, HIGH);
   digitalWrite(outputDpin, LOW);
   break;
  }
}