128x64 OLED display with Arduino UNO

Hi,

I am building a project to display the date and time on a 128x64 OLED display with Arduino UNO.
I am using a GPS module(Ubloax 6M module) for date, time, and GPS co-ordinates. Below is my code and also I attached an image of my OLED screen.
I am seeing whenever in date/time any of the digits is less than 10 it is displayed as a single-digit (1, 2, 3..so on) I want it to display as 01 or 02 or 03....so on...if the digits are less than 10 it should be displayed with a prefix of 0.
How do I do this? what is the command for displaying two digits whenever the number is less than 10.
please suggest

#include <TinyGPS++.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <SPI.h>
#include <Wire.h>

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET 5

int ISThour = 0;
int ISTmin = 0;
int GMTmin = 0;
float latitude = 0;
float longitude = 0;

TinyGPSPlus gps;

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

void setup()
{
Serial2.begin(9600);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.print("GPS SYSTEM");
display.setTextSize(1.5);
display.setCursor(0, 30);
display.setTextColor(WHITE);
display.print("INITIALISING...");
display.setCursor(0, 50);
display.print("Please Wait");
display.display();
delay(2000);
display.clearDisplay();
}

void loop()
{
while (Serial2.available() > 0)
if (gps.encode(Serial2.read()))
displayInfo();
if (millis() > 5000 && gps.charsProcessed() < 10)
{
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println(("No GPS signal"));
delay(1000);
display.clearDisplay();
}
}

void displayInfo()
{
//if (gps.time.isValid() && gps.date.isValid())
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println(("Date:"));
display.setCursor(33, 0);
display.println((gps.date.day()));
display.setCursor(47, 0);
display.println(("/"));
display.setCursor(57, 0);
display.println((gps.date.month()));
display.setCursor(67, 0);
display.println(("/"));
display.setCursor(77, 0);
display.println((gps.date.year()));

display.setCursor(0, 20);
display.println(("Time:"));
GMTmin = gps.time.minute();
ISThour = gps.time.hour() + 5;
if (GMTmin >= 30) ISThour = ISThour + 1;
if (ISThour > 24) ISThour = gps.time.hour() - 24;
ISTmin = gps.time.minute() + 30;
if (ISTmin >= 60) ISTmin = ISTmin - 60;
display.setCursor(35, 20);
display.println(ISThour);
display.setCursor(47, 20);
display.println(":");
display.setCursor(53, 20);
display.println(ISTmin);
display.setCursor(65, 20);
display.println(":");
display.setCursor(71, 20);
display.println((gps.time.second()));

latitude = gps.location.lat();
longitude = gps.location.lng();
display.setCursor(0, 35);
display.println(("Lat: "));
display.setCursor(30, 35);
display.println(latitude, 6);
display.setCursor(0, 45);
display.println(("Lng: "));
display.setCursor(30, 45);
display.println(longitude, 6);
display.display();
delay(1000);
display.clearDisplay();
}

Thanks
Santosh

if (someValue < 10)
{
  Serial.print("0");
}
Serial.print(someValue);

Just to give you the idea.

Please note how I posted the code.

  display.println((gps.date.day()));
  display.setCursor(47, 0);
  display.println(("/"));
  display.setCursor(57, 0);
  display.println((gps.date.month()));
  display.setCursor(67, 0);
  display.println(("/"));
  display.setCursor(77, 0);
  display.println((gps.date.year()));

On this part of the code, you can check first the values of each item, then print an extra "0" before printing it if it's less than 10

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.