/*BBQ Smoker Control-  May 11, 2014- Charles Hartley
This verion reads and displays two thermometers, and services two push buttons. One push button moves the set (desired) oven temperature  up and the other moves the set oven temperature down. AC relay to control oven heater is controlled by oven temperature and set temperature.

The meat temperature is stored every 15 minutes and can be displayed. The recorded meat temperature is displayed along with the time that it was recorded. By pressing and holding the memory button the up button and the down buttoncan be used to move up and down in time to see what the previously recorded meat temperatures were.

Buttons must be pressed and held for at least a second to be active.
*/

#include<LiquidCrystal.h> // include the library code for the LCD
LiquidCrystal lcd(12,11,5,4,3,2);// pin assigned to LCD
const int ovenPin = A0; // the oven thermistor input, voltage across the oven thermistor
const int meatPin = A1; // the meat thermistor input, voltate across the meat thermistor
int sensorVal;// value read from ACD
int ovenTemp;
int meatTemp;
int setTemp;// desired temperature of the oven
int deltaTemp=0;

float correction= 1.4;// calibration does not quite fit so I put in a three degree F correction.

float Kelvin;// intermediate parameter for temp calculation
float R;// intermediate parameter, resistance of the thermistor
const int downButtonPin = 6; // button to lower set temperature
const int upButtonPin = 7;// button to raise set temperature
const int memoryButtonPin = 9;//button to allow to go up and down the array to display values
int downButtonState = LOW;// neither button is pressed
int downReading;// status read from the button input
int memoryReading;
int upButtonState = LOW;
int upReading;
const int relayPin = 8;// AC relay to control oven heater element is attached to pin 8.
int relayReading;// status read from the button relay
long timeStart;// keep track of millis of start-- to keep track of elapsed time
long timeHrs;
long timeMins;
long timeSecs;
const int numberTemps = 60;// to help calculate change in oven temp over one minute period
int temp[numberTemps];// store recent meat temperatures
int tempCount = 0;// keep track of meat temperatures going into array temp[]
int tempLocation = 0;
int tempDisplay =0;
const int numberStoreLocations = 60;// to store meat temperature over 10 hour period
int storeTemp[numberStoreLocations];
int storeCount =0 ;
int storeLocation = 0;
int storeDisplay =0;
int storeMinutes[numberStoreLocations];
int deltaStoreTime = 15;
int elapsedMinutes = 0;
int lastStoreTime = -deltaStoreTime;
int displayHours;
int displayMinutes;
int i;

void setup()
{
  lcd.begin(16, 2);  // set up the LCD's number of columns and rows:
  pinMode(downButtonPin, INPUT);// assign pins to the buttons
  pinMode(upButtonPin, INPUT);
  pinMode(memoryButtonPin, INPUT);
  pinMode(relayPin, OUTPUT);// to control the relay
  setTemp = 235;
  Serial.begin(9600);// open serial port
  for ( i = 0; i< numberTemps; i++)
  {
    temp[i] = 0;
  }
  for( i = 0; i < numberStoreLocations; i++)
  {
    storeTemp[i] = 0;
    storeMinutes[i] = 0;
  }
  timeStart = millis();// time at which routine started running
}

void loop()

  sensorVal = analogRead(ovenPin);// read oven thermistor & process
  R = 2000.0/(1.0-sensorVal/1024.0);
  Kelvin = 1/(-0.0074+0.0022596*log(R) -0.0001562*log(R)*log(R)+0.0000038*log(R)*log(R)*log(R));
  ovenTemp = (int)((Kelvin -273)*9/5+32 + correction + 0.5); 
  sensorVal = analogRead(meatPin);// read meat thermistor & process
  R = 2000.0/(1.0-sensorVal/1024.0);
  Kelvin = 1/(-0.0074+0.0022596*log(R) -0.0001562*log(R)*log(R)+0.0000038*log(R)*log(R)*log(R));
  meatTemp = (int)((Kelvin -273)*9/5+32 + correction + 0.5);
  // store ovenTemp in an array to keep track of ovenTemp over last 60 seconds;
  if(tempLocation == numberTemps)
  {
    for (i = numberTemps -1; i>0; i--)
    {
      temp[i]= temp[i-1];
    }
    temp[0] = ovenTemp;
    deltaTemp = temp[0] - temp[numberTemps -1]   ;
  }
  else
  {
    temp[tempLocation] =ovenTemp;
    tempLocation ++;
  }
  //adjust set temperature or the retrieval of old temperature
  memoryReading = digitalRead(memoryButtonPin);
  downReading = digitalRead(downButtonPin);// get the status of the buttons
  upReading = digitalRead(upButtonPin);
  if(memoryReading == HIGH)
  {
    // adjust memory display
     if((upReading == HIGH) && (storeDisplay < numberStoreLocations -1))
    {
      storeDisplay ++;
    }
    if((downReading == HIGH) && (storeDisplay > 0))
    {
      storeDisplay --;
    }
  }
  else
  {
    // adjust oven temp setting
    if(downReading == HIGH)
    {
      setTemp = setTemp - 5;
    }   
    if(upReading == HIGH)
    {
      setTemp = setTemp + 5;
    }
  }
  //calculate elapsed time;
  int sixty = 60;
  timeSecs = (millis() - timeStart)/1000;
  timeMins = (int)(timeSecs/sixty);
  elapsedMinutes = timeMins;// to keep track of time for storing meat temp every so often
  timeSecs = timeSecs -timeMins*sixty;
  timeHrs = (int)(timeMins/sixty);
  timeMins = timeMins- timeHrs*sixty;
  //store meat temp every so often
  if(elapsedMinutes >= lastStoreTime + deltaStoreTime)
  {
    //store a new meat temp
    if(storeLocation < numberStoreLocations)
    {
      //if array not full
      storeTemp[storeLocation] = meatTemp;// store the meat temperature
      storeMinutes[storeLocation] = elapsedMinutes;
      lastStoreTime = elapsedMinutes;
      storeLocation ++;
    }
  }    
  //display some stuff
  lcd.clear();// adds a little flicker but that's O.K.
 
lcd.setCursor(0, 0);// column 0, row 0; top row
  lcd.print(ovenTemp);// oven temp. is the probe on the right next to the buttons
  lcd.setCursor(3, 0);// column, row
  lcd.print("/");
  lcd.print(setTemp);
  lcd.setCursor(8, 0);// column, row
  lcd.print(meatTemp);// meat temp. is the probe on the left away from the buttons
  lcd.setCursor(0,1);// bottom row
  lcd.print(timeHrs);
  lcd.print(":");
   if(timeMins < 10)
  {
    lcd.print("0");
  }
  lcd.print(timeMins);
  lcd.print(":");
  if(timeSecs < 10)
  {
    lcd.print("0");
  }
  lcd.print(timeSecs);
  lcd.setCursor(11,1);//print time of stored meat temp
  displayHours = (int)(storeMinutes[storeDisplay]/60);
  displayMinutes = storeMinutes[storeDisplay] -60*displayHours;
  lcd.print(displayHours);
  lcd.print(":");
  if(displayMinutes < 10)
  {
    lcd.print("0");
  }
  lcd.print(displayMinutes);
  lcd.setCursor(13,0);
  lcd.print(storeTemp[storeDisplay]);
  // turn oven ON or OFF
  if( ovenTemp > setTemp || (ovenTemp > setTemp - 20  && deltaTemp > 3))
  {
    digitalWrite(relayPin, LOW);
  }
  else
  {   
    digitalWrite(relayPin, HIGH);
  }
  delay(990);
}// close loop()

Back to the Project Description