Getting rid of the delay in my code

Hi all!

I want to be able to stop this loop at any moment with the press of a button so I have decided I should get rid of the delay in my code. At this point, I really like how concise the code is so I am hoping for another relatively concise way to make all these delays into something like the “blink without delay” example.

One thing I don’t think the “blink without delay” explains is how my steps will stay sequential. How do I make sure they stay in order? The other thing is if I am trying to make this loop loop continuously, will I have to change my while loop?

I obviously left out lots of code but this is the relevant portion that I need to rewrite.

Any help is appreciated!!

    digitalWrite(BV7Pin, HIGH);     //Post Keg drain valve open
    delay(50*SECOND);
    digitalWrite(BV11Pin, HIGH);    //Pre Keg drain valve open
    delay(20*SECOND);
    digitalWrite(BV11Pin, LOW);     //Pre Keg drain valve close
    delay(4*SECOND);
    digitalWrite(BV1Pin, HIGH);     //hot water rinse on
    delay (12*SECOND);              //-----------------------------1st rinse
    digitalWrite(BV1Pin, LOW);      //hot water rinse off
    delay (4*SECOND);
    digitalWrite(BV2Pin, HIGH);      //CO2 on
    delay(15*SECOND);
    digitalWrite(BV2Pin, LOW);       //CO2 off
    delay(40*SECOND);
    digitalWrite(BV11Pin, HIGH);    //Pre Keg drain valve open
    delay(10*SECOND);                
    
    digitalWrite(BV11Pin, LOW);     //Pre Keg drain valve close
    delay(4*SECOND);
    digitalWrite(BV1Pin, HIGH);     //hot water rinse on
    delay (8*SECOND);               //-------------------------------2nd rinse
    digitalWrite(BV1Pin, LOW);      //hot water rinse off
    delay (4*SECOND);
    digitalWrite(BV2Pin, HIGH);      //CO2 on
    delay(12*SECOND);
    digitalWrite(BV2Pin, LOW);       //CO2 off
    delay(30*SECOND);
    digitalWrite(BV11Pin, HIGH);    //Pre Keg drain valve open
    delay(10*SECOND);            

    digitalWrite(BV11Pin, LOW);     //Pre Keg drain valve close
    delay(4*SECOND);
    digitalWrite(BV1Pin, HIGH);     //hot water rinse on
    delay (8*SECOND);               //-------------------------------3rd rinse
    digitalWrite(BV1Pin, LOW);       //hot water rinse off
    delay (4*SECOND);
    digitalWrite(BV2Pin, HIGH);       //CO2 on
    delay(12*SECOND);
    digitalWrite(BV2Pin, LOW);       //CO2 off
    delay(30*SECOND);
    digitalWrite(BV11Pin, HIGH);    //Pre Keg drain valve open
    delay(10*SECOND);  
    digitalWrite(BV11Pin, LOW);     //Pre Keg drain valve close
    digitalWrite(BV7Pin, LOW);      //Post Keg drain valve close
    delay (4*SECOND);
    
 if(sensorValue > PBWTempRef) {            //if temp is already above setpoint turn off heating element
      digitalWrite(ElementPin, LOW);
    }
      else {
    while (sensorValue < PBWTempRef) {         //if temp is below setpoint keep heating element on  
      sensorValue = analogRead(A7);
      if (sensorValue > PBWTempRef) { break;  }
      }
   }
    digitalWrite(ElementPin, LOW);    //turn off heating element again just in case it didn't work before
    digitalWrite(BV5Pin, HIGH);      //open PBW drain  
    digitalWrite(BV4Pin, HIGH);      //open PBW return valve
    delay(20*SECOND);
    digitalWrite(PumpPin, HIGH);    //turn on pump
    delay(60*SECOND);
    digitalWrite(PumpPin, LOW);     //turn off pump
    digitalWrite(BV5Pin, LOW);      //close PBW drain
    delay(4*SECOND);
    digitalWrite(BV2Pin, HIGH);     //CO2 on
    delay(30*SECOND);       
    digitalWrite(BV2Pin, LOW);      //CO2 off
    delay(110*SECOND);
    digitalWrite(BV11Pin, HIGH);    //Pre Keg drain valve open
    delay(10*SECOND);      
    digitalWrite(BV11Pin, LOW);     //Pre Keg drain valve close
    digitalWrite(BV4Pin, LOW);      //Keg return to PBW close
    digitalWrite(BV7Pin, HIGH);     //Post Keg drain valve open
    delay(3*SECOND);
    digitalWrite(BV1Pin, HIGH);     //hot water rinse on
    delay(8*SECOND);
    digitalWrite(BV1Pin, LOW);      //hot water off
    delay(4*SECOND);
    digitalWrite(BV2Pin, HIGH);     //CO2 on
    delay(14*SECOND);
    digitalWrite(BV2Pin, LOW);      //CO2 off
    delay(40*SECOND);
    digitalWrite(BV11Pin, HIGH);    //Pre Keg drain valve open
    delay(10*SECOND);
    digitalWrite(BV11Pin, LOW);     //Pre Keg drain valve close
    delay(3*SECOND);
    digitalWrite(BV1Pin, HIGH);     //hot water rinse on
    delay(7*SECOND);
    digitalWrite(BV1Pin, LOW);      //hot water off
    delay(4*SECOND);
    digitalWrite(BV2Pin, HIGH);     //CO2 on
    delay(12*SECOND);
    digitalWrite(BV2Pin, LOW);      //CO2 off
    delay(40*SECOND);
    digitalWrite(BV11Pin, HIGH);   //Pre Keg drain valve open
    delay(10*SECOND);
    digitalWrite(BV11Pin, LOW);    //Pre Keg drain valve close
    digitalWrite(BV7Pin, LOW);     //Post Keg drain close

Have you ever coded a Satate Machine sketch?
.

Robin2's sketch here on how to do more than one thing at a time may help you think that through.

Your sketch might also benefit from his planning a sketch thread here.

LarryD:
Have you ever coded a Satate Machine sketch?
.

....or even a State Machine :slight_smile:

JimboZA:
....or even a State Machine :slight_smile:

:cry:

.

Nope state machine is not in my vocabulary. Thanks for the tip. I'll look it up. If anyone else has broad suggestions like what tutorials to read that's cool with me - sometimes you don't even know what to search for!

This is the best state machine tutorial that I know of online: State Machine

Written by forum member Grumpy Mike.

Hello there,

The best method to get rid of a delay in code is probably to program an interrupt on input capture together with a debounce for your push button.

ard_newbie:
The best method to get rid of a delay in code is probably to program an interrupt on input capture together with a debounce for your push button.

No, that's the best way to get yourself hopelessly lost and confused. I would not recommend this method.

I went through this process in a project recently and there's some great advice in this thread - http://forum.arduino.cc/index.php?topic=382224.0

I ended up using a library called elapsedMillis() which was very helpful, but I recommend trying to code it yourself for the learning....