Hi all-
First time poster here, I've been lurking for a long while trying to build out a project I've been working on. I've looked all over the forum for similar topics on this issue and have tried so many variations on code my head is swimming at this point.
The general idea is that a relay bank will control several components of an Arduino based pressure control switch. I would like to use a button to end the pressure control switch cycle and leave the components in a state which is safe to power down the controller. The issue I'm having is that on power up the code is entering directly into power down cycle, as if the button has been pressed. For further info the switch leads are placed on 5V the and D7 pins. Also, while we're at it, if anyone sees any issues with any other portion of my code (especially the use of the millis function for delays) I'd be really appreciative for your critique.
Thanks very much for any input in advance, looking forward to hearing what you all have to say!
#include <Wire.h> //allows communication over i2c devices
#include <EEPROM.h> //Electronically erasable programmable memory library
#include <LiquidCrystal_I2C.h> //allows interfacing with LCD screens
LiquidCrystal_I2C lcd(0x27, 16, 2); //sets the LCD I2C communication address; format(address, columns, rows)
#include <Bounce2.h> //Button debouncing library
Bounce debouncer = Bounce(); //instantiates a bounce object
//Inputs
int pressureInput = A7; //sets the analog input pin for the pressure transducer
int RedEnd = 7; //sets address of end loop button on Nano digital pins
//Digital Outputs
const int ConcRelay1 = 2; //sets address of concentrator1 relay on digital pins found on Nano
const int ConcRelay2 = 3; //sets address of concentrator2 relay on digital pins found on Nano
const int CompRelay3 = 4; //sets address of compressor relay on digital pins found on Nano
const 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
//Variables
int ConcRelay1bit = LOW; // this sets the variable tag ConcRelay2bit to low
int ConcRelay2bit = LOW; // this sets the variable tag ConcRelay3bit to low
int CompRelay3bit = LOW; // this sets the variable tag CompRelay3Bit to low
int ValveRelay4bit = LOW; // this sets the variable tag ValveRelay4Bit to low
// 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 = LOW; // 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() //loop routine runs over and over again forever
{
// this section monitors the live psi and turns the compressor and concentrators on or off based off setpoints
int psi = analogRead(7); // this reads the analog input(A7) and scales it
psi = map(psi, 102, 922, 0, 150); // this maps the raw analog input value to the converted PSI value
if (psi >= Stop ){ConcRelay1bit = LOW;} // if psi is greater than stop setpoint turn off concentrator1
if (psi >= Stop ){ConcRelay2bit = LOW;} // if psi is greater than stop setpoint turn off concentrator2
if (psi >= Stop ){CompRelay3bit = LOW;} // if psi is greater than stop setpoint turn off compressor
// 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 ){
{ValveRelay4bit = 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 ((unsigned long)(currentMillis - psiStopTime) >= solenoidInterval){ValveRelay4bit = LOW;} //cuts power to solenoid after solenoidInterval time
if (psi<=Start){
{ConcRelay1bit = HIGH;} // if psi is less than start setpoint turn on concentrator1
{ConcRelay2bit = HIGH;} // if psi is less than start setpoint turn on concentrator2
{ValveRelay4bit = HIGH;} // if psi is less than than start setpoint turn on solenoid valve
psiStartTime = currentMillis;
}
if ((unsigned long)(currentMillis - psiStartTime) >= warmInterval){ // allows concentrators to warm up prior to starting compressor, O2 purged through valve
{ValveRelay4bit = LOW;} //cuts power to solenoid after warmInterval time
{CompRelay3bit = 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)== HIGH){
{ConcRelay1bit = LOW;}
{ConcRelay2bit = LOW;}
{CompRelay3bit = LOW;}
{ValveRelay4bit = HIGH;}
delay(60000);
// 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
}}
// Code End