//Include the graphics library.
#include "U8glib.h"
//Initialize display.
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE | U8G_I2C_OPT_DEV_0);
#define outputA 2
#define outputB 3
int counter = 0;
int aState;
int aLastState;
#define MENU_ITEMS 3
const char *menu_strings[MENU_ITEMS] = { " ","DASHBOARD", "SETTINGS", "CALIBRATE"};
void setup(void)
{
pinMode (outputA,INPUT);
pinMode (outputB,INPUT);
Serial.begin(115200);
//Set font.
u8g.setFont(u8g_font_unifont);
}
void loop(void)
{
rotary();
u8g.firstPage();
do {
draw();
drawSubMenu();
} while (u8g.nextPage());
//Delay before repeating the loop.
delay(50);
}
void draw(void)
{
//Write text. (x, y, text)
u8g.setFont(u8g_font_unifont);
u8g.drawStr(25, 10, "MAIN MENU");
u8g.drawHLine(0,12,128);
}
void drawSubMenu(){
u8g.setFont(u8g_font_5x7);
for(int i=0;i<=MENU_ITEMS;i++){
u8g.drawStr(5, 20*i, menu_strings[i]);
}
}
void rotary(){
aState = digitalRead(outputA); // Reads the "current" state of the outputA
// If the previous and the current state of the outputA are different, that means a Pulse has occured
if (aState != aLastState){
// If the outputB state is different to the outputA state, that means the encoder is rotating clockwise
if (digitalRead(outputB) != aState) {
counter ++;
} else {
counter --;
}
Serial.print("Position: ");
Serial.println(counter);
}
aLastState = aState; // Updates the previous state of the outputA with the current state
}