// #include <SPI.h>
// #include <Wire.h>
// #include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display (SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
int potNEW = 0; // clear pot
int potOLD = potNEW; // make them equal
int line = 1;
bool button;
#define buttonPin 3 // change lines
void setup()
{
// Serial.begin(115200);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.begin(115200);
Serial.println(F("SSD1306 allocation failed"));
while (1);
} // OLED init failed. HALT.
display.clearDisplay();
pinMode(buttonPin, INPUT_PULLUP);
// Print the OVERLAY that never changes in the SETUP
display.setTextColor(SSD1306_BLACK, SSD1306_WHITE);
display.setTextSize(1);
display.setCursor(0, 0);
display.print(" CALIBRATION ");
display.setTextColor(WHITE);
display.setCursor(35, 10);
display.print("THR 1: 0");
display.setCursor(35, 20);
display.print("THR 2: 0");
display.display();
}
void loop()
{
potNEW = analogRead(A0);
button = digitalRead(buttonPin);
carrot(25, 10*line);
// delay(150);
// only redraw potentiometer value if value has changed
if (potNEW != potOLD) {
// redraw old pot value in BLACK to erase old value
display.setCursor(70, 10);
display.setTextColor(BLACK);
spacepad(potOLD); // for right-alignment see function
display.print(potOLD);
// display.display(); // do NOT update display, it will cause flicker
// draw new pot value in WHITE
display.setCursor(70, 10);
display.setTextColor(WHITE);
spacepad(potNEW); // for right-alignment see function
display.print(potNEW);
display.display();
potOLD = potNEW; // update old data
}
if (!button) {
// Serial.print(line);
display.setTextColor(BLACK);
carrot(25, 10 * line);
line++;
if (line > 2)
line = 1;
display.setTextColor(WHITE);
carrot(25, 10 * line);
}
}
void spacepad(int pot) {
// spacepadding or zeropadding creates right-alignment
if (pot < 1000) display.print(" ");
if (pot < 100) display.print(" ");
if (pot < 10) display.print(" ");
}
void carrot(int x, int y) { // indicate the menu choice
display.setCursor(x, y);
display.print(">");
display.display();
}