I want to make a project ,like a digital clock with Uno and Oled (128 by64) . I tried several ode , modified them but still cant get the result needed. What I want is i will enter the current time of my country like this : (HH:MM) in the serial monitor , this time will be displayed on the Oled , updating accordingly every minute.
Please provide details of the hardware that you are using and post your best attempt at the sketch, using code tags when you do
What problems have you had getting the result needed ?
Hardware: Arduino UNO, Oled display (128 by 64) , jumper wires
CODE:
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// Variables to store the initial time
unsigned long initialMillis = 0;
unsigned long initialSeconds = 0;
void setup() {
Serial.begin(9600);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x64
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
// Store the initial time
initialMillis = millis();
initialSeconds = initialMillis / 1000;
}
void loop() {
// Clear the display buffer.
display.clearDisplay();
// Get the current elapsed time since the Arduino was powered on
unsigned long currentMillis = millis();
unsigned long currentSeconds = (currentMillis / 1000) - initialSeconds;
// Calculate hours, minutes, and seconds
unsigned long hours = currentSeconds / 3600;
unsigned long minutes = (currentSeconds % 3600) / 60;
unsigned long seconds = (currentSeconds % 3600) % 60;
// Format hours, minutes, and seconds with leading zeros
String formattedTime = (hours < 10 ? "0" : "") + String(hours) + ":" +
(minutes < 10 ? "0" : "") + String(minutes) + ":" +
(seconds < 10 ? "0" : "") + String(seconds);
// Display the formatted time on the OLED display
display.setTextSize(2); // Set text size
display.setTextColor(SSD1306_WHITE); // Set text color
display.setCursor(0, 0); // Set cursor position
display.println(formattedTime); // Print the time
display.display(); // Update the display
// Delay for one second before updating the time again
delay(1000);
}
That sketch works for me. Does it work for you ?
Your other topic on the same subject deleted.
Please do not duplicate your questions as doing so wastes the time and effort of the volunteers trying to help you as they are then answering the same thing in different places.
Please create one topic only for your question and choose the forum category carefully. If you have multiple questions about the same project then please ask your questions in the one topic as the answers to one question provide useful context for the others, and also you won’t have to keep explaining your project repeatedly.
Repeated duplicate posting could result in a temporary or permanent ban from the forum.
Could you take a few moments to Learn How To Use The Forum
It will help you get the best out of the forum in the future.
Thank you.
It works, but I want to enter a current time in the serial monitor and it will take it as a starting time and update it minute by minute. That is the code that I could do I cant move on and no idea what to do with the code....
Here is one way to get user input. It can be improved on but it is a starting point
void setup()
{
Serial.begin(115200);
Serial.print("Enter the current hour : ");
while (Serial.available() == 0)
{
}
String hour = Serial.readString();
Serial.print("You entered ");
Serial.println(hour);
}
void loop()
{
}
thanks
Some things to think about
- You cannot set the time using a String so it will need to be converted
- You really ought to verify that the user has entered a sensible value
- You need to consider what the Line Ending is set to in the Serial monitor
- It would be neater if the code to get user input were in a function of its own rather than part of setup()
1. Try it first with the OutputBox of the Serial Monitor as Display Unit.
Sketch:
char myTime[10] = "";
byte myHrs, myMin;
void setup()
{
Serial.begin(9600);
}
void loop()
{
byte n = Serial.available();
if (n != 0)
{
byte m = Serial.readBytesUntil('\n', myTime, sizeof(myTime) - 1);
myTime[m] = '\0';
//---------------------------------
//Serial.println(myTime);
char *token;
token = strtok(myTime, ":");
myHrs = atoi(token);
Serial.print(myHrs); Serial.print(':');
//-----------
token = strtok(NULL, ":");
myMin = atoi(token);
Serial.println(myMin);
//-----------------------------------
while (true)
{
delay(1000 * 60UL); //1-min delay
myMin = myMin + 1;
if (myMin == 60)
{
myMin = 0;
myHrs = myHrs + 1;
if (myHrs == 24)
{
myHrs = 0;
}
}
if (myHrs < 10)
{
Serial.print('0'); //leading zero
}
Serial.print(myHrs); Serial.print(':');
if (myMin < 10)
{
Serial.print('0'); //leading zero
}
Serial.println(myMin);
}
}
}
Test Output:
21:54:24.752 -> 23:59
21:55:24.758 -> 00:00
21:56:24.811 -> 00:01
2. Now connect your OLED with Arduino UNO and create your sketch taking help from the sketch of Step-1.
Sketch (Tested for Hrs:Min:Sec format using millis() function)
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
unsigned long currentMillis = millis();
char myTime[20] = "";
byte myHrs = 00;
byte myMin = 00;
byte mySec = 00 ;
void setup()
{
Serial.begin(9600);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS))
{
Serial.println(F("SSD1306 allocation failed"));
for (;;); // Don't proceed, loop forever
}
display.setCursor(0, 0);
display.clearDisplay();
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.print("Enter HH:MM:SS");
display.display();
display.clearDisplay();
display.setCursor(0, 0);
display.setTextSize(1);
display.println("Hrs : Min : Sec");
//-----------------------
display.setCursor(0, 10);
display.setTextSize(2); // Normal 1:1 pixel scale
while (Serial.available() == 0)
{
;
}
byte m = Serial.readBytesUntil('\n', myTime, sizeof(myTime) - 1);
myTime[m] = '\0';
//---------------------------------
//Serial.println(myTime);
char *token;
token = strtok(myTime, ":");
myHrs = atoi(token);
//Serial.print(myHrs); Serial.print(':');
//-----------
token = strtok(NULL, ":");
myMin = atoi(token);
//Serial.print(myMin); Serial.print(':');
//-----------------------------------
token = strtok(NULL, ":");
mySec = atoi(token);
//Serial.println(mySec);
showTime();
currentMillis = millis();
}
void loop()
{
if (millis() - currentMillis >= 1000)
{
currentMillis = millis();
mySec = mySec + 1;
if (mySec == 60)
{
mySec = 0;
myMin = myMin + 1;
if (myMin == 60)
{
myMin = 0;
myHrs = myHrs + 1;
if (myHrs == 24)
{
mySec = 0;
myMin = 0;
myHrs = 0;
}
}
}
display.clearDisplay();
display.setCursor(0, 0);
display.setTextSize(1);
display.println("Hrs : Min : Sec");
//-----------------------
display.setCursor(0, 10);
display.setTextSize(2); // Normal 1:1 pixel scale
showTime();
}
}
void showTime()
{
byte temp = myHrs;
if (temp < 10)
{
display.print('0');
}
display.print(myHrs, DEC);
display.print(':');
//------------------------
temp = myMin;
if (myMin < 10)
{
display.print('0');
}
display.print(myMin, DEC);
display.print(':');
//------------------------
temp = mySec;
if (temp < 10)
{
display.print('0');
}
display.print(mySec, DEC);
display.println();
display.display();
}
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.