#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
Adafruit_SSD1306 display = Adafruit_SSD1306(128, 64, &Wire);
int potentiometer = A0;
unsigned static int pot;
static int potRange = 1000;
unsigned static int selection;
static int numSelects = 5;
unsigned static int state;
static int screenStates = 7;
int oldSelection;
int highlighted;
void setup() {
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
pinMode(potentiometer, INPUT);
selection = analogRead(potentiometer);
oldSelection = selection;
state = 0 + 2 * (selection - 2);
highlighted = 1;
loadScreen();
}
void loop() {
pot = analogRead(potentiometer);
pot = map(pot, 0, 1023, 0, potRange);
select(pot);
}
void loadScreen() {
display.display();
delay(1000);
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
}
void select(int p) {
int range = potRange / 5;
for (int i = 0; i < numSelects; i++) {
if (pot > range * i && pot <= (range * i) + range) {
selection = i;
if (highlighted == 1 && selection > oldSelection) {
highlighted = 2;
state = 2 * i - 1;
updateScreen(state);
oldSelection = selection;
} else if (highlighted == 1 && selection < oldSelection) {
highlighted = 1;
state = 2 * i;
updateScreen(state);
oldSelection = selection;
} else if (highlighted == 2 && selection > oldSelection) {
highlighted = 2;
state = 2 * i - 1;
updateScreen(state);
oldSelection = selection;
} else if (highlighted == 2 && selection < oldSelection) {
highlighted = 1;
state = 2 * i;
updateScreen(state);
oldSelection = selection;
}
}
}
}
void updateScreen(int s) {
if (s == 0) {
display.clearDisplay();
display.setCursor(5,10);
display.setTextColor(BLACK, WHITE);
display.print(" First ");
display.setTextColor(WHITE);
display.setCursor(5,30);
display.print(" Second ");
} else if (s == 2) {
display.clearDisplay();
display.setTextColor(BLACK, WHITE);
display.setCursor(5,10);
display.print(" Second ");
display.setTextColor(WHITE);
display.setCursor(5,30);
display.print(" Third ");
} else if (s == 4) {
display.clearDisplay();
display.setTextColor(BLACK, WHITE);
display.setCursor(5,10);
display.print(" Third ");
display.setTextColor(WHITE);
display.setCursor(5,30);
display.print(" Fourth ");
} else if (s == 6) {
display.clearDisplay();
display.setTextColor(BLACK, WHITE);
display.setCursor(5,10);
display.print(" Fourth ");
display.setTextColor(WHITE);
display.setCursor(5,30);
display.print(" Fifth ");
} else if (s == 7) {
display.clearDisplay();
display.setTextColor(WHITE);
display.setCursor(5,10);
display.print(" Fourth ");
display.setTextColor(BLACK, WHITE);
display.setCursor(5,30);
display.print(" Fifth ");
} else if (s == 5) {
display.clearDisplay();
display.setTextColor(WHITE);
display.setCursor(5,10);
display.print(" Third ");
display.setTextColor(BLACK, WHITE);
display.setCursor(5,30);
display.print(" Fourth ");
} else if (s == 3) {
display.clearDisplay();
display.setTextColor(WHITE);
display.setCursor(5,10);
display.print(" Second ");
display.setTextColor(BLACK, WHITE);
display.setCursor(5,30);
display.print(" Third ");
} else if (s == 1) {
display.clearDisplay();
display.setTextColor(WHITE);
display.setCursor(5,10);
display.print(" First ");
display.setTextColor(BLACK, WHITE);
display.setCursor(5,30);
display.print(" Second ");
}
display.display();
}