i tried simulating your code
if ( (currentMillis - psiStartTime) >= warmInterval) {
the problem with a statement above it what prevents it from repeating instead of just occurring once.
psiStartTime gets set when you want it to occur, currentMillis increases and the action taken, but it continues to be taken more than once.
the same it true for the other timer case with solenoidInterval
i think your code recognize specific state such as
- monitoring the pressure for it to exceed some threshold
- recognizing when that threshold is exceeded and take some action that may take some period of time
- wait for that period to expire and probably start monitor for some other event such as the pressure dropping below some threshold
- similarly take action when that threshold is exceeded
here's a version more conventionally formatted
#include <Wire.h> //allows communication over i2c devices
#include <EEPROM.h> //Electronically erasable programmable memory library
#define MyHW
#ifdef MyHW
struct LiquidCrystal_I2C {
LiquidCrystal_I2C (int a, int b, int c) { };
void clear (void) { Serial.println (); };
void backlight (void) { };
void init (void) { };
void setCursor (int a, int b) { };
void print (char *s) { Serial.print (s); };
void print (const char *s) { Serial.print (s); };
void print (int i) { Serial.print (i); };
};
struct Bounce {
void attach (int pin) { };
void interval (int i) { };
void update (void) { };
};
const int pressureInput = A0;
const byte RedEnd = A1;
const byte ConcRelay1 = 10;
const byte ConcRelay2 = 11;
const byte CompRelay3 = 12;
const byte ValveRelay4 = 13;
const int Start = 88;
const int Stop = 92;
#else
#include <LiquidCrystal_I2C.h> //allows interfacing with LCD screens
#include <Bounce2.h> //Button debouncing library
//Inputs
int pressureInput = A7; //sets the analog input pin for the pressure transducer
int RedEnd = 10; //sets address of end loop button on Nano digital pins
//Digital Outputs
int ConcRelay1 = 2; //sets address of concentrator1 relay on digital pins found on Nano
int ConcRelay2 = 3; //sets address of concentrator2 relay on digital pins found on Nano
int CompRelay3 = 4; //sets address of compressor relay on digital pins found on Nano
int ValveRelay4 = 5; //sets address of unloader valve relay on digital pins found on Nano
//Start and stop pressure values
const int Start = 40; //concentrators and compressor start at 40 PSI
const int Stop = 85; //concentrators and compressor stop at 85 PSI
#endif
LiquidCrystal_I2C lcd (0x27, 16, 2); //sets the LCD I2C communication address; format (address, columns, rows)
Bounce debouncer = Bounce (); //instantiates a bounce object
// -----------------------------------------------------------------------------
// this is the EEPROM section
int eeAddress1 = 10; // this sets the eeAddress1 tag and sets the address to 10, eeprom
int eeAddress2 = 20; // this sets the eeAddress2 tag and sets the address to 20, eeprom
//Debounce section
int buttonState; // this set the tag "buttonState", to the current button state from the input pin
int lastButtonState = HIGH; // this set the tag "lastButtonState", to the previous button state from the input pin
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 100; // the debounce time; increase if the output flickers
// this is the "Clock"
unsigned long previousTime = 0; // this sets the tag "previousTime", that will store the last time "delay" that was updated (unnecessary?)
unsigned long psiStopTime; // time the PSI reached the stop point
unsigned long psiStartTime; // time the PSI reached the start point
const long solenoidInterval = 30000; // sets the interval time for solenoid to be open (30s)
const long warmInterval = 15000; // sets time interval for concentrators to be on with valve open prior to compressor start
// -----------------------------------------------------------------------------
void setup () //setup routine, runs once when system turned on or reset
{
// this section assigns the inputs and outputs
// inputs
pinMode (RedEnd,INPUT_PULLUP); // End loop button
debouncer.attach (RedEnd); // Attach the debouncer to REDEND with INPUT_PULLUP mode
debouncer.interval (debounceDelay); // interval in ms
// outputs
pinMode (ConcRelay1, OUTPUT); // concentrator1 relay
digitalWrite (ConcRelay1, LOW); // initially sets output tag "ConcRelay1" low
pinMode (ConcRelay2, OUTPUT); // concentrator2 relay
digitalWrite (ConcRelay2, LOW); // initially sets output tag "ConcRelay2" low
pinMode (CompRelay3, OUTPUT); // comperssor relay
digitalWrite (CompRelay3, LOW); // initially sets output tag "CompRelay3" low
pinMode (ValveRelay4, OUTPUT); // unloader valve relay
digitalWrite (ValveRelay4, LOW); // initially sets output tag "ValveRelay4" low
// Retrieves the setpoints from the arduino eeprom so the unit can start up after a power fail
EEPROM.get (eeAddress1, Start); // retrieves the start psi from eeprom
EEPROM.get (eeAddress2, Stop); // retrieves the stop psi from eeprom
// Starts up the different libraries
Serial.begin (9600); // start the serial monitor at 9600 baud
lcd.init (); // start the lcd library
lcd.backlight (); // turn on the lcd backlight
lcd.clear (); // clear the cld screen
Wire.begin (); // start the I2C library
//Start up messge
lcd.setCursor (2,0); //sets cursor to column 2, row 0
lcd.print ("Hello There!"); //prints label
lcd.setCursor (0,1); //sets cursor to column 0, row 1
lcd.print ("Starting in 3sec");
delay (3000);
lcd.clear ();
}
// -----------------------------------------------------------------------------
void loop ()
{
// this section monitors the live psi and turns the compressor and concentrators on or off based off setpoints
int psi = analogRead (pressureInput);
// this maps the raw analog input value to the converted PSI value
psi = map (psi, 102, 922, 0, 150);
// if psi is greater then turn off concentrator 1, 2 & compressor
if (psi >= Stop )
digitalWrite (ConcRelay1 , LOW);
if (psi >= Stop )
digitalWrite (ConcRelay2 , LOW);
if (psi >= Stop )
digitalWrite (CompRelay3 , LOW);
// this section keeps track of the millis counts (this is the "clock!")
// millis is running in the background so any program delays wont effect the timing
unsigned long currentMillis = millis (); // updates and holds a copy of the SecondMillis
if (psi >= Stop ){
digitalWrite (ValveRelay4 , HIGH); // if psi is greater than stop setpoint turn on solenoid valve
psiStopTime = currentMillis; // sets the time the stop point was reached to current time
}
if ( (currentMillis - psiStopTime) >= solenoidInterval){
digitalWrite (ValveRelay4 , LOW);
} //cuts power to solenoid after solenoidInterval time
if (psi <= Start){
digitalWrite (ConcRelay1 , HIGH); // if psi is less than start setpoint turn on concentrator1
digitalWrite (ConcRelay2 , HIGH); // if psi is less than start setpoint turn on concentrator2
digitalWrite (ValveRelay4 , HIGH); // if psi is less than than start setpoint turn on solenoid valve
psiStartTime = currentMillis;
}
// allows concentrators to warm up prior to starting compressor, O2 purged through valve
if ( (currentMillis - psiStartTime) >= warmInterval){
digitalWrite (ValveRelay4 , LOW); //cuts power to solenoid after warmInterval time
digitalWrite (CompRelay3 , HIGH); // if psi is less than start setpoint turn on compressor after warmInterval
}
// This section prints the PSI value caluculated from transducer to the LCD screeen
Serial.print (psi, 1); //prints value from previous line to serial
Serial.println ("psi"); //prints label to serial
lcd.setCursor (0,0); //sets cursor to column 0, row 0
lcd.print ("Current= "); //prints label
lcd.setCursor (10,0); //sets cursor to column 10, row 0
lcd.print (psi); //prints pressure value to lcd screen
lcd.setCursor (12,0); //sets cursor to column 12, row 0
lcd.print ("PSI"); //prints label after value
lcd.print (" "); //to clear the display after large values or negatives
lcd.setCursor (0,1); //sets cursor to column 0, row 1
lcd.print ("Min=40 ; Max=85"); //prints Start and Stop pressure values
// Update the Bounce instance :
debouncer.update ();
// this section defines function of RedEnd button
if (digitalRead (RedEnd)== LOW){
digitalWrite (ConcRelay1 , LOW);
digitalWrite (ConcRelay2 , LOW);
digitalWrite (CompRelay3 , LOW);
digitalWrite (ValveRelay4 , HIGH);
// Notifies that the end cycle button has been pressed
lcd.clear ();
lcd.setCursor (1,0); //sets cursor to column 1, row 0
lcd.print ("Program Ended!"); //prints label
lcd.setCursor (0,1); //sets cursor to column 0, row 1
lcd.print ("60s to shut down"); //prints label
delay (60000);
}
}