automatic pressure controller

Hi all, I’m new to coding and learning it slowly as a bit of a bonding thing with my son. I’ve written a sketch which is supposed to control a compressor and exhaust valve for a pressure vessel to maintain it within certain limits. I also need to have a runtime limiter for one of the outputs (COMP) which will turn it off automatically if it runs for more than 5mins. I have simulated the height sensor with a potentiometer in my sketch and used LEDs as surrogate compressors and valves until I have the sketch working as desired. I have everything working as I want except for the time limit bit. I’ve spent about a week on it and still can’t get it. I want to do it in the sketch and not start using external timers or shields, etc. I’ve tried using the millis() function but it won’t work for me. What code I have is below. I’d really appreciate any help.

/*so far -

  • pot switches between red, green & yellow
  • Red = exhaust on
  • Green = compressor run
  • Yellow = pot fault/outside set limits
  • serial monitor prints current state, pot value and reset required command.
  • If fault condition occurs, an empty while loop hold the main loop where it is infinitely until the reset button is pressed.
  • During a fault condition, the relay switching the compressor feed goes high which cuts off the compressor.
  • Still need to figure out repeatable 5min run limit timer for compressor.
    */

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);//serial comms are started
int comp=8;//pin 8 is set as the compressor
int exh=9;//pin 9 is set as the exhaust
int fault=10;//pin 10 is set as the fault
pinMode(comp,OUTPUT);
pinMode(exh,OUTPUT);
pinMode(fault,OUTPUT);
//Next section tests the 3LEDs before running the program to make sure they are working
Serial.println((String)“LED test start”);
digitalWrite(comp,HIGH);digitalWrite(exh,HIGH);digitalWrite(fault,HIGH);
delay(5000);
digitalWrite(comp,LOW);digitalWrite(exh,LOW);digitalWrite(fault,LOW);
Serial.println((String)“LED test complete”);

}

void loop() {
// put your main code here, to run repeatedly:
int pot = analogRead(A0);//AI(A0) is set to be POT
float voltage = pot * (5.0 / 1023.0);//float value used to convert analog input to 0-5V display
Serial.println((String)"Voltage - "+voltage+“Vdc”);//the word “voltage” is printed to the serial monitor
int comp=8;
int exh=9;
int fault=10;
if (pot >52&&pot<=512){delay(2000);digitalWrite(comp,HIGH);digitalWrite(exh,LOW);digitalWrite(fault,LOW);Serial.println(“Compressor run”);}
//if the compressor run criteria are met then wait three seconds and then bring the compressor output high, exhaust & fault low, print “Compressor run” in the serial monitor
if (pot >512&&pot<=974){delay(2000);digitalWrite(exh,HIGH);digitalWrite(comp,LOW);digitalWrite(fault,LOW);Serial.println(“Exhaust active”);}
//if the exhaust run criteria are met then wait three seconds and then bring the exhaust output high, compressor & fault low, print “Exhaust active” in the serial monitor
if (pot <52||pot>974){digitalWrite(fault,HIGH);digitalWrite(comp,LOW);digitalWrite(exh,LOW);Serial.println(“reset required”);while(fault,HIGH){};}
/*if the fault criteria are met then immediately bring the compressor relay output high, fault high, compressor & exhaust low, print “reset required” in the serial monitor

  • An empty while loop holds the fault state until the reset button is pressed
    */
    //need to create runtimer next

}

Here is the type of thing I’ve tried in order to get the timer to work.

/*so far -

  • pot switches between red, green & yellow
  • Red = exhaust on
  • Green = compressor run
  • Yellow = pot fault/outside set limits
  • serial monitor prints current state, pot value and reset required command.
  • If fault condition occurs, an empty while loop hold the main loop where it is infinitely until the reset button is pressed.
  • During a fault condition, the relay switching the compressor feed goes high which cuts off the compressor.
  • Still need to figure out repeatable 5min run limit timer for compressor.
    */

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);//serial comms are started
int comp=8;//pin 8 is set as the compressor
int exh=9;//pin 9 is set as the exhaust
int fault=10;//pin 10 is set as the fault
pinMode(comp,OUTPUT);
pinMode(exh,OUTPUT);
pinMode(fault,OUTPUT);
//Next section tests the 3LEDs before running the program to make sure they are working
Serial.println((String)“LED test start”);
digitalWrite(comp,HIGH);digitalWrite(exh,HIGH);digitalWrite(fault,HIGH);
delay(5000);
digitalWrite(comp,LOW);digitalWrite(exh,LOW);digitalWrite(fault,LOW);
Serial.println((String)“LED test complete”);

}

void loop() {
// put your main code here, to run repeatedly:
int pot = analogRead(A0);//AI(A0) is set to be POT
float voltage = pot;//float value used to convert analog input to 0-5V display
Serial.println((String)"Voltage - "+voltage*(5.0 / 1023.0)+“Vdc”);//the word “voltage” is printed to the serial monitor
int comp=8;
int exh=9;
int fault=10;
unsigned long runtime;
unsigned long limit=8000;
if (pot >52&&pot<=512){delay(2000);runtime=millis();digitalWrite(comp,HIGH);digitalWrite(exh,LOW);digitalWrite(fault,LOW);Serial.println(“Compressor run”);}
//if the compressor run criteria are met then wait three seconds and then bring the compressor output high, exhaust & fault low, print “Compressor run” in the serial monitor
if (pot >512&&pot<=974){delay(2000);runtime=0;digitalWrite(exh,HIGH);digitalWrite(comp,LOW);digitalWrite(fault,LOW);Serial.println(“Exhaust active”);}
//if the exhaust run criteria are met then wait three seconds and then bring the exhaust output high, compressor & fault low, print “Exhaust active” in the serial monitor
if (pot <52||pot>974){digitalWrite(fault,HIGH);runtime=0;digitalWrite(comp,LOW);digitalWrite(exh,LOW);Serial.println(“reset required”);while(fault,HIGH){};}
/*if the fault criteria are met then immediately bring the compressor relay output high, fault high, compressor & exhaust low, print “reset required” in the serial monitor

  • An empty while loop holds the fault state until the reset button is pressed
    */
    //need to create runtimer next
    if (comp,HIGH&&exh,LOW&&millis()-runtime>limit){Serial.println((String)“runtime limit reached”);}
    }

