#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
Adafruit_SSD1306 srituhobby = Adafruit_SSD1306(128, 64, &Wire);
#define sensor A0
int sX = 10;
int sY = 50;
int x = 10;
int Svalue;
int value;
int value1;
int pb = 3;
bool state = false;
int detik = 0;
int menit = 0;
long lastButtonPress = 0; // to store the last time the button was pressed
long timerStart = 0; // to store the start time of the timer
bool timerRunning = false; // to indicate if the timer is running
void setup() {
Serial.begin(9600);
pinMode(pb, INPUT_PULLUP);
srituhobby.begin(SSD1306_SWITCHCAPVCC, 0x3C); // Address 0x3C for 128x64
delay(1000);
srituhobby.clearDisplay();
srituhobby.drawLine(10, 10, 10, 50, WHITE); // Draw initial axes
srituhobby.drawLine(10, 50, 120, 50, WHITE);
srituhobby.display();
}
void loop() {
// Read sensor and map values
Svalue = analogRead(sensor);
value1 = map(Svalue, 0, 1023, 2000, 5000);
value = map(value1, 2000, 5000, 1, 40);
int y = 50 - value;
// Draw graph
if (x > 128) {
x = 10;
sX = 10;
srituhobby.clearDisplay();
srituhobby.drawLine(10, 10, 10, 50, WHITE);
srituhobby.drawLine(10, 50, 120, 50, WHITE);
} else {
srituhobby.drawLine(sX, sY, x, y, WHITE);
sX = x;
sY = y;
x++;
}
// Timer logic
if (!digitalRead(pb) && (millis() - lastButtonPress > 200)) {
srituhobby.clearDisplay();
srituhobby.drawLine(10, 10, 10, 50, WHITE);
srituhobby.drawLine(10, 50, 120, 50, WHITE);
x = 10;
sX = 10;
lastButtonPress = millis();
if (!timerRunning) {
timerRunning = true;
timerStart = millis();
menit = 44;
detik = 0;
}
}
if (timerRunning) {
long elapsed = (millis() - timerStart) / 1000;
detik = 60 - (elapsed % 60);
menit = 44 - (elapsed / 60);
if (menit < 0) {
timerRunning = false;
menit = 0;
detik = 0;
}
}
// Display timer with larger text and clear background
srituhobby.fillRect(90, 0, 38, 16, BLACK); // Clear background for timer
srituhobby.setTextSize(1); // Larger text size
srituhobby.setTextColor(SSD1306_WHITE);
srituhobby.setCursor(90, 0);
srituhobby.print(menit);
srituhobby.print(":");
if (detik < 10) {
srituhobby.print("0");
}
srituhobby.print(detik);
// Display everything
srituhobby.display();
delay(50); // Small delay to slow down the drawing speed
}