#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#include <Wire.h>
//----------------------------------------------------------------------------------------------------------------------
//Screen Characteristics
const uint8_t SCREEN_WIDTH = 128;
const uint8_t SCREEN_HEIGHT = 64;
const uint8_t OLED_RESET = -1;
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
//----------------------------------------------------------------------------------------------------------------------
//button interrupts
#include "OneButton.h"
#define PIN_INPUT 3
OneButton button(PIN_INPUT, true);
// save the millis when a press has started.
unsigned long pressStartTime;
int presslength; //time that is button pressed
//----------------------------------------------------------------------------------------------------------------------
//options
String O1="Option 1:";
String O2="Option 2:";
String O3="Option 3:";
int highlightOption=2;
int changeOption=1;
//----------------------------------------------------------------------------------------------------------------------
//
void setup() {
Serial.begin(115200);
setup_Display();
//setup_OLED();
//button interrupts
attachInterrupt(digitalPinToInterrupt(PIN_INPUT), checkTicks, CHANGE); //interrupt occurs when buttons are pressed
button.attachClick(singleClick); //singleClick runs when button is clicked
button.setPressTicks(800); // time waited until longpress
button.attachLongPressStart(pressStart); //when button is first pressed
button.attachLongPressStop(pressStop); //when button is released
}
void loop() {
mainMenu();
}
//=============================================================================
//OLED Display setup
void setup_Display() {
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
//Setting display/message charateristics
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
display.cp437(true); // Use full 256 char 'Code Page 437' font
display.clearDisplay();
}
//===================================================================================================
//interrupts
void checkTicks() { //interrupt occurs when buttons are pressed
button.tick();
}
void pressStart() { //when button is first pressed
pressStartTime = millis() - 800; // as set in setPressTicks()
}
// function called after long press
void pressStop() { //when button is released
presslength = millis() - pressStartTime; //measures length of time button is pressed
Serial.print("Button Pressed for:"); Serial.println(presslength); //serial print is for testing
}
void singleClick() {
Serial.println("click");
changeOption=1;
} // singleClick
//===================================================================================================
//menu display
void mainMenu(){
if (changeOption==1){
changeOption=0;
highlightOption++;
if (highlightOption>2) {highlightOption=0;}
clearAndPrint_Display();
}
}
void clearAndPrint_Display(){
display.clearDisplay();
display.setTextSize(2);
display.setCursor(0, 0);
display.print(" Main Menu");
display.display();
display.setTextSize(1);
display.setCursor(10, 20);
display.println(O1);
display.setCursor(10, 30);
display.println(O2);
display.setCursor(10, 40);
display.println(O3);
display.display();
int arrowPos=20+highlightOption*10;
display.setTextSize(1);
display.setCursor(0, arrowPos);
display.write(16);
display.display();
}