pause, resume, restart

Hello All,

I am trying to create a process driven project in which the arduino will open solenoid valves for a certain time (delay) and then close solenoids and then open the next solenoid until completion. I would like to have the capability to pause, then resume this "process" or restart the process alltogether at any time during the process. I have tried to read through the interrupts and loop profiles to find the appropriate ways to handle this...as a novice to arduino not sure which methods would be the best fit as I need something to constantly look for the "pause" or "reset" triggers. Any help is most appreciated.

Thanks

Look up state machine.

rjstocks:
Hello All,

I am trying to create a process driven project in which the arduino will open solenoid valves for a certain time (delay) and then close solenoids and then open the next solenoid until completion. I would like to have the capability to pause, then resume this "process" or restart the process alltogether at any time during the process. I have tried to read through the interrupts and loop profiles to find the appropriate ways to handle this...as a novice to arduino not sure which methods would be the best fit as I need something to constantly look for the "pause" or "reset" triggers. Any help is most appreciated.

Thanks

so you want to make your ARDUINO a MACHINE that deals with and utilizes different STATEs?

well state machine can sort of get the job done, however, loops that start depending on a state will not pause if that state is changed...i guess what i want is an emergency stop...then be able to resume or restart the process from the beginning

however, loops that start depending on a state will not pause if that state is changed

They will if written correctly.

loop()
{
if (state == danger_will_robinson)
{ if (its_just_dr_smith) state = all_clear;
return;
}
}

1 Like

below is some code ive started, so KeithRB I should just nest some if loops to take care of the conditions?...how do I jump out once the process has started?

thanks again for the help

/*
Keg Washer

Uses mega2560 with 16 channel relay board to actuate ball valves to appropriate plumbing sources

*/

int valves[] = {
2, 3, 4, 5, 6, 7, 8, 9, 10
}; // an array of pin numbers to which LEDs are attached
int pinCount = 9; // the number of pins (i.e. the length of the array)
int drain = 0;
int air = 1;
int hotH20 = 2;
int caustic = 3;
int acid = 4;
int sany = 5;
int co2 = 6;
int causticDrain = 7;
int acidDrain = 8;
char state = 0;

void setup() {
// the array elements are numbered from 0 to (pinCount - 1).
// use a for loop to initialize each pin as an output:
Serial.begin(9600);
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
pinMode(valves[thisPin], OUTPUT); // sets as output
digitalWrite(valves[thisPin], HIGH); // turns relays off
}
}

//void(* resetFunc) (void) = 0;//declare reset function at address 0

void loop() {
// begin the keg washer cycle

while (Serial.available() > 0) {
// get incoming byte:
state = Serial.read();
// resetFunc(); //call reset
}

while (state == '1') {
digitalWrite(valves[drain], LOW); // open drain
Serial.println("open drain");
digitalWrite(valves[air], LOW); // purge air
Serial.println("purge air");
delay(10000);
digitalWrite(valves[air], HIGH); // air off
Serial.println("purge air off");
digitalWrite(valves[hotH20],LOW);// open hot rinse
Serial.println("open hot rinse");
delay(10000);
digitalWrite(valves[hotH20], HIGH); // close hot rinse
Serial.println("close hot rinse");
digitalWrite(valves[air], LOW); // open air purge
Serial.println("purge air");
delay(10000);
digitalWrite(valves[air], HIGH); // close air purge
Serial.println("purge air off");
digitalWrite(valves[caustic], LOW); // open caustic supply
Serial.println("start caustic");
digitalWrite(valves[causticDrain], LOW); // open caustic return
Serial.println("open caustic return");
digitalWrite(valves[drain], HIGH); // close drain
Serial.println("close drain");
delay(30000);
digitalWrite(valves[caustic], HIGH);
Serial.println("close caustic");
digitalWrite(valves[air], LOW);
Serial.println("purge air");
delay(10000);
digitalWrite(valves[air], HIGH);
Serial.println("purge air off");
digitalWrite(valves[causticDrain], HIGH);
Serial.println("close caustic return");
digitalWrite(valves[drain], LOW);
Serial.println("open drain");
delay(5000);
digitalWrite(valves[hotH20], LOW);
Serial.println("open hot rinse");
delay(10000);
digitalWrite(valves[hotH20], HIGH);
digitalWrite(valves[air], LOW);
delay(5000);
digitalWrite(valves[air], HIGH);
digitalWrite(valves[acid], LOW); // open caustic supply
digitalWrite(valves[acidDrain], LOW); // open caustic return
digitalWrite(valves[drain], HIGH); // close drain
delay(30000);
digitalWrite(valves[acid], HIGH);
digitalWrite(valves[air], LOW);
delay(10000);
digitalWrite(valves[air], HIGH);
digitalWrite(valves[acidDrain], HIGH);
digitalWrite(valves[drain], LOW);
delay(5000);
digitalWrite(valves[hotH20], LOW);
delay(10000);
digitalWrite(valves[hotH20], HIGH);
digitalWrite(valves[air], LOW);
delay(5000);
digitalWrite(valves[air], HIGH);
digitalWrite(valves[sany], LOW);
delay(15000);
digitalWrite(valves[sany], HIGH);
digitalWrite(valves[co2], LOW);
delay(5000);
digitalWrite(valves[drain], HIGH);
delay(10000);
state = 48;
}

while(state== '0'){

for (int thisPin = 0; thisPin < pinCount; thisPin++) {
digitalWrite(valves[thisPin], HIGH);
Serial.println("all valves closed");
}
while (Serial.available() > 0) {
// get incoming byte:
state = Serial.read();
// resetFunc(); //call reset
}
}

}

void serialEvent() {
while (Serial.available() > 0) {
// get incoming byte:
state = Serial.read();
// resetFunc(); //call reset
}
}

Your code is an industrial accident waiting to happen.
Please consider a professional rewrite urgently.

Groove:
Your code is an industrial accident waiting to happen.
Please consider a professional rewrite urgently.

you said it!

int caustic = 3;
int acid = 4;
int sany = 5;
int co2 = 6;
int causticDrain = 7;
int acidDrain = 8;

All those delay(long_time) statements will assure your code won't be able to abort. You need to start with a simpler project and learn how to ... (wait for it) ... do several things at the same time.

There is a Keg washer state machine without the use of delay()in this thread BEER! Developing a sketch to automate the cleaning process of beer kegs - Project Guidance - Arduino Forum

There is an emergency stop.

When the issues of pause and resume were considered it was decided not to pursue them. They are addressed some where between post #30 and #40

Wow. That's a lot of states, there.

You know, I'd consider doing this as an array of closures.

struct Step {
  void (*doStep)();
  unsigned long stepMs;


  Step((*doStep)(), unsigned long stepMs) : doStep(doStep), stepMs(stepMs) {} 
}
steps[] = {
  Step( []->{
    digitalWrite(valves[drain], LOW);  // open drain
     Serial.println("open drain");
     digitalWrite(valves[air], LOW); // purge air
     Serial.println("purge air");
  }, 10000 ),
  Step([]->{
    digitalWrite(valves[air], HIGH); // air off
    Serial.println("purge air off");
    digitalWrite(valves[hotH20],LOW);// open hot rinse
    Serial.println("open hot rinse");  
  }, 10000)
}

The main body then keeps track of which step it is at and at what time that step started.

rjstocks:
i guess what i want is an emergency stop...then be able to resume or restart the process from the beginning

It wasn't particularly difficult to implement the chart below using switch...case.

464482_state_chart.GIF

Thanks guys for pointing me in the right direction and keeping the disaster percentages lowered....once i have time to finish the re-write, i'll repost!

1 Like