Work in progress 2 Probes, LCD, 2 Relay, Buttons, Swich, menu and eeprom write.

/*      JulioMGM       */
/* Use It and Abuse It */
#include <EEPROM.h>
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define I2C_ADDR    0x3F  // Define I2C Address where the PCF8574A is
#define BACKLIGHT_PIN     3
#define En_pin  2
#define Rw_pin  1
#define Rs_pin  0
#define D4_pin  4
#define D5_pin  5
#define D6_pin  6
#define D7_pin  7
/**/
#define ONE_WIRE_BUS_PIN 2
OneWire oneWire(ONE_WIRE_BUS_PIN);
DallasTemperature sensors(&oneWire);
DeviceAddress Probe01 = { 0x28, 0xF5, 0xC9, 0xE3, 0x08, 0x00, 0x00, 0xB2 };
DeviceAddress Probe02 = { 0x28, 0x0F, 0x58, 0xE2, 0x08, 0x00, 0x00, 0xFA };
/**/
LiquidCrystal_I2C  lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);
/**/
int pinOut1 = 10;
int ProbeN = 1;
/**/
int OPSelc = 0;
int OPTime = 0;
float OPVal = 0;
float OPMax = 0;
float OPMin = 0;
float OPInc = 0;
/**/
double TempMIN  = 30;
double TempMAX  = 60;
double Temp1    = 0;
double Temp2    = 0;
double TempES   = 0;
String Relay1   = "OFF";
/**/
float TempDH    = 2.0;
float TempDL    = 0.5;
float TempUT    = 0.0;
/**/
const int button1Pin = 3;  // pushbutton 1 pin
const int button2Pin = 4;  // pushbutton 2 pin
const int button3Pin = 5;  // pushbutton 3 pin
const int button4Pin = 6;  // pushbutton 4 pin
/**/
int button1State, button2State, button3State, button4State;  // variables to hold the pushbutton states
/**/
int Input1Pin=7 ;
int Input1State=0 ;
int Input1ONDelay = 10 ;
int Input1OFFDelay = 10 ;
int pinOut2 = 9 ;
String Relay2 = "OFF" ;
int Input1ONLast = 0 ;
int Input1OFFLast = 0 ;
/**/
void setup()
{
  /**/
  TempMIN         = EEPROMRead(10);
  TempDH          = EEPROMRead(20);
  TempDL          = EEPROMRead(30);
  TempMAX         = EEPROMRead(40);
  Input1ONDelay   = EEPROMRead(50);
  Input1OFFDelay  = EEPROMRead(60);
  /**/
  lcd.begin (20,4);
  // Switch on the backlight
  lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
  lcd.setBacklight(HIGH);
  lcd.home ();                   // go home
  lcd.setCursor ( 0, 0 );
  lcd.print("MIN ON:"+String(TempMIN,1) + "          ");
  lcd.setCursor ( 0, 1 );
  lcd.print("DIF:"+String(TempDH,1) + "  OFF:" + String(TempDL,1));
  /**/
  Serial.begin(9600);
  // Initialize the Temperature measurement library
  sensors.begin();
  // set the resolution to 10 bit (Can be 9 to 12 bits .. lower is faster)
  sensors.setResolution(Probe01, 10);
  sensors.setResolution(Probe02, 10);
  /**/
  pinMode(pinOut1, OUTPUT);
  /**/
  delay(1000);
  lcd.clear();
  /**/
  // Set up the pushbutton pins to be an input:
  pinMode(button1Pin, INPUT);
  pinMode(button2Pin, INPUT);
  pinMode(button3Pin, INPUT);
  pinMode(button4Pin, INPUT);
  /**/
  pinMode(Input1Pin, INPUT);
  pinMode(pinOut2, OUTPUT);
  /**/
}

void loop()
{
  LCDInit();

  if (OPSelc > 0) {LCDMenu();}
  else{LCDMain();}

  delay(10);

  if (OPTime >200) {lcd.setBacklight(LOW);}
  else{lcd.setBacklight(HIGH);}
  Input1Run();
}


