I don't have it up on github but here is the code.
#include <Wire.h>
#include <LiquidCrystal.h> // include LCD library
#include <EEPROM.h>
const byte EEPROM_ID = 0x99;
float pres_1;
float pres_2;
// DEFAULT VALUES - ONLY USED ON FIRST RUN OR IF EEPROM IS NOT AVAILABLE
//------------------------------------------------------------------------------------------
float SET_PRES_1 = 5;
float SET_PRES_2 = 5;
int DELAY = 30;
float Hy = 0.2;
float PRES_OFFSET_1 = 0;
float PRES_OFFSET_2 = 0;
int menu = 1; // Set starting menu to 1 - Pressure Menu
int write_current = 0;
// RELAY CONTROL VARIABLES
//------------------------------------------------------------------------------------------
int pres_1_relay = 12;
int pres_2_relay = 13;
int countdown_1;
int countdown_2;
long start_time_1;
long start_time_2;
int relay_state_1;
int relay_state_2;
int start_relay_state_1;
int start_relay_state_2;
// EEPROM ADDRESSES FOR STORED VARIABLES
//------------------------------------------------------------------------------------------
const int ID_ADDR = 0; // the EEPROM address used to store the ID
const int SET_PRES_1_E_ADDR = 1;
const int SET_PRES_2_E_ADDR = 2;
const int DELAY_E_ADDR = 3;
const int Hy_E_ADDR = 4;
const int PRES_OFFSET_1_E_ADDR = 5;
const int PRES_OFFSET_1_SIGN_E_ADDR = 6;
const int PRES_OFFSET_2_E_ADDR = 7;
const int PRES_OFFSET_2_SIGN_E_ADDR = 8;
int SET_PRES_1_E;
int SET_PRES_2_E;
int DELAY_E;
int Hy_E;
int PRES_OFFSET_1_E;
int PRES_OFFSET_2_E;
int PRES_OFFSET_1_SIGN_E;
int PRES_OFFSET_2_SIGN_E;
/*--------------------------------------------------------------------------------------
Defines
--------------------------------------------------------------------------------------*/
// Pins in use
#define BUTTON_ADC_PIN A0 // A0 is the button ADC input
#define LCD_BACKLIGHT_PIN 3 // D3 controls LCD backlight
// ADC readings expected for the 5 buttons on the ADC input
#define RIGHT_10BIT_ADC 0 // right
#define UP_10BIT_ADC 145 // up
#define DOWN_10BIT_ADC 329 // down
#define LEFT_10BIT_ADC 505 // left
#define SELECT_10BIT_ADC 741 // right
#define BUTTONHYSTERESIS 10 // hysteresis for valid button sensing window
//return values for ReadButtons()
#define BUTTON_NONE 0 //
#define BUTTON_RIGHT 1 //
#define BUTTON_UP 2 //
#define BUTTON_DOWN 3 //
#define BUTTON_LEFT 4 //
#define BUTTON_SELECT 5 //
//some example macros with friendly labels for LCD backlight/pin control, tested and can be swapped into the example code as you like
#define LCD_BACKLIGHT_OFF() digitalWrite( LCD_BACKLIGHT_PIN, LOW )
#define LCD_BACKLIGHT_ON() digitalWrite( LCD_BACKLIGHT_PIN, HIGH )
#define LCD_BACKLIGHT(state) { if( state ){digitalWrite( LCD_BACKLIGHT_PIN, HIGH );}else{digitalWrite( LCD_BACKLIGHT_PIN, LOW );} }
/*--------------------------------------------------------------------------------------
Variables
--------------------------------------------------------------------------------------*/
byte buttonJustPressed = false; //this will be true after a ReadButtons() call if triggered
byte buttonJustReleased = false; //this will be true after a ReadButtons() call if triggered
byte buttonWas = BUTTON_NONE; //used by ReadButtons() for detection of button events
/*--------------------------------------------------------------------------------------
Init the LCD library with the LCD pins to be used
--------------------------------------------------------------------------------------*/
LiquidCrystal lcd( 8, 9, 4, 5, 6, 7 ); //Pins for the freetronics 16x2 LCD shield. LCD: ( RS, E, LCD-D4, LCD-D5, LCD-D6, LCD-D7 )
/*--------------------------------------------------------------------------------------
setup()
Called by the Arduino framework once, before the main loop begins
--------------------------------------------------------------------------------------*/
//Presure Sensor Details
//----------------------------------------------------------------------------------------
#define SENSOR_1_PIN A4 // Pin that pressure sensor 1 is connected to
#define SENSOR_2_PIN A5 // Pin that pressure sensor 2 is connected to
#define NUM_SAMPLES 50 // Number of samples
int INPUT_ARRAY_1[NUM_SAMPLES]; // Setup input array
int INPUT_ARRAY_2[NUM_SAMPLES]; // Setup input array
int i; // Integer for inputing values into array
// REFRESH TIMES
//----------------------------------------------------------------------------------------------
long previousMillis_1 = 0;
long interval_1 = 1000; // Serial refresh time
long previousMillis_2 = 0;
long interval_2 = 1000; // LCD refresh time
long previousMillis_3 = 0;
long interval_3 = 500; // Button debounce time (LEFT & RIGHT)
long previousMillis_6 = 0;
long interval_6 = 2000; // Delay between probe probe reads
//______________________________________________________________________________________________
// PROGRAM SETUP
//______________________________________________________________________________________________
void setup()
{
// Serial.begin(9600);
// READ STORED VARIABLES FROM EEPROM
//------------------------------------------------------------------------------------------
// Serial.println("Using data from EEPROM");
SET_PRES_1_E = EEPROM.read(SET_PRES_1_E_ADDR);
SET_PRES_2_E = EEPROM.read(SET_PRES_2_E_ADDR);
DELAY_E = EEPROM.read(DELAY_E_ADDR);
Hy_E = EEPROM.read(Hy_E_ADDR);
PRES_OFFSET_1_E = EEPROM.read(PRES_OFFSET_1_E_ADDR);
PRES_OFFSET_1_SIGN_E = EEPROM.read(PRES_OFFSET_1_SIGN_E_ADDR);
PRES_OFFSET_2_E = EEPROM.read(PRES_OFFSET_2_E_ADDR);
PRES_OFFSET_2_SIGN_E = EEPROM.read(PRES_OFFSET_2_SIGN_E_ADDR);
// Calculate SET_PRES values from stored integers
SET_PRES_1 = SET_PRES_1_E;
SET_PRES_2 = SET_PRES_2_E;
// Calculate DELAY value from stored integers
DELAY = DELAY_E;
// Calculate stored Hy value from integers
Hy = 0.1 * Hy_E;
// Calculate stored Pressure Offset values from integers
if(PRES_OFFSET_1_SIGN_E == 1){
PRES_OFFSET_1 = 0.1 * PRES_OFFSET_1_E;
}
else if(PRES_OFFSET_1_SIGN_E == 2){
PRES_OFFSET_1 = -0.1 * PRES_OFFSET_1_E;
}
if(PRES_OFFSET_2_SIGN_E == 1){
PRES_OFFSET_2 = 0.1 * PRES_OFFSET_2_E;
}
else if(PRES_OFFSET_2_SIGN_E == 2){
PRES_OFFSET_2 = -0.1 * PRES_OFFSET_2_E;
}
//button adc input
pinMode( BUTTON_ADC_PIN, INPUT ); //ensure A0 is an input
digitalWrite( BUTTON_ADC_PIN, LOW ); //ensure pullup is off on A0
//lcd backlight control
digitalWrite( LCD_BACKLIGHT_PIN, HIGH ); //backlight control pin D3 is high (on)
pinMode( LCD_BACKLIGHT_PIN, OUTPUT ); //D3 is an output
//set up the LCD number of columns and rows:
lcd.begin( 16, 2 );
// RELAY CLOCKS AND PIN OUTPUT
//---------------------------------------------------------------------------------------------
start_time_1 = -1000*DELAY;
start_time_2 = -1000*DELAY;
pinMode(pres_1_relay, OUTPUT);
pinMode(pres_2_relay, OUTPUT);
digitalWrite(pres_1_relay, HIGH);
digitalWrite(pres_2_relay, HIGH);
}
//______________________________________________________________________________________________
// MAIN PROGRAM LOOP
//______________________________________________________________________________________________
void loop()
{
float media_1;
float media_2;
countdown_1 = DELAY - (millis() - start_time_1)/1000;
countdown_1 = constrain(countdown_1, 0, DELAY);
countdown_2 = DELAY - (millis() - start_time_2)/1000;
countdown_2 = constrain(countdown_2, 0, DELAY);
start_relay_state_1 = digitalRead(pres_1_relay);
start_relay_state_2 = digitalRead(pres_2_relay);
unsigned long currentMillis_6 = millis();
if(currentMillis_6 - previousMillis_6 > interval_6) {
previousMillis_6 = currentMillis_6;
// Writes resistance readings from analog pin to array
for (i=0; i< NUM_SAMPLES; i++) {
INPUT_ARRAY_1 = analogRead(SENSOR_1_PIN);
INPUT_ARRAY_2 = analogRead(SENSOR_2_PIN);
}
// Sums array values
media_1= 0;
media_2= 0;
for (i=0; i< NUM_SAMPLES; i++) {
media_1 += INPUT_ARRAY_1;
media_2 += INPUT_ARRAY_2;
}
// Divides sum of array values by number of entries to find average
media_1 = media_1 / NUM_SAMPLES;
media_2 = media_2 / NUM_SAMPLES;
// Convert the thermal stress value to resistance
media_1 = (media_1 * 5) / 1024;
media_2 = (media_2 * 5) / 1024;
pres_1 = PRES_OFFSET_1 -14.5 + (media_1 - 0.5) * (36.3 + 14.5) / (4.5 - 0.5);
pres_2 = PRES_OFFSET_2 -14.5 + (media_2 - 0.5) * (36.3 + 14.5) / (4.5 - 0.5);
pres_1 = constrain(pres_1, 0, 40);
pres_2 = constrain(pres_2, 0, 40);
// SET RELAY OUTPUTS
//---------------------------------------------------------------------------------------------
if(pres_1 > SET_PRES_1 && countdown_1==0)
{
digitalWrite(pres_1_relay, LOW);
}
else if(pres_1 > SET_PRES_1 && countdown_1 > 0)
{
digitalWrite(pres_1_relay, HIGH);
}
else if(pres_1 <= SET_PRES_1 && pres_1 > SET_PRES_1 - Hy)
{
digitalWrite(pres_1_relay, LOW);
}
else if(pres_1 <= SET_PRES_1 - Hy)
{
digitalWrite(pres_1_relay, HIGH);
}
if(pres_2 > SET_PRES_2 && countdown_2==0)
{
digitalWrite(pres_2_relay, LOW);
}
else if(pres_2 > SET_PRES_2 && countdown_2 > 0)
{
digitalWrite(pres_2_relay, HIGH);
}
else if(pres_2 <= SET_PRES_2 && pres_2 > SET_PRES_2 - Hy)
{
digitalWrite(pres_2_relay, LOW);
}
else if(pres_2 <= SET_PRES_2 - Hy)
{
digitalWrite(pres_2_relay, HIGH);
}
relay_state_1 = digitalRead(pres_1_relay);
relay_state_2 = digitalRead(pres_2_relay);
if(relay_state_1 == HIGH && start_relay_state_1 == LOW) {
start_time_1 = millis();
}
if(relay_state_2 == HIGH && start_relay_state_2 == LOW) {
start_time_2 = millis();
}
}
byte button;
byte timestamp;
//get the latest button pressed, also the buttonJustPressed, buttonJustReleased flags
button = ReadButtons();
//blank the demo text line if a new button is pressed or released, ready for a new label to be written
/* if( buttonJustPressed || buttonJustReleased )
{
lcd.setCursor( 4, 1 );
lcd.print( " " );
}
//show text label for the button pressed
switch( button )
{
case BUTTON_NONE:
{
break;
}
case BUTTON_RIGHT:
{
Serial.print( "RIGHT" );
break;
}
case BUTTON_UP:
{
Serial.print( "UP" );
break;
}
case BUTTON_DOWN:
{
Serial.print( "DOWN" );
break;
}
case BUTTON_LEFT:
{
Serial.print( "LEFT" );
break;
}
case BUTTON_SELECT:
{
Serial.print( "SELECT" );
//clear the buttonJustPressed or buttonJustReleased flags, they've already done their job now.
if( buttonJustPressed )
buttonJustPressed = false;
if( buttonJustReleased )
buttonJustReleased = false;
// MENUS AND VARIABLE INPUTS
//-----------------------------------------------------------------------------------------------
//MENU 1 - MAIN MENU ----------------------------------------------------------------------------
if(menu==1){
lcd.setCursor( 0, 0 );
lcd.print( "F1 " );
lcd.setCursor( 7, 0 );
lcd.print( "psi " );
lcd.setCursor( 11, 0 );
if(SET_PRES_1<10){
lcd.print(" ");
lcd.print(SET_PRES_1,0);
}
else
{
lcd.print(SET_PRES_1,0);
}
lcd.print( "psi" );
lcd.setCursor( 13, 0 );
lcd.print( "psi" );
lcd.setCursor( 0, 1 );
lcd.print( "F2 " );
lcd.setCursor( 7, 1 );
lcd.print( "psi " );
lcd.setCursor( 11, 1 );
if(SET_PRES_2 < 10){
lcd.print(" ");
lcd.print(SET_PRES_2,0);
}
else
{
lcd.print(SET_PRES_2,0);
}
lcd.print( "psi" );
lcd.setCursor( 13, 1 );
lcd.print( "psi" );
lcd.setCursor( 3, 0 );
if(pres_1 < 10){
lcd.print(" ");
lcd.print(pres_1,1);
}
else
{
lcd.print(pres_1,1);
}
lcd.setCursor( 3, 1 );
if(pres_2<10){
lcd.print(" ");
lcd.print(pres_2,1);
}
else
{
lcd.print(pres_2,1);
}
if(button==BUTTON_LEFT){
menu = 5;
lcd.clear();
}
else if(button==BUTTON_RIGHT){
menu = 2;
lcd.clear();
}
else if(button==BUTTON_SELECT){
menu = 1;
lcd.clear();
if(write_current == 1)
{
eeprom_save_standard();
write_current = 0;
}
}
}
//MENU 2 - Fermentor 1 Set Pressure ----------------------------------------------------------------------------
else if(menu==2){
lcd.setCursor( 0, 0 );
lcd.print( "Fermentor 1 " );
lcd.setCursor( 0, 1 );
lcd.print( "Set Pres: " );
lcd.setCursor( 10, 1 );
if(SET_PRES_1<10){
lcd.print(" ");
lcd.print(SET_PRES_1,0);
}
else
{
lcd.print(SET_PRES_1,0);
}
lcd.print( "psi " );
if(button==BUTTON_UP){
SET_PRES_1++;
SET_PRES_1 = constrain(SET_PRES_1, 0, 30);
write_current = 1;
}
else if(button==BUTTON_DOWN){
SET_PRES_1--;
SET_PRES_1 = constrain(SET_PRES_1, 0, 30);
write_current = 1;
}
else if(button==BUTTON_LEFT){
menu = 1;
lcd.clear();
}
else if(button==BUTTON_RIGHT){
menu = 3;
lcd.clear();
}
else if(button==BUTTON_SELECT){
if(write_current == 1)
{
eeprom_save_standard();
write_current = 0;
}
menu = 1;
lcd.clear();
}
}
//MENU 3 - Fermenter 2 Set Pressure ----------------------------------------------------------------------------
else if(menu==3){
lcd.setCursor( 0, 0 );
lcd.print( "Fermentor 2 " );
lcd.setCursor( 0, 1 );
lcd.print( "Set Pres: " );
lcd.setCursor( 10, 1 );
if(SET_PRES_1 < 10){
lcd.print(" ");
lcd.print(SET_PRES_2,0);
}
else
{
lcd.print(SET_PRES_2,0);
}
lcd.print( "psi " );
if(button==BUTTON_UP){
SET_PRES_2++;
SET_PRES_2 = constrain(SET_PRES_2, 0, 30);
write_current = 1;
}
else if(button==BUTTON_DOWN){
SET_PRES_2--;
SET_PRES_2 = constrain(SET_PRES_2, 0, 30);
write_current = 1;
}
else if(button==BUTTON_LEFT){
menu--;
lcd.clear();
}
else if(button==BUTTON_RIGHT){
menu++;
lcd.clear();
}
else if(button==BUTTON_SELECT){
if(write_current == 1)
{
eeprom_save_standard();
write_current = 0;
}
menu = 1;
lcd.clear();
}
}
//MENU 4 - Solenoid Delay ----------------------------------------------------------------------
else if(menu==4){
lcd.setCursor( 0, 0 );
lcd.print( "Solenoid Delay:" );
lcd.setCursor( 0, 1 );
lcd.print(DELAY);
lcd.print( "sec " );
if(button==BUTTON_UP){
DELAY++;
DELAY = constrain(DELAY, 0, 600);
write_current = 1;
}
else if(button==BUTTON_DOWN){
DELAY--;
DELAY = constrain(DELAY, 0, 600);
write_current = 1;
}
if(button==BUTTON_LEFT){
menu--;
lcd.clear();
}
else if(button==BUTTON_RIGHT){
menu++;
lcd.clear();
}
else if(button==BUTTON_SELECT){
if(write_current == 1)
{
eeprom_save_standard();
write_current = 0;
}
menu = 1;
lcd.clear();
}
}
//MENU 5 - Hysteresis ----------------------------------------------------------------------
else if(menu==5){
lcd.setCursor( 0, 0 );
lcd.print( "Pressure Hyst: " );
lcd.setCursor( 0, 1 );
lcd.print(Hy,1);
lcd.print( "psi " );
if(button==BUTTON_UP){
Hy = Hy + 0.1;
Hy = constrain(Hy, 0, 5);
write_current = 1;
}
else if(button==BUTTON_DOWN){
Hy = Hy - 0.1;
Hy = constrain(Hy, 0, 5);
write_current = 1;
}
if(button==BUTTON_LEFT){
menu--;
lcd.clear();
}
else if(button==BUTTON_RIGHT){
menu++;
lcd.clear();
}
else if(button==BUTTON_SELECT){
if(write_current == 1)
{
eeprom_save_standard();
write_current = 0;
}
menu = 1;
lcd.clear();
}
}
//MENU 6 - Pressure Offset 1 ----------------------------------------------------------------------
else if(menu==6){
lcd.setCursor( 0, 0 );
lcd.print( "Pressure Offset 1: " );
lcd.setCursor( 0, 1 );
lcd.print(PRES_OFFSET_1,1);
lcd.print( "psi " );
if(button==BUTTON_UP){
PRES_OFFSET_1 = PRES_OFFSET_1 + 0.1;
write_current = 1;
}
else if(button==BUTTON_DOWN){
PRES_OFFSET_1 = PRES_OFFSET_1 - 0.1;
write_current = 1;
}
if(button==BUTTON_LEFT){
menu--;
lcd.clear();
}
else if(button==BUTTON_RIGHT){
menu++;
lcd.clear();
}
else if(button==BUTTON_SELECT){
if(write_current == 1)
{
eeprom_save_standard();
write_current = 0;
}
menu = 1;
lcd.clear();
}
}
//MENU 7 - Pressure Offset 2 ----------------------------------------------------------------------
else if(menu==7){
lcd.setCursor( 0, 0 );
lcd.print( "Pressure Offset 2: " );
lcd.setCursor( 0, 1 );
lcd.print(PRES_OFFSET_2,1);
lcd.print( "psi " );
if(button==BUTTON_UP){
PRES_OFFSET_2 = PRES_OFFSET_2 + 0.1;
write_current = 1;
}
else if(button==BUTTON_DOWN){
PRES_OFFSET_2 = PRES_OFFSET_2 - 0.1;
write_current = 1;
}
if(button==BUTTON_LEFT){
menu--;
lcd.clear();
}
else if(button==BUTTON_RIGHT){
menu = 1;
lcd.clear();
}
else if(button==BUTTON_SELECT){
if(write_current == 1)
{
eeprom_save_standard();
write_current = 0;
}
menu = 1;
lcd.clear();
}
}
}
//____________________________________________________________________________________________
// BUTTON READ SUB ROUTINE
//____________________________________________________________________________________________
byte ReadButtons()
{
unsigned int buttonVoltage;
unsigned long currentMillis_3 = millis();
byte button = BUTTON_NONE; // return no button pressed if the below checks don't write to btn
//read the button ADC pin voltage
buttonVoltage = analogRead( BUTTON_ADC_PIN );
//sense if the voltage falls within valid voltage windows
if(currentMillis_3 - previousMillis_3 > interval_3) {
previousMillis_3 = currentMillis_3;
if( buttonVoltage < ( RIGHT_10BIT_ADC + BUTTONHYSTERESIS ) )
{
button = BUTTON_RIGHT;
}
else if( buttonVoltage >= ( UP_10BIT_ADC - BUTTONHYSTERESIS )
&& buttonVoltage <= ( UP_10BIT_ADC + BUTTONHYSTERESIS ) )
{
button = BUTTON_UP;
}
else if( buttonVoltage >= ( DOWN_10BIT_ADC - BUTTONHYSTERESIS )
&& buttonVoltage <= ( DOWN_10BIT_ADC + BUTTONHYSTERESIS ) )
{
button = BUTTON_DOWN;
}
else if( buttonVoltage >= ( LEFT_10BIT_ADC - BUTTONHYSTERESIS )
&& buttonVoltage <= ( LEFT_10BIT_ADC + BUTTONHYSTERESIS ) )
{
button = BUTTON_LEFT;
}
else if( buttonVoltage >= ( SELECT_10BIT_ADC - BUTTONHYSTERESIS )
&& buttonVoltage <= ( SELECT_10BIT_ADC + BUTTONHYSTERESIS ) )
{
button = BUTTON_SELECT;
}
}
//handle button flags for just pressed and just released events
if( ( buttonWas == BUTTON_NONE ) && ( button != BUTTON_NONE ) )
{
//the button was just pressed, set buttonJustPressed, this can optionally be used to trigger a once-off action for a button press event
//it's the duty of the receiver to clear these flags if it wants to detect a new button change event
buttonJustPressed = true;
buttonJustReleased = false;
}
if( ( buttonWas != BUTTON_NONE ) && ( button == BUTTON_NONE ) )
{
buttonJustPressed = false;
buttonJustReleased = true;
}
//save the latest button value, for change event detection next time round
buttonWas = button;
return( button );
}
//____________________________________________________________________________________________
// SAVE STANDARD VALUES TO EEPROM
//____________________________________________________________________________________________
void eeprom_save_standard() {
// Convert decimal values to integers for saving
SET_PRES_1_E = SET_PRES_1;
SET_PRES_2_E = SET_PRES_2;
Hy_E = 10 * Hy;
DELAY_E = DELAY;
PRES_OFFSET_1_E = 10 * PRES_OFFSET_1;
PRES_OFFSET_2_E = 10 * PRES_OFFSET_2;
if(PRES_OFFSET_1 >= 0){
PRES_OFFSET_1_SIGN_E = 1;
}
else {
PRES_OFFSET_1_SIGN_E = 2;
}
if(PRES_OFFSET_2 >= 0){
PRES_OFFSET_2_SIGN_E = 1;
}
else {
PRES_OFFSET_2_SIGN_E = 2;
}
// Send message to serial
Serial.println("Writing default data to EEPROM");
// Write values to EEPROM
EEPROM.write(ID_ADDR,EEPROM_ID); // write the ID to indicate valid data
EEPROM.write(SET_PRES_1_E_ADDR, SET_PRES_1_E);
EEPROM.write(SET_PRES_2_E_ADDR, SET_PRES_2_E);
EEPROM.write(Hy_E_ADDR, Hy_E);
EEPROM.write(DELAY_E_ADDR, DELAY_E);
EEPROM.write(PRES_OFFSET_1_E_ADDR, PRES_OFFSET_1_E);
EEPROM.write(PRES_OFFSET_2_E_ADDR, PRES_OFFSET_2_E);
EEPROM.write(PRES_OFFSET_1_SIGN_E_ADDR, PRES_OFFSET_1_SIGN_E);
EEPROM.write(PRES_OFFSET_2_SIGN_E_ADDR, PRES_OFFSET_2_SIGN_E);
}