take a look at following
#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;
const long solenoidInterval = 5000;
const long warmInterval = 3000;
// -------------------------------------
#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
const long solenoidInterval = 30000; // 30 sec
const long warmInterval = 15000; // 15 sec
#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
byte butState;
// -----------------------------------------------------------------------------
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
butState = digitalRead (RedEnd);
// 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 ("Press button to start");
lcd.clear ();
}
// -----------------------------------------------------------------------------
void monitor ()
{
// measure pressure
int psi = analogRead (pressureInput);
psi = map (psi, 102, 922, 0, 150);
// report pressure change
char s [40];
sprintf (s, "%3d psi, %3d min, %3d max", psi, Start, Stop);
lcd.clear ();
lcd.setCursor (0,0);
lcd.print (s);
// check for stop condition
if (psi >= Stop ) {
digitalWrite (ConcRelay1 , LOW);
digitalWrite (ConcRelay2 , LOW);
digitalWrite (CompRelay3 , LOW);
digitalWrite (ValveRelay4 , HIGH);
delay (solenoidInterval);
digitalWrite (ValveRelay4 , LOW);
}
else if (psi <= Start){
digitalWrite (ConcRelay1 , HIGH);
digitalWrite (ConcRelay2 , HIGH);
digitalWrite (ValveRelay4 , HIGH);
delay (warmInterval);
digitalWrite (ValveRelay4 , LOW);
digitalWrite (CompRelay3 , HIGH);
}
}
// -----------------------------------------------------------------------------
void loop ()
{
enum { ST_IDLE, ST_RUN, ST_SHUTDOWN };
static int state = ST_IDLE;
switch (state) {
case ST_RUN:
monitor ();
break;
case ST_SHUTDOWN:
digitalWrite (ConcRelay1 , LOW);
digitalWrite (ConcRelay2 , LOW);
digitalWrite (CompRelay3 , LOW);
digitalWrite (ValveRelay4 , HIGH);
lcd.clear ();
lcd.setCursor (1,0); //sets cursor to column 1, row 0
lcd.print ("Program Ended!"); //prints label
state = ST_IDLE;
break;
case ST_IDLE:
break;
}
byte but = digitalRead (RedEnd);
if (butState != but) {
butState = but;
delay (10); // debounce
if (LOW == but) { // press
if (ST_IDLE == state)
state = ST_RUN;
else
state = ST_SHUTDOWN;
}
}
}