The Adjustable OLED Timer

by sister mariaM in Circuits > Arduino

76 Views, 0 Favorites, 0 Comments

The Adjustable OLED Timer

This Instructables will explain how I made an adjustable OLED timer .It can be used for times up to 99 minutes .It could be used for cooking (cookies!) or taking a nap(22 minutes or more ) or whenever you need a timer for 99 minutes or less .For more time you would have to change the program. I have used to to test it (2 minutes ) and to take a nap (22 minutes)too

Supplies

Supplies are ;

Arduino and USB cable

Arduino IDE

OLED ,

3 pushbuttons

buzzer

wires

Theory

In this circuit, the OLED acts as a timer .This happens when the Arduino does 2 tasks .First it tracks the passing time in its internal processor .Secondly it sends the data across the two I2 data pathways, the SDA and SCL which to update the display with the Arduino code . .

The OLED display has an organic material in it .When electricity passes through it the pixels light up according to the Arduino code .

The buttons adjust the time you want .The first pushbutton increases the time .The middle pushbutton starts the time and the last pushbutton decreases the amount of time

The buzzer will sound at zero .

Setting Up the Circuit

Arduino;

5 volts goes to the positive rail on the breadboard

ground goes to the negative rail on the breadboard

OLED

VCC goes to positive rail on the breadboard

Ground goes to the negative rail on the breadboard

SCL goes to SCL or A5 on Arduino

SDA goes to SDA or A4 on Arduino

Buzzer goes to Arduino 8

and negative side goes to ground

Pushbutton (left)goes to Arduino 2 and ground

pushbutton (middle)goes to Arduino 3 and ground

pushbutton (right goes to Arduino 4 and ground)

Arduino Code

This is the Arduino code;


#include <Wire.h>

#include <Adafruit_GFX.h>

#include <Adafruit_SSD1306.h>

#include <EEPROM.h>


#define SCREEN_WIDTH 128

#define SCREEN_HEIGHT 64

#define OLED_RESET -1

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);


// Pin Definitions

const int ADJUST_UP_BUTTON = 2;

const int START_BUTTON = 3;

const int ADJUST_DOWN_BUTTON = 4; // NEW: Decrease button pin

const int BUZZER = 8;


// Memory Settings

const int EEPROM_ADDR = 0;

const int MAGIC_NUMBER = 143;

const int CONFIG_ADDR = 10;


// Timer Logic Variables

long initialMinutes = 22; // Default starting minutes for your test

long totalSeconds;

bool isRunning = false;

unsigned long previousMillis = 0;


// Debounce helper variables

bool lastAdjustUpState = HIGH;

bool lastAdjustDownState = HIGH; // NEW

bool lastStartState = HIGH;


// Forward declarations

void updateDisplay();

void triggerAlarm();


void setup() {

pinMode(ADJUST_UP_BUTTON, INPUT_PULLUP);

pinMode(ADJUST_DOWN_BUTTON, INPUT_PULLUP); // NEW

pinMode(START_BUTTON, INPUT_PULLUP);

pinMode(BUZZER, OUTPUT);


// Initialize OLED Display

if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {

for(;;);

}


// --- TEST MODE FORCED OVERRIDE ---

// Forces the code to reset to 2 minutes every time you upload or reboot

initialMinutes = 2;

EEPROM.update(EEPROM_ADDR, initialMinutes);

EEPROM.update(CONFIG_ADDR, MAGIC_NUMBER);

// ---------------------------------


totalSeconds = initialMinutes * 60;


display.clearDisplay();

updateDisplay();

}


void loop() {

unsigned long currentMillis = millis();


// Read Button States

bool adjustUpState = digitalRead(ADJUST_UP_BUTTON);

bool adjustDownState = digitalRead(ADJUST_DOWN_BUTTON); // NEW

bool startState = digitalRead(START_BUTTON);


// --- BUTTON 1: ADJUST TIME UP ---

if (adjustUpState == LOW && lastAdjustUpState == HIGH) {

delay(50); // Debounce delay

if (!isRunning) {

initialMinutes++;

if (initialMinutes > 99) initialMinutes = 1; // Cycle wrap down to 1


totalSeconds = initialMinutes * 60;

EEPROM.update(EEPROM_ADDR, initialMinutes);

updateDisplay();

}

}

lastAdjustUpState = adjustUpState;


// --- BUTTON 3: ADJUST TIME DOWN (NEW) ---

if (adjustDownState == LOW && lastAdjustDownState == HIGH) {

delay(50); // Debounce delay

if (!isRunning) {

initialMinutes--;

if (initialMinutes < 1) initialMinutes = 99; // Cycle wrap up to 99


totalSeconds = initialMinutes * 60;

EEPROM.update(EEPROM_ADDR, initialMinutes);

updateDisplay();

}

}

lastAdjustDownState = adjustDownState;


// --- BUTTON 2: START / PAUSE / RESET ---

if (startState == LOW && lastStartState == HIGH) {

delay(50);

if (totalSeconds <= 0 && !isRunning) {

// Fetch the last saved time from memory to reset

initialMinutes = EEPROM.read(EEPROM_ADDR);

totalSeconds = initialMinutes * 60;

} else {

isRunning = !isRunning;

}

updateDisplay();

}

lastStartState = startState;


// --- TIMER COUNTDOWN LOGIC ---

if (isRunning && (currentMillis - previousMillis >= 1000)) {

previousMillis = currentMillis;


if (totalSeconds > 0) {

totalSeconds--;

updateDisplay();

}


// --- TIME'S UP ---

if (totalSeconds <= 0) {

isRunning = false;

updateDisplay();

triggerAlarm();

}

}

}


void updateDisplay() {

display.clearDisplay();


// Status Header Text Layout

display.setTextSize(1);

display.setTextColor(SSD1306_WHITE);

display.setCursor(0,0);

if (totalSeconds <= 0) {

display.print(" !!! TIME'S UP !!! ");

} else if (isRunning) {

display.print(" COUNTING DOWN ");

} else {

display.print(" MEM SAVED SET ");

}


display.drawFastHLine(0, 12, 128, SSD1306_WHITE);


// Time Breakdown Calculation

long dispMins = totalSeconds / 60;

long dispSecs = totalSeconds % 60;


// Big Clock Display Positioning

display.setTextSize(3);

display.setCursor(20, 28);


if (dispMins < 10) display.print("0");

display.print(dispMins);

display.print(":");

if (dispSecs < 10) display.print("0");

display.print(dispSecs);


display.display();

}


void triggerAlarm() {

for (int i = 0; i < 5; i++) {

tone(BUZZER, 2500); // Sharp, high frequency for extra volume

delay(200);

noTone(BUZZER);

delay(100);

}

}

Conclusion

This Instructables shows how you can use an OLED as a timer .It can be set to any time less than 99 minutes ,I have tested it using 2 minutes and 22 minutes ,but any time less than 99 minutes can be used .

It is useful as a timer for cooking ,naps and whenever a timer is needed ,It is adjustable .I hope this helps you

Thank you