void Input1Run()
{
    /**/
    int Input1StateAct = 0;
    unsigned long time = millis()/1000;
    if (time==0){Input1OFFLast=0;Input1ONLast=0;}

    Input1StateAct = digitalRead(Input1Pin);

    if (Input1State!=Input1StateAct)
    {
      Input1State = Input1StateAct;
      if (Input1State == LOW)
      {
        Relay2 = "OFF" ;
        Input1OFFLast = time;
      }
      else
      { 
        Relay2 = "ON " ;
        Input1ONLast = time ;
      }
    /*Serial.println(Input1State);*/
    }
    switch(Input1State) {
      case LOW:
        if (time>(Input1OFFLast+Input1OFFDelay)){digitalWrite(pinOut2, LOW);}
        break;
      default:
        if (time>(Input1ONLast+Input1ONDelay)){digitalWrite(pinOut2, HIGH);}
        break;
    }

}

void LCDInit()
{
  /**/
  button1State = digitalRead(button1Pin);
  button2State = digitalRead(button2Pin);
  button3State = digitalRead(button3Pin);
  button4State = digitalRead(button4Pin);

  if ((button1State == LOW) || (button2State == LOW) || (button3State == LOW) || (button4State == LOW)) {
    if (OPTime > 200) {button1State = HIGH;}
    OPTime = 0;
    delay(100);
  }

  if (button1State == LOW) {

    delay(150);
    OPSelc = OPSelc + 1;
    /*if (OPSelc == 6){OPSelc = 1;}
    /**/
    OPMax = 100.0;
    OPMin = 0.0;
    OPInc = 1;

    switch(OPSelc) {
/*      case 1:
        OPVal = TempMIN;
        OPMax = 100.0;
        OPMin = 0.0;
        OPInc = 0.5;
        break;*/
      case 2:
        OPMax = 10.0;
        OPMin = 1.0;
        OPVal = TempDH;
        OPInc = 0.1;
        break;
      case 3:
        OPMax = 1.0;
        OPMin = 0.1;
        OPVal = TempDL;
        OPInc = 0.1;
        break;
      case 4:
        OPVal = TempMAX;
        break;
      case 5:
        OPVal = Input1ONDelay;
        break;
      case 6:
        OPVal = Input1OFFDelay;
        break;
      default:
        OPSelc = 1;
        OPVal = TempMIN;
        break;
    }
  }  
}

void LCDMenu() 
{
    if (button4State == LOW) {
      SaveOption() ;
    }
    else{
      ChangeOption() ;

      lcd.setCursor ( 0, 0 );
      switch(OPSelc) {
        case 1:
          lcd.print("MINIMUM TEMPERATURE");
          break;
        case 2:
          lcd.print("MINIMUM DIFFERENCE");
          break;
        case 3:
          lcd.print("OFF DIFFERENCE    ");
          break;
        case 4:
          lcd.print("MAX TEMPERATURE   ");
          break;
        case 5:
          lcd.print("SWITCH ON DELAY   ");
          break;
        case 6:
          lcd.print("SWITCH OFF DELAY  ");
          break;
        case 7:
          lcd.print("SWITCH TIMER      ");
          break;
      }
      lcd.setCursor ( 0, 1 );
      lcd.print("        " + String(OPVal,1) + "    ");
    }

    if (OPTime >200) {
      OPTime = 0;
      OPSelc = 0;
      lcd.clear();
    }
}

void ChangeOption()
{
  if (button3State == LOW) {
    delay(150);
    OPVal = OPVal + OPInc;
    if ((OPVal > OPMax) || (OPVal < OPMin)){OPVal = OPMin;}
  }
  else {
    if (button2State == LOW) {
      delay(150);
      OPVal = OPVal - OPInc;
      if ((OPVal < OPMin)|| (OPVal > OPMax)){OPVal = OPMax;}
    }
    else {OPTime = OPTime + 1;}
  }
}