It is unclear from your code and comments how you want this to work as it is all a bit of a jumble. It makes sense to you but not to me. Sentences like this:

If fault condition occurs, an empty while loop hold the main loop where it is infinitely until the reset button is pressed.

This is not a description of the operation, it’s how you’re programming it which isn’t helpful as a comment. Comments must describe the what and why, the program is the how and it stands on its own.

There is your code posted properly within code tags, please consider writing out the operation step by step with short, concise sentences.

/*so far -
   pot switches between red, green & yellow
   Red = exhaust on
   Green = compressor run
   Yellow = pot fault/outside set limits
   serial monitor prints current state, pot value and reset required command.
   If fault condition occurs, an empty while loop hold the main loop where it is infinitely until the reset button is pressed.
   During a fault condition, the relay switching the compressor feed goes high which cuts off the compressor.
   Still need to figure out repeatable 5min run limit timer for compressor.
*/

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);//serial comms are started
  int comp = 8; //pin 8 is set as the compressor
  int exh = 9; //pin 9 is set as the exhaust
  int fault = 10; //pin 10 is set as the fault
  pinMode(comp, OUTPUT);
  pinMode(exh, OUTPUT);
  pinMode(fault, OUTPUT);
  //Next section tests the 3LEDs before running the program to make sure they are working
  Serial.println((String)"LED test start");
  digitalWrite(comp, HIGH); digitalWrite(exh, HIGH); digitalWrite(fault, HIGH);
  delay(5000);
  digitalWrite(comp, LOW); digitalWrite(exh, LOW); digitalWrite(fault, LOW);
  Serial.println((String)"LED test complete");

}


void loop() {
  // put your main code here, to run repeatedly:
  int pot = analogRead(A0);//AI(A0) is set to be POT
  float voltage = pot;//float value used to convert analog input to 0-5V display
  Serial.println((String)"Voltage - " + voltage * (5.0 / 1023.0) + "Vdc"); //the word "voltage" is printed to the serial monitor
  int comp = 8;
  int exh = 9;
  int fault = 10;
  unsigned long runtime;
  unsigned long limit = 8000;
  if (pot > 52 && pot <= 512) {
    delay(2000);
    runtime = millis();
    digitalWrite(comp, HIGH);
    digitalWrite(exh, LOW);
    digitalWrite(fault, LOW);
    Serial.println("Compressor run");
  }
  //if the compressor run criteria are met then wait three seconds and then bring the compressor output high, exhaust & fault low, print "Compressor run" in the serial monitor
  if (pot > 512 && pot <= 974) {
    delay(2000);
    runtime = 0;
    digitalWrite(exh, HIGH);
    digitalWrite(comp, LOW);
    digitalWrite(fault, LOW);
    Serial.println("Exhaust active");
  }
  //if the exhaust run criteria are met then wait three seconds and then bring the exhaust output high, compressor & fault low, print "Exhaust active" in the serial monitor
  if (pot < 52 || pot > 974) {
    digitalWrite(fault, HIGH);
    runtime = 0;
    digitalWrite(comp, LOW);
    digitalWrite(exh, LOW);
    Serial.println("reset required");
    while (fault, HIGH) {};
  }
  /*if the fault criteria are met then immediately bring the compressor relay output high, fault high, compressor & exhaust low, print "reset required" in the serial monitor
     An empty while loop holds the fault state until the reset button is pressed
  */
  //need to create runtimer next
  if (comp, HIGH && exh, LOW && millis() - runtime > limit) {
    Serial.println((String)"runtime limit reached");
  }
}

Maybe this will help. Here is a 5 minute timer using the millis() function.

unsigned long interval = 5 * 60 * 1000;
unsigned long startTime;
unsigned long currentTime;

void setup() {
  Serial.begin(9600);
  Serial.print("Starting timer at ");
  startTime = millis();
  Serial.println(startTime);
}

void loop()
{
  currentTime = millis();
  if (currentTime - startTime > interval) {
    Serial.print("Time's up at: ");
    Serial.println(currentTime);
    while (true){} // die here
  }
}

@WattsThat, sorry for the dodgy descriptions. You're right, I put the notes in there for myself while making the sketch.
I actually tried a timer like the one you showed me already. It doesn't work for some reason.
I think where the trouble comes from for me is that I want the timer to only start if there is a (comp,HIGH) state(line 41). If that state changes then the timer must stop. I want to be able to limit how long the (comp,HIGH) state can occur for to prevent it running constantly. If (comp,HIGH) occurs for say 5mins unbroken then I want to be able to set (fault,HIGH), like line 45 but with different printed text. If (comp,HIGH) exist and then goes LOW before the time limit is reached then I need the timer to reset.
At one stage I got the program doing this but it was doing it to the wrong LED. It would turn off the red EXH LED after the set time and I have no idea why or how to make it work for the green COMP one.