Quick question about clock code

Hi,
Fairly basic question,

I got this clock and have added a ‘0’ if the minutes is less than 10, so it will display 10:09 instead of 10:9, but after ten the ‘0’ still remains, so I’m getting 10:009

I’m missing something quite simple and have slept on it with no success. I thought I didnt clear the ‘0’ but it’s not clearing from the screen.

void printTime() {
//For DS1302 clock
  Time t = rtc.time();

  //setup strings for display 
  const String day = dayAsString(t.day);
  String month;
  String date;
  String hour;
  String minutes;

  //define strings 
  month = String(t.mon);
  date = String(t.date);
  hour = String(t.hr);
  minutes = String(t.min);

  //Display formatting using strings 
  String fullDate = day + " " + date + "/" + month;
  String time = hour + ":" + minutes;
  String min0 = "0" + minutes;

  
  
  //5110 display code for day and month 
  display.clearDisplay();
  display.setTextColor(BLACK);
  display.setCursor(0, 0);
  display.setTextSize(2);
  display.println (fullDate);
  display.display();
  delay(150);


 //Print the hours 
  if (t.hr < 10)
  {
    hour = "0" + hour + ":";
    display.setCursor(0, 33);
    display.println(hour);
    display.display();
  } 
  else
  {
    hour = hour + ":";
    display.setCursor(0, 33);
    display.println(hour);
    display.display();

  }

  
   //Print the minutes
  if (t.min > 10)
  {
    display.println(t.min);
    display.setCursor(46, 33);
    display.display();
    delay(150);
    //display.clearDisplay();
  }
  else
  {
    minutes = "0" + minutes;
    display.setCursor(44, 33);
    display.println(minutes);
    display.display();
    delay(150);  
    display.clearDisplay();
    
    
    
    
  }
}
  if (t.min > 10)

What happens when min is 11, does it do right then? Maybe this should be >=.

Please post your entire sketch, not just snippets.

What kind of display are you using, and what library are you using to write to the display? I ask because I am trying to make sense out of this

    display.setCursor(46, 33);

and other, similar lines in your code.

Hi, I’m using a 5110 Display (Adafruit_PCD8544) with the Adafruit GFX library.
The hours work, it will switch from 09 to 10 (hours) but still not the minutes,
Now once the minutes goes over 10 to 11, the minute display clears and there’s nothing.

SO what the hell am I doing wrong :o :o :o ???

// Example sketch for interfacing with the DS1302 timekeeping chip.
//
// Copyright (c) 2009, Matt Sparks
// All rights reserved.
//
// http://quadpoint.org/projects/arduino-ds1302
#include <stdio.h>
#include <DS1302.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>
#include "DHT.h"
#include <Wire.h>

#define DHTPIN 4
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);

namespace {

// Set the appropriate digital I/O pin connections. These are the pin
// assignments for the Arduino as well for as the DS1302 chip. See the DS1302
// datasheet:
//
//   http://datasheets.maximintegrated.com/en/ds/DS1302.pdf
const int kCePin   = 10;  // Chip Enable RST
const int kIoPin   = 9;  // Input/Output
const int kSclkPin = 8;  // Serial Clock

// Create a DS1302 object.
DS1302 rtc(kCePin, kIoPin, kSclkPin);

String dayAsString(const Time::Day day) {
  switch (day) {
    case Time::kSunday: return "Sunday";
    case Time::kMonday: return "Monday";
    case Time::kTuesday: return "Tuesday";
    case Time::kWednesday: return "Wednesday";
    case Time::kThursday: return "Thursday";
    case Time::kFriday: return "Friday";
    case Time::kSaturday: return "Saturday";
  }
  return "(unknown day)";
}



}  // namespace



// Software SPI (slower updates, more flexible pin options):
//pin 9 - Serial clock out (SCLK)
// pin 10 - Serial data out (DIN)
// pin 11 - Data/Command select (D/C)
// pin 12 - LCD chip select (CS)
// pin 13 - LCD reset (RST)
Adafruit_PCD8544 display = Adafruit_PCD8544(7, 6, 5, 4, 3);

// Hardware SPI (faster, but must use certain hardware pins):
// SCK is LCD serial clock (SCLK) - this is pin 13 on Arduino Uno
// MOSI is LCD DIN - this is pin 11 on an Arduino Uno
// pin 5 - Data/Command select (D/C)
// pin 4 - LCD chip select (CS)
// pin 3 - LCD reset (RST)
// Adafruit_PCD8544 display = Adafruit_PCD8544(5, 4, 3);
// Note with hardware SPI MISO and SS pins aren't used but will still be read
// and written to during SPI transfer.  Be careful sharing these pins


