#include "ButtonFunction.h"
#include "menu.h"
// Button pins
#define BUTTON_LEFT 4
#define BUTTON_RIGHT 12
#define BUTTON_UP 13
#define BUTTON_DOWN 14
#define BUTTON_ENTER 27
void setup() {
Serial.begin(9600);
// Initialize the display
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;)
;
}
display.display();
delay(2000);
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
//Initialize the button to input
pinMode(BUTTON_LEFT, INPUT_PULLUP);
pinMode(BUTTON_RIGHT, INPUT_PULLUP);
pinMode(BUTTON_UP, INPUT_PULLUP);
pinMode(BUTTON_DOWN, INPUT_PULLUP);
pinMode(BUTTON_ENTER, INPUT_PULLUP);
//Call interrupt when button will press
attachInterrupt(digitalPinToInterrupt(BUTTON_LEFT), handleLeftButtonPress, FALLING); // Attach interrupt on falling edge
attachInterrupt(digitalPinToInterrupt(BUTTON_RIGHT), handleRightButtonPress, FALLING);
attachInterrupt(digitalPinToInterrupt(BUTTON_UP), handleUpButtonPress, FALLING);
attachInterrupt(digitalPinToInterrupt(BUTTON_DOWN), handleDownButtonPress, FALLING);
attachInterrupt(digitalPinToInterrupt(BUTTON_ENTER), handleEnterButtonPress, FALLING);
showMenu();
}
void loop() {
if (BUTTON_LEFT_Pressed) {
BUTTON_LEFT_Pressed = false; // Reset the flag
Serial.println("Left Button pressed!"); // Debug message
// Add additional logic to handle the button press
showMenu();
}
if (BUTTON_RIGHT_Pressed) {
BUTTON_RIGHT_Pressed = false; // Reset the flag
Serial.println("Right Button pressed!"); // Debug message
// Add additional logic to handle the button press
showMenu();
}
if (BUTTON_UP_Pressed) {
BUTTON_UP_Pressed = false; // Reset the flag
Serial.println("Up Button pressed!"); // Debug message
// Add additional logic to handle the button press
currentMenuIndex--;
if (currentMenuIndex < 0) {
currentMenuIndex = menuItemCount - 1;
}
showMenu();
}
if (BUTTON_DOWN_Pressed) {
BUTTON_DOWN_Pressed = false; // Reset the flag
Serial.println("Down Button pressed!"); // Debug message
// Add additional logic to handle the button press
currentMenuIndex++;
if (currentMenuIndex >= menuItemCount) {
currentMenuIndex = 0;
}
showMenu();
}
if (BUTTON_ENTER_Pressed) {
BUTTON_ENTER_Pressed = false; // Reset the flag
Serial.println("Enter Button pressed!"); // Debug message
// Add additional logic to handle the button press
executeMenuAction();
}
}