#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_ADDR 0x3C
Adafruit_SSD1306 display(128, 64, &Wire, -1);
const int buttonPin = A1;
int buttonState = 0;
int lastButtonState = LOW; // Assume button starts unpressed
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
int counter = 0;
void setup() {
pinMode(buttonPin, INPUT);
Serial.begin(9600);
if(!display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
display.clearDisplay();
displayAnimation(); // Play the startup animation
}
void loop() {
int reading = digitalRead(buttonPin);
// Check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited
// long enough since the last press to ignore any noise:
if ((millis() - lastDebounceTime) > debounceDelay) {
// If the switch changed, due to noise or pressing:
if (reading != lastButtonState) {
lastDebounceTime = millis(); // reset the debouncing timer
if (reading == HIGH) {
counter++;
displayCounter();
}
}
}
lastButtonState = reading;
}
void displayCounter() {
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.print("Count: ");
display.println(counter);
display.display();
}
void displayAnimation() {
for(int i = 0; i < display.width(); i += 4) {
display.clearDisplay();
display.drawRoundRect(0, display.height()/2 - 10, i, 20, 5, WHITE);
display.display();
delay(25);
}
}