#include <Wire.h>
#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 menuOptionCount = 4;
const int downButtonPin = 5;
const int selectButtonPin = 6;
const int clearButtonPin = 7;
int selectedOption = 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;
// 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() {
// Display the menu and handle button presses
displayMenu();
handleButtons();
delay(2000);
}
void displayMenu() {
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
display.setTextSize(0.5);
for (int i = 0; i < menuOptionCount; i++) {
display.setCursor(0, i * 8);
display.print(menuOption[i]);
}
display.drawTriangle(120, selectedOption * 8, 120, selectedOption * 8 + 5, 125, selectedOption * 8 + 2, SSD1306_WHITE);
display.display();
}
void handleButtons() {
currentButtonStateDown = digitalRead(downButtonPin);
if (currentButtonStateDown != lastButtonStateDown) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (currentButtonStateDown == LOW) {
selectedOption++;
if (selectedOption >= menuOptionCount) {
selectedOption = 0;
}
}
}
// Update last button states only when the down button is pressed
if (currentButtonStateDown == LOW) {
lastButtonStateDown = currentButtonStateDown;
}
}