void setup() {
  Serial.begin(9600);
  display.begin();
  display.setContrast(50);
  display.clearDisplay();
  display.display();
  // Initialize a new chip by turning off write protection and clearing the
  // clock halt flag. These methods needn't always be called. See the DS1302
  // datasheet for details.
  rtc.writeProtect(false);
  rtc.halt(false);

  // Make a new time object to set the date and time.
  // Sunday, September 22, 2013 at 01:38:50.
  Time t(2015, 11, 01, 12, 9, 55, Time::kSunday);

  // Set the time and date on the chip.
  //Uncomment this to reset the time.
  rtc.time(t);
}

// Loop and print the time every second.
void loop() {

  printTime();
  //delay(5000);
  //ReadoutDHT();
  //delay(10000);
  //ReadoutTest();
  //delay(20000);

}


void printTime() {
//For DS1302 clock
  Time t = rtc.time();

  //setup strings for display
  const String day = dayAsString(t.day);
  String month;
  String date;
  String hour;
  String minutes;

  //define strings
  month = String(t.mon);
  date = String(t.date);
  hour = String(t.hr);
  minutes = String(t.min);

  //Display formatting using strings
  String fullDate = day + " " + date + "/" + month;
  String time = hour + ":" + minutes;
  String min0 = "0" + minutes;

 
 
  //5110 display code for day and month
  display.clearDisplay();
  display.setTextColor(BLACK);
  display.setCursor(0, 0);
  display.setTextSize(2);
  display.println (fullDate);
  display.display();
  delay(150);


 //Print the hours
  if (t.hr < 10)
  {
    hour = "0" + hour + ":";
    display.setCursor(0, 33);
    display.println(hour);
    display.display();
  }
  else
  {
    hour = hour + ":";
    display.setCursor(0, 33);
    display.println(hour);
    display.display();

  }

 
   //Print the minutes
  if (t.min > 10)
  {
    display.println(t.min);
    display.setCursor(46, 33);
    display.display();
    delay(150);
    display.clearDisplay();
  }
  else
  {
    //minutes = "0" + minutes;
    display.setCursor(44, 33);
    display.println(minutes);
    display.display();
    delay(150); 
    display.clearDisplay();
   
   
   
   
  }
  
  
}

Ditch the stupid String class. There is NO reason to convert a number to a String.

sprintf() and char arrays are the way to go.

PaulS: There is NO reason to convert a number to a String.

sprintf() and char arrays are the way to go.

Paul is right, there is a tool out there for you called sprintf()...

int hours = 2;
int minutes = 15;
int seconds = 9;

void setup()
{
  Serial.begin(9600);
  char buffer[12] = "";
  sprintf(buffer,"%02d:%02d:%02d", hours, minutes, seconds);
  Serial.println(buffer);
}

void loop()
{
  
}

here you can learn about the sprintf() formatters

Read my reply again. You've got it set up so if the minutes are greater than 10 don't print the extra zero ELSE print the extra zero. 10 is not greater than 10, so for 10 it falls into the else and prints an extra zero.

I think I found the problem:

   //Print the minutes
  if (t.min > 10)
  {
    display.println(t.min);    // first print ...
    display.setCursor(46, 33); // and then set the cursor ???
    display.display();
    delay(150);
    display.clearDisplay();
  }
  else
  {
    //minutes = "0" + minutes;
    display.setCursor(44, 33);
    display.println(minutes);
    display.display();
    delay(150);
    display.clearDisplay();   
  }

That was it!!!!!! display.setCursor(46, 33) after display.println(t.min).

Sometimes when you look at one problem too much and get frustrated you miss the completly obvious like I have done. I've also gotten rid of the strings and replaced them with sprintf(). Thanks for everyone's help, really appreciate peoples input and fantastic advice.

odometer: I think I found the problem:

   //Print the minutes
  if (t.min > 10)
  {
    display.println(t.min);    // first print ...
    display.setCursor(46, 33); // and then set the cursor ???
    display.display();
    delay(150);
    display.clearDisplay();
  }
  else
  {
    //minutes = "0" + minutes;
    display.setCursor(44, 33);
    display.println(minutes);
    display.display();
    delay(150);
    display.clearDisplay();   
  }

Consistency is a good thing. Look at how you deal with hours and minutes. The process should be the same. Your process is NOT.