#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME680.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <SPI.h>
//#include <SD.h>
//TFT LCD Setup
#define TFT_CS 10
#define TFT_DC 9
#define SCREEN_WIDTH 320
#define SCREEN_HEIGHT 240
//BME680 Setup
Adafruit_BME680 bme;
const int downButtonPin = 5;
const int selectButtonPin = 6;
const int clearButtonPin = 7;
int cursorPosition = 0;
//go down the list of options
boolean buttonStateDown = LOW;
boolean lastButtonStateDown = LOW;
boolean currentButtonStateDown = LOW;
//select an menu option
boolean buttonStateSelect = LOW;
boolean lastButtonStateSelect = LOW;
boolean currentButtonStateSelect = LOW;
//clear button, will reset the menu back to the start
boolean buttonStateClear = LOW;
boolean lastButtonStateClear = LOW;
boolean currentButtonStateClear = LOW;
// measure the debouncing time of a button push
// unsigned long as they have a larger size, in milliseconds the numbers can get quite large
unsigned long lastDebounceTime = 0; //takes snapshot of time when button is pressed
unsigned long debounceDelay = 200; //delay time in milliseconds
//boolean menuSelected = false;
// SD Card Setup
const int chipSelect = 4; // chip select pin for the SD card module
String Temperature, Pressure, Humidity, Gas, Data; //for the SDcard File
//File dataFile; // File object to store sensor data
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
void setup() {
//setup the LCD
tft.begin();
tft.setRotation(3); // adjust the rotation of screen
tft.fillScreen(ILI9341_BLACK); //used to reset the screen
//set button pins as inputs
pinMode(downButtonPin, INPUT_PULLUP);
pinMode(selectButtonPin, INPUT_PULLUP);
pinMode(clearButtonPin, INPUT_PULLUP);
bme.setTemperatureOversampling(BME680_OS_8X);
bme.setPressureOversampling(BME680_OS_8X);
bme.setHumidityOversampling(BME680_OS_2X);
bme.setGasHeater(320,150);
/*if (!bme.begin(0x76)) {
Serial.println("Could not find a valid BME680 sensor, check wiring!");
while (1);
}*/
// SD card initialization
/*if (!SD.begin(chipSelect)) {
Serial.println("SD card initialization failed!");
while (1);
}
dataFile = SD.open("datalog.txt", FILE_WRITE);
if (dataFile){
dataFile.println("Temperature(°C), Pressure(Pa), Gas(ohms), Humidity(%) \r\n");
dataFile.close();
}
else {
Serial.println("error opening data.txt");
}
*/
// Set the temperature, pressure and humidity and gas resistance oversampling
}
void loop() {
Serial.begin(9600);
//create an arrow system
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(3);
tft.setCursor(14, 0); // Menu displayed on the LCD.
tft.println("Temperature");
tft.println(" Pressure");
tft.println(" Gas");
tft.println(" Humidity"); //menu selection going down
tft.setCursor(270, cursorPosition); //changes cursor position each time the button is pressed
tft.print(">");
//wait for an input
currentButtonStateDown = digitalRead(downButtonPin);
currentButtonStateSelect = digitalRead(selectButtonPin);
currentButtonStateClear = digitalRead(clearButtonPin);
if (currentButtonStateDown != lastButtonStateDown || currentButtonStateSelect != lastButtonStateSelect || currentButtonStateClear != lastButtonStateClear)
// If there is a button push on any of the buttons, the following routine runs to check if it was a valid press:
{
lastDebounceTime = millis(); // lastDebounceTime is set equal to the running millis() function.
}
if ((millis() - lastDebounceTime) > debounceDelay)
/* If the lastDebounceTime (aka. the "snapshot" time) minus the running millis() function is higher than the set debounce delay, the following routine
below runs and checks which button was pushed:*/
{
//down button press
if (currentButtonStateDown != buttonStateDown)
{
buttonStateDown = currentButtonStateDown;
if (buttonStateDown == LOW)
{
Serial.println("Down Button Pressed");
cursorPosition = cursorPosition + 28; //for ours will have arrow go down the list
}
if (cursorPosition > 84) { //resets menu, goes back to top of list
cursorPosition = 0;
}
delay(300);
tft.fillScreen(ILI9341_BLACK);
}
//select button press
if (currentButtonStateSelect != buttonStateSelect)
{
buttonStateSelect = currentButtonStateSelect;
if (buttonStateSelect == LOW) {
if (cursorPosition == 0){
Serial.println("Enter Pressed");
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(14, 10);
tft.println("");
tft.println("TEMPERATURE : ");
tft.print(bme.temperature);
tft.print(" *C");
delay(1000);
//logSensorData("Temperature", bme.temperature);
//menuSelected = true;
//logSensorData("Temperature", bme.temperature);
} else if (cursorPosition == 28) {
//clear LCD, set cursor
//print the option
Serial.println("Enter Pressed again");
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(14, 10);
tft.println("");
tft.println("PRESSURE : ");
tft.print(bme.pressure);
tft.print(" Pa");
delay(1000);
//logSensorData("Pressure", bme.pressure);
//menuSelected = true;
} else if (cursorPosition == 56) {
//clear LCD, set cursor
//print the option
Serial.println("Enter Pressed for the 3rd time");
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(14, 10);
tft.println("");
tft.println("GAS : ");
tft.print(bme.gas_resistance);
tft.print(" Ohms");
delay(1000);
//logSensorData("Gas", bme.gas_resistance);
//menuSelected = true;
} else if (cursorPosition == 84) {
//clear LCD, set cursor
//print the option
Serial.println("Enter Pressed for 4th time");
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(10, 10);
tft.println("");
tft.println("Humidity : ");
tft.print(bme.humidity);
tft.print(" %");
delay(1000);
//logSensorData("Humidity", bme.humidity);
//menuSelected = true;
}
//clear the LCD
tft.fillScreen(ILI9341_BLACK);
}
}
// CLEAR BUTTON PRESS
if (currentButtonStateClear != buttonStateClear)
{
buttonStateClear = currentButtonStateClear;
if (buttonStateClear == LOW)
{
//clear LCD, set the cursor
Serial.println("Cleared Menu");
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(10, 10);
tft.println("MENU IS CLEARED");
delay(2000);
cursorPosition = 0;
//menuSelected = false;
}
tft.fillScreen(ILI9341_BLACK);
}
}
// After a button is pushed and the count recorded, all the states reset back to LOW for the data to be processed correctly.
lastButtonStateDown = currentButtonStateDown; // resets the down button state to LOW
lastButtonStateSelect = currentButtonStateSelect; // resets the enter button state to LOW
lastButtonStateClear = currentButtonStateClear; // resets the clear button state to LOW
//attempt at making the menu stay when option is selected
/* if (menuSelected) {
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(10, 10);
tft.println("Option Selected");
delay(2000);
}*/
}
/*need to create a boolean function for the debounce time, as the delays in the main loop are
messing up the timer
//logs data into SDCardFile, eg. logSensorData("Humidity", bme.humidity)
void logSensorData(const String& dataType, float value){
myFile = SD.open("data.txt", FILE_WRITE);
if (myFile) {
dataFile.print(dataType);
dataFile.print(": ");
dataFile.println(value);
dataFile.close();
Serial.println("Sensor data logged to SD card");
} else {
Serial.println("Error opening datalog.txt");
}
}*/