Go Down

Topic: weird counter behaviour (Read 745 times) previous topic - next topic


i wrote this code:

#include <LiquidCrystal.h>

LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

void setup() {
  lcd.begin(16, 2);

void loop() {
  lcd.print("Press B1 or i'll ");
  lcd.print("call help : ");
  int i=60;
  for(int i=60; i > 0; i--)
    if (i==0) break;

the purpose is to display the seconds from 60 to zero and if no reponse of button is pressed (i havent written that code here) it calls for help

now, when the counter goes to zero in my program, intead of restarting again (since it is in the loop() function) and basically counting back wards from 60 again, it instead starts printing in the lcd :
90, 80, then 70 ... 10 then
starts from 60 counting backwards, and it also displays the character 'r' in the LCD (i didnt include that)

i thought that was weird

thats why i put the command int i=60;
to ensure that when the program restarts the loop(), the variable is given the value 60
still, the behaviour of the program is the same

can anyone help please?

thank you!


Is this simply a leading zero problem?
What if you print a space before the number, if "i" is less than 10?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

James C4S

You might also consider called lcd.clear() at the start of loop(), to ensure no numbers are left over from the previous iteration.

Maybe this was a troubleshooting step, but there is really no reason for "  if (i==0) break;" inside of your for() loop.
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com


Jun 07, 2012, 01:38 pm Last Edit: Jun 07, 2012, 01:49 pm by nikolakos Reason: 1
yeah i know about the if (i==0) break :)

now the counter it goes down normally, until it reaches ten
in that moment it prints to the lcd

then starts again from 60 as normal, and the extra character is removed magically....until it appears again

so YES, it is a leading zero problem.

how do i remove that goddamn zero?

UPDATE: The extra character is basically a whole extra string. It rewrites the lcd print text, omitting the first letter only which is "P".
This behavior is removed when i call lcd.clear() at the start of the loop, but why is this behaviour happening in the first place?


AWOL has answered that already

If i<10 print (" ")
Arduino Libraries https://github.com/MajicDesigns?tab=Repositories
Parola for Arduino https://github.com/MajicDesigns/Parola
Arduino++ blog https://arduinoplusplus.wordpress.com


It may even be as simple as always printing a space after your number
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.


thanks everyone for everything!

i seemed to fixed everything!

except i dont know why these two errors occured.. the leading zero and the redisplay of the string (with the first character omitted), whe i dont write a lcd.clear() funtction at the start of the loop

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131