#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define BUTTON_PIN 2
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
unsigned long lastCount = 0;
unsigned long currentCount = 0;
char buffer[100];
void setup() {
Serial.begin(9600);
// initialize OLED display with address 0x3C for 128x64
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
while (true);
}
delay(2000); // wait for initializing
oled.clearDisplay(); // clear display
oled.setTextSize(2); // text size
oled.setTextColor(WHITE); // text color
pinMode(BUTTON_PIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), buttonPressed, RISING);
}
void loop() {
if (lastCount != currentCount) {
Serial.println(currentCount); // print count to Serial Monitor
oled.clearDisplay(); // clear display
oled.setCursor(0, 0);
snprintf(buffer, 100, "Count: %c", currentCount);
oled.println(buffer); // display count
oled.display(); // show on OLED
lastCount = currentCount;
}
}
void buttonPressed() {
currentCount++;
}
Loading
ssd1306
ssd1306