void SaveOption()
{
  switch(OPSelc) {
    case 1:
       TempMIN = OPVal;
      break;
    case 2:
      TempDH = OPVal;
      break;
    case 3:
      TempDL = OPVal;
      break;
    case 4:
      TempMAX = OPVal;
      break;
    case 5:
      Input1ONDelay   = OPVal ;
      break;
    case 6:
      Input1OFFDelay  = OPVal ;
      break;
  }
  OPVal=OPVal*10;
  int valeeprom = OPVal;
  EEPROMWriteInt((OPSelc*10), valeeprom);

  OPSelc = 0;
  delay(150);
  lcd.clear();
}

void LCDMain()
{
    OPTime = OPTime + 1;
    lcd.setCursor ( 0, 0 );
    lcd.print("T1:");
    lcd.setCursor ( 9, 0 );
    lcd.print("T2:");
    // Command all devices on bus to read temperature 
    sensors.requestTemperatures();
  
    ProbeN=1;
    printTemperature(Probe01);
    ProbeN=2;
    printTemperature(Probe02);
  
    /**/
    if (Temp1 > TempMIN && Temp2 < TempMAX) {
      /**/
      if (TempES==0) {TempUT = TempDH;}
      else {TempUT = TempDL;}
      /**/
      TempES = (Temp1 - TempUT) - Temp2 ;
      /**/
      if (TempES > 0){TempES = 1;}
      else{TempES = 0;}
      /**/
    }
    else{TempES = 0 ;}
  
    if (TempES == 1){
      digitalWrite(pinOut1, HIGH);
      Relay1 = "ON ";
    }
    else {
      digitalWrite(pinOut1, LOW);
      Relay1 = "OFF";
    }

    /**/
    lcd.setCursor (9,1);
    lcd.print(Relay1);
    /**/

    /**/
    lcd.setCursor (13,1);
    lcd.print(Relay2);
    /**/
}


float EEPROMRead(int p_option)
{
   float p_value = EEPROMReadInt(p_option);
   p_value = p_value/10;
   return p_value;
}

void printTemperature(DeviceAddress deviceAddress)
{
  float tempC = sensors.getTempC(deviceAddress);
  if (ProbeN==1) {
    lcd.setCursor (3,0);
    Temp1 = tempC;
  }
  else {
    lcd.setCursor (12,0);
    Temp2 = tempC;
  }

  if (tempC == -127.00) {lcd.print("--.-");}
  else {lcd.print(tempC,1);}
}

/*** vizfxca *****/
/* http://forum.arduino.cc/index.php?topic=37470.0 */

void EEPROMWriteInt(int p_address, int p_value)
     {
     byte lowByte = ((p_value >> 0) & 0xFF);
     byte highByte = ((p_value >> 8) & 0xFF);

     EEPROM.write(p_address, lowByte);
     EEPROM.write(p_address + 1, highByte);
     }

unsigned int EEPROMReadInt(int p_address)
     {
     byte lowByte = EEPROM.read(p_address);
     byte highByte = EEPROM.read(p_address + 1);

     return ((lowByte << 0) & 0xFF) + ((highByte << 8) & 0xFF00);
     }

I don't understand "smaller". Do you want fewer lines of code? Then take out all the serial print lines and the line to set the serial monitor baud rate to 9600.

Do you want smaller memory usage? Why?

Paul

One thing i wanted to do was to create one array with 2 columns to store the probes address and temperatures and i am unable to do it.

Can anyone help please. Thanks.

one other thing in the code i have

button1State = digitalRead(button1Pin);

is it possibe to make this:

var1 = "button" + string(1) + "State";
var2 = "button" + string(1) + Pin";
var1 = var2;

*** edit:

or this:

command = "button" + string(1) + "State = " + "button" + string(1) + Pin";
run command;