#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "Button.h"
#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 -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// Create a button object on pin 4
Button btnG(4);
Button btnB(3);
int count; //count variable
unsigned long cm;
void setup() {
Serial.begin(9600);
// initialize and clear display
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
display.clearDisplay();
}
void loop() {
cm=millis();
display.clearDisplay();
btnG.update();
btnB.update();
if (btnB.wasPressed())
{
incCount();
}
if(count>23) count=0;
displayCount();
display.display();
delay(100);
}
void incCount()
{
if(btnG.isPressed())
{
count++;
}
if (btnG.wasLongPressed() ) {
static unsigned long pm=cm;
if((cm-pm)>=200)
{
count++;
pm=cm;
}
}
}
void displayCount() {
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.print("COUNT:");
display.setCursor(59, 26);
display.print(count < 10 ? "0" : "");
display.print(count);
}