#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#include <Button.h>
//-------- INPUTS --------
#define encApin 2
#define encBpin 3
#define encSWpin 4
#define exhaustBlowerSnapDiscTempPin 6
#define housingSnapDiscTempPin 7
#define spareInputPin 8
#define roomTempPin A0
#define fireBoxPin A1
//-------- OUTPUTS --------
#define augerPin 9
#define exhastPin 10
#define airBlowerPin 11
#define ignitorPin 12
Button exhaustTemp(exhaustBlowerSnapDiscTempPin); // Connect your button between pin 2 and GND
Button housingTemp(housingSnapDiscTempPin);
Button spareInput(spareInputPin);
Button encoderButton(encSWpin);
bool encoderChanged = false;
//-------- CONSTANTS --------
const float BETA = 3950; // should match the Beta Coefficient of the thermistor
long encoderPosition = 0;
long oldPosition = -999;
bool lastClk = HIGH;
//-------- define vars for testing menu --------
byte currentMenu = 1;
byte nextMenu = 0;
const int timeout = 10000; //define timeout of 10 sec
char mainMenu = 0;
long time0;
bool rotaryEncIButtonVal;
//=====================================================================================================
void setup() {
//-------- LCD --------
lcd.init();
lcd.backlight();
//-------- OUTPUTS --------
pinMode(augerPin, OUTPUT);
pinMode(exhastPin, OUTPUT);
pinMode(airBlowerPin, OUTPUT);
pinMode(ignitorPin, OUTPUT);
//-------- INPUTS --------
exhaustTemp.begin();
housingTemp.begin();
spareInput.begin();
encoderButton.begin();
//-------- SERIAL --------
while (!Serial) { }; // for Leos
Serial.begin(9600);
//-------- Encoder --------
pinMode(encSWpin, INPUT_PULLUP);
pinMode(encApin, INPUT);
pinMode(encBpin, INPUT);
attachInterrupt(digitalPinToInterrupt(encApin), readEncoder, FALLING);
encoderPosition = 1;
//-------- MENU --------
//mainMenu = menu();
}
//=====================================================================================================
void readEncoder() {
int dtValue = digitalRead(encBpin);
if (dtValue == HIGH) {
//Serial.println("Rotated clockwise ⏩");
encoderPosition++;
}
if (dtValue == LOW) {
//Serial.println("Rotated counterclockwise ⏪");
encoderPosition--;
}
}
//=====================================================================================================
void loop() {
if (encoderPosition != oldPosition) {
oldPosition = encoderPosition;
Serial.print("encoder: "); Serial.println(encoderPosition);
encoderChanged = true;
}
int roomTempValue = analogRead(roomTempPin);
int fireBoxValue = analogRead(fireBoxPin);
float roomCelsius = (1 / (log(1 / (1023. / roomTempValue - 1)) / BETA + 1.0 / 298.15) - 273.15);
float roomFahrenheit = roomCelsius * 9 / 5 + 32;
float fireBoxCelsius = (1 / (log(1 / (1023. / fireBoxValue - 1)) / BETA + 1.0 / 298.15) - 273.15);
float fireBoxFahrenheit = fireBoxCelsius * 9 / 5 + 32;
/*
lcd.setCursor(2,0);
lcd.print(roomCelsius,1);
lcd.print("c/");
lcd.print(roomFahrenheit,1);
lcd.print("f");
lcd.setCursor(2,1);
lcd.print(fireBoxCelsius,1);
lcd.print("c/");
lcd.print(fireBoxFahrenheit,1);
lcd.print("f");
delay(500);
lcd.clear();
if (exhaustTemp.pressed())
Serial.println("exhaustTemp");
if (housingTemp.released())
Serial.println("housingTemp released");
if (spareInput.toggled()) {
if (spareInput.read() == Button::PRESSED)
Serial.println("spareInput has been pressed");
else
Serial.println("spareInput has been released");
}
*/
mainMenu = encoderPosition;
if (encoderButton.pressed()) {
nextMenu = encoderPosition;
encoderPosition = 1;
}
//-------------------------------- MENU ----------------------------------------
if (encoderChanged) {
if (currentMenu == 1) {
switch (mainMenu) {
case 1:
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(">Menu 1");
lcd.setCursor(0, 1);
lcd.print(" Menu 2");
break;
case 2:
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" Menu 1");
lcd.setCursor(0, 1);
lcd.print(">Menu 2");
break;
case 3:
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" Menu 2");
lcd.setCursor(0, 1);
lcd.print(">Menu 3");
break;
} // end of main menu
encoderChanged = false;
}
}
}
//=====================================================================================================
// Menu function for selecting the components to be tested
// Follow serial monitor for instrcutions
char menu() {
Serial.println(F("\nWhich component would you like to test?"));
Serial.println(F("(1) LCD Display 20x4 I2C"));
Serial.println(F("(2) Rotary Encoder Module"));
Serial.println(F("(3) Menu Test"));
while (!Serial.available());
// Read data from serial monitor if received
while (Serial.available()) {
char c = Serial.read();
if (isAlphaNumeric(c)) {
if (c == '1')
Serial.println(F("Now Testing LCD Display 20x4 I2C"));
else if (c == '2')
Serial.println(F("Now Testing Rotary Encoder Module"));
else if (c == '3')
Serial.println(F("Now Testing Menu System"));
else {
Serial.println(F("illegal input!"));
return 0;
}
time0 = millis();
return c;//c menu counter, copy that code
}
}
}