#include <Wire.h> // Library for I2C communication
#include <LiquidCrystal_I2C.h> // Library for LCD
#include <Adafruit_ILI9341.h>
// Wiring: SDA pin is connected to A4 and SCL pin to A5.
// Connect to LCD via I2C, default address 0x27 (A0-A2 not jumpered)
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 16, 2); // Change to (0x27,20,4) for 20x4 LCD.
int pitprobe1;
int meatprobe1;
int probestate1;// On/Off states of the meat probes.
int probestate2;// On/Off states of the meat probes.
int BBQFAN;
/* Temperature Threshold */
int targetTempBBQPROBE = 100;
int targetTempMEATPROBE1 = 100;
int buttonpin1 = 0; // the number of the pushbutton pin
int buttonpin2 = 1; // the number of the pushbutton pin
int buttonpin3 = 2; // the number of the pushbutton pin
int buttonone = 0; // A filtered conditioned button state used for direct control.
int buttontwo = 0;
int buttonthree = 0;
int onestatelast = 1;
int twostatelast = 1;
int threestatelast = 1;
int dt=100;
int screen = 1;
int WhichScreen = 1;
boolean hasChanged = true;
/* Steinhart-Hart coefficients*/
#define COEFFICIENTA -0.000099646
#define COEFFICIENTB 0.000352239
#define COEFFICIENTC -0.000000257
#define NUMSAMPLES 3 // The number of samples in a row to take
/* Volatge Divider values */
#define SERIESRESISTOR 10000
float celsius;
int samples[NUMSAMPLES];
int THERMISTORPIN;
/* Pins */
int PitProbePin = A0;
int MeatProbe1Pin = A1;
//TFT
#define TFT_CS 9
#define TFT_DC 10
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
#define WIDTH 320
#define HEIGHT 240
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(buttonpin1, INPUT_PULLUP);
pinMode(buttonpin2, INPUT_PULLUP);
pinMode(buttonpin3, INPUT_PULLUP);
tft.begin();
tft.setRotation(1);
tft.setCursor(26, 120);
tft.setTextColor(ILI9341_RED);
tft.setTextSize(5);
tft.println("Hello, TFT!");
tft.setCursor(20, 160);
tft.setTextColor(ILI9341_GREEN);
tft.setTextSize(5);
tft.println("I can has colors?");
// Initiate the LCD:
lcd.init();
lcd.backlight();
lcd.home();
lcd.setCursor(0,0);
String message = "BBQ VENTILATOR";
for (byte i = 0; i < message.length(); i++) {
lcd.print(message[i]);
delay(100);
}
lcd.setCursor(0, 1);
String message1 = "AND TEMP PROBE";
for (byte i = 0; i < message1.length(); i++) {
lcd.print(message1[i]);
delay(100);
}
delay(1000);
lcd.clear();
screen = 0;
}
void loop() {
int onestate=digitalRead(buttonpin1);
if(onestatelast==0 && onestate==1){
if (buttonone==0){
hasChanged = true;
WhichScreen++;
if (WhichScreen > 6) {
WhichScreen = 1;
}
}
}
onestatelast=onestate;
if (hasChanged == true) {
hasChanged = false;
switch (WhichScreen) {
case 1:
screen = 1;
lcd.clear();
break;
case 2:
screen = 2;
lcd.clear();
break;
case 3:
screen = 3;
lcd.clear();
break;
case 4:
screen = 4;
lcd.clear();
break;
case 5:
screen = 5;
lcd.clear();
break;
case 6:
screen = 6;
lcd.clear();
break;
case 0:
break;
}
}
/*
int twostate=digitalRead(buttonpin2);
if(twostatelast==0 && twostate==1){
if (buttontwo==0){
buttontwo=1;
delay(dt);
}else{
buttontwo=0;
delay(dt);
}
}
twostatelast=twostate;
int threestate=digitalRead(buttonpin3);
if(threestatelast==0 && threestate==1){
if (buttonthree==0){
buttonthree=1;
delay(dt);
}else{
buttonthree=0;
delay(dt);
}
}
threestatelast=threestate;
*/
int twostate=digitalRead(buttonpin2);
if(twostate != twostatelast){
if (twostate== LOW){
if (screen == 2){
probestate1 = 1;
}
if (screen == 3){
probestate2 = 1;
}
if (screen == 4){
BBQFAN = 1;
}
if (screen == 5){
targetTempBBQPROBE = targetTempBBQPROBE + 10;
delay(dt);
}
if (screen == 6){
targetTempMEATPROBE1 = targetTempMEATPROBE1 + 10;
delay(dt);
}
}
delay(dt);
}
twostatelast=twostate;
int threestate=digitalRead(buttonpin3);
if(threestate != threestatelast){
if (threestate== LOW){
if (screen == 2){
probestate1 = 0;
}
if (screen == 3){
probestate2 = 0;
}
if (screen == 4){
BBQFAN = 0;
}
if (screen == 5){
targetTempBBQPROBE = targetTempBBQPROBE - 10;
delay(dt);
}
if (screen == 6){
targetTempMEATPROBE1 = targetTempMEATPROBE1 - 10;
delay(dt);
}
}
delay(dt);
}
threestatelast=threestate;
if (screen == 1){
if (probestate1 == 1){//BBQ PROBE 1
int pitprobe1 = tempRead(PitProbePin);
lcd.setCursor(0,0);
lcd.print("BBQ PROBE:");
lcd.setCursor(13,0);
lcd.print(pitprobe1 );
} else {
lcd.setCursor(0,0);
lcd.print("BBQ PROBE:");
lcd.setCursor(13,0);
lcd.print(pitprobe1 );
int pitprobe1 = 0;
}
if (probestate2 == 1){//MEAT PROBE 1
int meatprobe1 = tempRead(MeatProbe1Pin);
lcd.setCursor(0,1);
lcd.print("MEAT PROBE:");
lcd.setCursor(13,1);
lcd.print(meatprobe1 );
} else {
lcd.setCursor(0,1);
lcd.print("MEAT PROBE:");
lcd.setCursor(13,1);
lcd.print(meatprobe1 );
int meatprobe1 = 0;
}
}
if (screen == 2){
LCDTEMPDisplay2 ();
}
if (screen == 3){
LCDTEMPDisplay3 ();
}
if (screen == 4){
LCDTEMPDisplay4 ();
}
if (screen == 5){
LCDTEMPDisplay5 ();
}
if (screen == 6){
LCDTEMPDisplay6 ();
}
}
void LCDTEMPDisplay2 (){
if (probestate1 == 1){
lcd.setCursor(0,0);
lcd.print("BBQ PROBE:");
lcd.setCursor(13,0);
lcd.print(" ON");
}else{
lcd.setCursor(0,0);
lcd.print("BBQ PROBE:");
lcd.setCursor(13,0);
lcd.print("OFF");
}
}
void LCDTEMPDisplay3(){
if (probestate2 == 1){
lcd.setCursor(0,0);
lcd.print("MEAT PROBE:");
lcd.setCursor(13,0);
lcd.print(" ON");
}else{
lcd.setCursor(0,0);
lcd.print("MEAT PROBE:");
lcd.setCursor(13,0);
lcd.print("OFF");
}
}
void LCDTEMPDisplay4(){
if (BBQFAN == 1){
lcd.setCursor(0,0);
lcd.print("BBQ FAN:");
lcd.setCursor(13,0);
lcd.print(" ON");
}else{
lcd.setCursor(0,0);
lcd.print("BBQ FAN:");
lcd.setCursor(13,0);
lcd.print("OFF");
}
/*
if (pitprobe1 == targetTempBBQPROBE) { // BLOWER OFF
lcd.setCursor(0,1);
lcd.print("BLOWER OFF");
} else if (pitprobe1 >= targetTempBBQPROBE - 10) { // BLOWER OFF
lcd.setCursor(0,1);
lcd.print("BLOWER OFF");
} else if (pitprobe1 >= targetTempBBQPROBE - 35) { // BLOWER ON AT HALF SPEED
lcd.setCursor(0,1);
lcd.print("BLOWER ON: AT HALF SPEED");
} else if (pitprobe1 <= targetTempBBQPROBE - 35){// BLOWER ON AT FULL SPEED
lcd.setCursor(0,1);
lcd.print("BLOWER ON: AT FULL SPEED");
}*/
}
void LCDTEMPDisplay5(){
lcd.setCursor(0,0);
lcd.print("SET TARGET ");
lcd.setCursor(0,1);
lcd.print("BBQ PROBE:");
lcd.setCursor(13,1);
lcd.print(targetTempBBQPROBE);
lcd.print(" C ");
}
void LCDTEMPDisplay6(){
lcd.setCursor(0,0);
lcd.print("SET TARGET");
lcd.setCursor(0,1);
lcd.print("MEAT PROBE:");
lcd.setCursor(13,1);
lcd.print(targetTempMEATPROBE1);
lcd.print(" C ");
}
/****** Calculate Temperature ******/
int tempRead(int THERMISTORPIN){
uint8_t i;
float Resistance;
// take N samples in a row, with a slight delay
for (i=0; i< NUMSAMPLES; i++) {
samples[i] = analogRead(THERMISTORPIN);
delay(10);
}
// average all the samples out
Resistance = 0;
for (i=0; i< NUMSAMPLES; i++) {
Resistance += samples[i];
}
Resistance /= NUMSAMPLES;
// convert the value to resistance
Resistance = 1023 / Resistance - 1;
Resistance = SERIESRESISTOR / Resistance;
float steinhart;
// Temperature in Kelvin = 1 / {A + B[ln(R)] + C[ln(R)]^3} Steinhart–Hart equation
steinhart = log(Resistance);
steinhart = 1 / (COEFFICIENTA + (COEFFICIENTB * steinhart) + (COEFFICIENTC * steinhart * steinhart * steinhart));
celsius = steinhart - 273.15; // convert to C
return celsius;
}