#include <Wire.h> // deals with I2C connections
#include <LiquidCrystal_I2C.h> // activates the LCD I2C library
String menuOption[] = {{"Temperature"},{"Pressure"},{"Gas"},{"Humidity"}};
const int downButtonPin = 5;
const int selectButtonPin = 6;
const int clearButtonPin = 7;
int directionPush = 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 = 50; //delay time in milliseconds
//setup the lcd, whatever is connected to the arduino
LiquidCrystal_I2C lcd(0x27, 16, 2); // Activates the LCD "object"
void setup() {
//setup the lcd, turn on and clear
lcd.backlight(); // THESE COMMANDS TURN ON AND CLEAR THE LCD SCREEN
lcd.init();
lcd.clear();
//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
lcd.setCursor(0,0); // Menu displayed on the LCD.
lcd.print(menuOption[directionPush]); // The menuOption that is displayed is determined by the left or right push.
//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 )
{
directionPush++; //for ours will have arrow go down the list
}
if (directionPush > 4) //resets menu, goes back to top of list
{
directionPush = 0;
}
lcd.clear();
}
//select button press
if (currentButtonStateSelect != buttonStateSelect)
{
buttonStateSelect = currentButtonStateSelect;
if (buttonStateSelect == LOW && directionPush == 0)
{
//clear lcd, set cursor
//print the option
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Temperature");
lcd.setCursor(0,1);
lcd.print("IS ENTERED");
delay(2000);
}
if (buttonStateSelect == LOW && directionPush == 1)
{
//clear lcd, set cursor
//print the option
Serial.println("82 Pressure");
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Pressure");
lcd.setCursor(0,1);
lcd.print("IS ENTERED");
delay(2000);
}
if (buttonStateSelect == LOW && directionPush == 2)
{
//clear lcd, set cursor
//print the option
Serial.println("92 Gas");
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Gas");
lcd.setCursor(0,1);
lcd.print("IS ENTERED");
delay(2000);
}
if (buttonStateSelect == LOW && directionPush == 3)
{
//clear lcd, set cursor
//print the option
Serial.println("62 Humidity");
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Humidity");
lcd.setCursor(0,1);
lcd.print("IS ENTERED");
delay(2000);
}
//clear the lcd
lcd.clear();
}
// CLEAR BUTTON PRESS
if (currentButtonStateClear != buttonStateClear)
{
buttonStateClear = currentButtonStateClear;
if (buttonStateClear == LOW)
{
//clear lcd, set the cursor
lcd.clear(); // The press counts for both the up and down variables are also reset to zero.
lcd.setCursor(0,0);
lcd.print("MENU");
lcd.setCursor(0,1);
lcd.print("IS CLEARED");
delay(2000);
}
lcd.clear();
}
}
// 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
}