/*
Ega Rizky Setiawan(21/479314/TK/52861)
*/
#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
// Declaration for SSD1306 display connected using I2C
#define OLED_RESET -1 // Reset pin
#define SCREEN_ADDRESS 0x3C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
//declare button class to manage buttons
class pushBtn{
public:
int btnPin;
bool lastSteadyState = LOW;
bool lastBounceState = LOW;
bool isPressed;
bool btnState;
int* counter;
int mod;
const int debounceDelay = 50;
unsigned long int lastDebounceTime = 0;
pushBtn(int pinNumber, int modifier , int* count){
btnPin = pinNumber;
mod = modifier;
counter = count;
}
//updatecounter
void updateCounter(){
*counter += mod;
if(*counter < 0){
*counter = 0;
}
}
//check if button is pressed
void btnPressCheck(){
btnState = digitalRead(btnPin);
if((millis() - lastDebounceTime) > debounceDelay){
if(btnState == LOW && lastSteadyState == HIGH){
isPressed == true;
updateCounter();
}
else if(btnState == HIGH && lastSteadyState == LOW){
isPressed = false;
}
lastSteadyState = btnState;
}
if(btnState != lastBounceState){
lastDebounceTime = millis();
lastBounceState = btnState;
}
}
//
};
//function for display main screen
void mainDisplay(int counter){
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(2);
display.setCursor(20, 10);
display.println("Counter: ");
display.setTextSize(3);
display.setCursor(55, 35);
display.println(String(counter));
display.display();
}
//function for display reset
void resetDisplay(){
display.clearDisplay();
display.setTextSize(3);
display.setCursor(20, 10);
display.println("RESET");
display.display();
delay(1000);
}
//declare pin for button
const int upBtnPin = 12;
const int downBtnPin = 14;
int counter = 0;
//variable for reset function
const unsigned int resetDelay = 2000;
unsigned long int resetTime = 0;
bool waitloop = false;
//instance up and down button object
pushBtn upBtn(upBtnPin, 1, &counter);
pushBtn downBtn(downBtnPin, -1, &counter);
//function for reset
void resetCheck(){
if(upBtn.isPressed && downBtn.isPressed){
if((millis() - resetTime) > resetDelay){
if(waitloop){
counter = 0;
resetDisplay();
}
waitloop = true;
resetTime = millis();
}
}
else{
waitloop = false;
}
}
void setup() {
Serial.begin(115200);
// Checks if the OLED is connected or not.
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
// Shows the Adafruit splash screen
display.display();
delay(1000);
display.clearDisplay();
// Use internal pullups for buttons
pinMode(upBtnPin, INPUT_PULLUP);
pinMode(downBtnPin, INPUT_PULLUP);
}
void loop() {
mainDisplay(counter); //display main screeen
upBtn.btnPressCheck(); // check if up button is pressed
downBtn.btnPressCheck(); // check if down button is pressed
resetCheck(); // check if both up and down button is pressed
}