integer 1 written as 01

Hi everyone,
I’m trying to program a clock. Everything seems to work fine. I know my code could probably be more efficient (feel free to give me some cues ont that). There is one that is a bit off tough. When the °seconds° integer is only one digit, the number is written in the wrong column. When my “seconds” integer has two digits, there is no problem. But, when my “seconds” integer has a value between 0 and 9. the value is still written in the fourteenth column. The best would have my clock having a display of my seconds like this: 01-02-03-04-05-06-07-08-09-10 -11 and so on. What i have looks like after the “seconds” integer go back to 0. 09-19-29-39-49-59-69-79-89-99-10.What would be the simplest way to solve this. Is there a way to have my one digit integer to be written with a 0 before them?

Thank you for your answers!

#include <LiquidCrystal.h>
LiquidCrystal lcd (12 , 11 , 5,  4, 3, 2);
int secondes = 55;
int minutes = 59;
int heures = 12;
void setup (){
  lcd.begin (16,2);
  Serial.begin(9600);
 lcd.setCursor(13,0);
 lcd.print (":");
}
void loop(){
Serial.println ( "sec" );
 Serial.println(secondes); 
Serial.println("mins");
Serial.println(minutes);
Serial.println("heures");
Serial.println(heures);
lcd.setCursor(14,0);
lcd.print (secondes);
lcd.setCursor(11,0);
lcd.print (minutes);


delay (1000);
secondes +=1;
if (secondes == 60){
    minutes +=1;
  secondes = 0;
  }
  if (minutes == 60){
    heures +=1;
  minutes = 0;
  }
  if (heures == 13){
  
  heures = 1;
  }
}

Do you mean, when the value is less than ten, you need to print a zero?

Exactly, i need to print a 0 before the values that are less than 10

francisverville:
Exactly, i need to print a 0 before the values that are less than 10

You just answered your own question

Can you test the value of a variable ?
Can you print a zero ?

Ie, there is no magic clever way to get “print” to automatically add the leading zeros for you - you’ll have to write extra code to print it, after figuring out that it’s necessary.

Who the f... removed half of the replies? Including the useful ones.

I did - it was getting insanely OT.

The original question has been adequately answered, IMO

OK, thanks for clarifying. I do not quite agree with the approach but I’ve learned long ago that one never argues with the bartender :slight_smile:

Bartender?
A promotion!

If your values never exceed 99:

       print( N / 10 );    // no newline
       print( N % 10 );    // add newline

("print( )" is whatever you are using to print with.)

Thanks for you answers!
I'm not sure i get this tough

If your values never exceed 99:

Code: [Select]

print( N / 10 ); // no newline
print( N % 10 ); // add newline

Does it mean i'm going to print the value of the integer N divided by ten two times?

Does it mean i'm going to print the value of the integer N divided by ten two times?

Sort of

N / 10 will give you the integer result of the division by 10, ie the first digit When N is less that 10 the result will be zero
N % 10 will give you the remainder (modulo) of N divided by 10, ie the second digit

I prefer this kind of thing. It just feels neater to me. Shrug.

if (seconds < 10) 
{
   lcd.print("0");
}

lcd.print(seconds);

The if method and the dividing methods are now clear to me. Thank you!

GypsumFantastic:
I prefer this kind of thing. It just feels neater to me. Shrug.

if (seconds < 10) 

{
  lcd.print(“0”);
}

lcd.print(seconds);

If you like adding superfluous code that runs slower and grows your code.

AWOL:
Bartender?
A promotion!

No, it just means that you can't drink on the job anymore.