/*
Wiring :
LCD Arduino
SDA ----> A4
SCL ----> A5
VCC ----> 5V
GND ----> GND
buzzer ----> 4
btn_up ----> 3
btn_down ---> 2
*** O-Tech ***
A Place You Can Trust
https://wa.me/201207738604
https://www.facebook.com/profile.php?id=61555112881938
https://www.youtube.com/@OTECHegypt
*/
#define btn_up 3
#define btn_down 2
#define buzzer 4
#include <Wire.h> // Required for I2C communication
#include <Adafruit_GFX.h> // Core graphics library
#include <Adafruit_SSD1306.h> // Driver for SSD1306 display
// OLED dimensions (change if you have a different size)
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// For 128x32 OLED, change SCREEN_HEIGHT to 32
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
// The I2C address is usually 0x3C or 0x3D. Check your module!
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
// For I2C, generally use -1. If your OLED has a RST pin you want to control,
// connect it to an Arduino digital pin (e.g., 4) and change -1 to 4.
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
int count = 0;
unsigned long int last_press = 0;
void setup() {
Serial.begin(9600);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C is common. Try 0x3D if 0x3C doesn't work.
Serial.println(F("SSD1306 allocation failed"));
for (;;) delay(1000); // Don't proceed, loop forever
}
pinMode(btn_up, INPUT_PULLUP);
pinMode(btn_down, INPUT_PULLUP);
pinMode(buzzer, OUTPUT);
// Clear the buffer
display.clearDisplay();
// Display initial text
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
display.println(F("Hello, World!"));
display.display(); // Show initial text
delay(2000); // Pause for 2 seconds
display.clearDisplay();
}
void loop() {
if(millis() <= 6000){
reset_to_home();
display.print(F("Count: "));
count = millis() / 1000;
display.print(count); // Display seconds since Arduino started
display.print(" "); // Delete trailing characters from previous print() commands
}
if(!digitalRead(btn_up)){
if(millis() - last_press >= 250){
last_press = millis();
Serial.println("up");
reset_to_home();
display.print(F("f: "));
display.print(++count * 100); // Increment count THEN print it
display.print(" ");
tone(buzzer, count * 100, 250);
}
}
else if(!digitalRead(btn_down)){
if(millis() - last_press >= 250){
last_press = millis();
Serial.println("down");
reset_to_home();
display.print(F("f: "));
if(count < 1){ count = 1; }
display.print(--count * 100); // Decrement count THEN print it
display.print(" ");
tone(buzzer, count * 100, 250);
}
}
// Update the display with new content
display.display();
delay(100); // Small delay to make the counter readable
}
void reset_to_home(){
// Clear the buffer before drawing new content display.clearDisplay();
// Display a simple counter
display.setTextSize(2); // Use a larger text size
display.setCursor(0, 0);
display.setTextColor(WHITE, BLACK);
}
Up
Down