#include <Wire.h> // deals with I2C connections
#include "SPI.h"
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "Adafruit_ILI9341.h"
#include <avr/pgmspace.h>
#define TFT_CS 9
#define TFT_DC 10
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
#define WIDTH 320
#define HEIGHT 240
const int downButtonPin = 5;
const int selectButtonPin = 6;
const int clearButtonPin = 7;
int cursorPosition = 0;
// BUTTON PRESS STATES FOR EACH FUNCTION, ALL SET TO "LOW".
// MAKE THESE "BOOLEAN" VARIABLES AS THESE ONLY WILL BE "HIGH" OR "LOW".
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;
// DEBOUNCE VARIABLES TO MEASURE THE DEBOUNCING TIME OF A BUTTON PUSH.
// MAKE THESE "UNSIGNED LONG" VARIABLES AS THE NUMERICAL VALUE WILL HAVE AN EXTENDED SIZE.
unsigned long lastDebounceTime = 0; //takes snapshot of time when button is pressed
unsigned long debounceDelay = 200; //delay time in milliseconds
//setup the lcd, whatever is connected to the arduino
void setup() {
//setup the lcd, turn on and clear
Serial.begin(9600);
tft.begin();
delay(2000);
tft.fillScreen(ILI9341_BLACK);
//set button pins as inputs
pinMode(downButtonPin, INPUT);
pinMode(selectButtonPin, INPUT);
pinMode(clearButtonPin, INPUT);
delay(1000);
}
void loop() {
//again set up lcd and serial monitor
//create an arrow system
tft.fillScreen(ILI9341_BLACK);
tft.setTextColor(SSD1306_WHITE);
tft.setTextSize(0.5);
tft.setCursor(0,0); // Menu displayed on the LCD.
tft.println("Temperature ");
tft.println("Pressure");
tft.println("Gas");
tft.println("Humidity"); // The menuOption that is displayed is determined by the left or right push.
tft.setCursor(80, cursorPosition);
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 + 8; //for ours will have arrow go down the list
}
if (cursorPosition > 24) //resets menu, goes back to top of list
{
cursorPosition = 0;
}
delay(3000);
tft.fillScreen(ILI9341_BLACK);
}
//select button press
if (currentButtonStateSelect != buttonStateSelect)
{
buttonStateSelect = currentButtonStateSelect;
if (buttonStateSelect == LOW && cursorPosition == 0)
{
//clear lcd, set cursor
//print the option
Serial.println("Enter Pressed");
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(0,0);
tft.println("TEMPERATURE");
tft.println(">");
delay(2000);
}
if (buttonStateSelect == LOW && cursorPosition == 8)
{
//clear lcd, set cursor
//print the option
Serial.println("Enter Pressed again");
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(0,0);
tft.println("PRESSURE");
tft.println("IS ENTERED");
delay(2000);
}
if (buttonStateSelect == LOW && cursorPosition == 16)
{
//clear lcd, set cursor
//print the option
Serial.println("Enter Pressed for the 3rd time");
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(0,0);
tft.println("GAS");
tft.println("IS ENTERED");
delay(2000);
}
if (buttonStateSelect == LOW && cursorPosition == 24)
{
//clear lcd, set cursor
//print the option
Serial.println("Enter Pressed for 4th time");
Serial.println("62 Humidity");
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(0,0);
tft.println("Humidity");
tft.println("IS ENTERED");
delay(2000);
}
//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");
tft.fillScreen(ILI9341_BLACK); // The press counts for both the up and down variables are also reset to zero.
tft.setCursor(0,0);
tft.println("MENU");
tft.println("IS CLEARED");
delay(2000);
}
delay(2000);
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
}