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
}