#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
//Screen setup
#define Width 128
#define Height 64
#define Oled_reset -1
Adafruit_SSD1306 oled(Width, Height, &Wire, -1);
//Button Pin and State
const int ButtonPin = 3;
int State = 0;
//Screen variables
int Y = 1;
int X = 1;
int A = 0;
int Multiplier = 2;
int Digits;
void setup() {
Serial.begin(9600);
pinMode(ButtonPin, INPUT);
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
while (true);
}
oled.clearDisplay();
oled.display();
delay(500);
}
void loop() {
//Count number of digits
Digits = (log10(A) + 1);
X = (Digits * 5 * Multiplier + (Digits - 1) * 1) / 2;
if (digitalRead(ButtonPin) == HIGH and State == 0){
//Add 1 when pressed
State = 1;
A += 1;
}
else if (digitalRead(ButtonPin) == LOW){
//Change state if not pressed
State = 0;
}
//oled Write and update
oled.setTextSize(Multiplier);
oled.setCursor(64 - X, 32 - 8 * Multiplier / 2);
oled.setTextColor(WHITE);
oled.println(A);
oled.display();
oled.clearDisplay();
}