#include <Wire.h> // deals with I2C connections
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <avr/pgmspace.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 32
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 = 200; //delay time in milliseconds
//setup the lcd, whatever is connected to the arduino
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
void setup() {
//setup the lcd, turn on and clear
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.display();
delay(2000);
display.clearDisplay();
//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
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
display.setTextSize(0.5);
display.setCursor(0,0); // Menu displayed on the LCD.
display.println(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;
}
display.display();
delay(3000);
display.clearDisplay();
}
//select button press
if (currentButtonStateSelect != buttonStateSelect)
{
buttonStateSelect = currentButtonStateSelect;
if (buttonStateSelect == LOW && directionPush == 0)
{
//clear lcd, set cursor
//print the option
display.clearDisplay();
display.setCursor(0,0);
display.println("TEMPERATURE");
display.println("IS ENTERED");
display.display();
delay(2000);
}
if (buttonStateSelect == LOW && directionPush == 1)
{
//clear lcd, set cursor
//print the option
display.clearDisplay();
display.setCursor(0,0);
display.println("PRESSURE");
display.println("IS ENTERED");
display.display();
delay(2000);
}
if (buttonStateSelect == LOW && directionPush == 2)
{
//clear lcd, set cursor
//print the option
display.clearDisplay();
display.setCursor(0,0);
display.println("GAS");
display.println("IS ENTERED");
display.display();
delay(2000);
}
if (buttonStateSelect == LOW && directionPush == 3)
{
//clear lcd, set cursor
//print the option
Serial.println("62 Humidity");
display.clearDisplay();
display.setCursor(0,0);
display.println("Humidity");
display.println("IS ENTERED");
display.display();
delay(2000);
}
//clear the lcd
display.clearDisplay();
}
// CLEAR BUTTON PRESS
if (currentButtonStateClear != buttonStateClear)
{
buttonStateClear = currentButtonStateClear;
if (buttonStateClear == LOW)
{
//clear lcd, set the cursor
display.clearDisplay(); // The press counts for both the up and down variables are also reset to zero.
display.setCursor(0,0);
display.println("MENU");
display.println("IS CLEARED");
display.display();
delay(2000);
}
display.display();
delay(2000);
display.clearDisplay();
}
}
// 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
}