Pages: [1]   Go Down
Author Topic: weird counter behaviour  (Read 349 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 31
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hello
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.setCursor(0,1);
  lcd.print("call help : ");
  int i=60;
  for(int i=60; i > 0; i--)
  {
    lcd.setCursor(13,1);
    lcd.print(i);
    delay(1000);
    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!
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25875
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Is this simply a leading zero problem?
What if you print a space before the number, if "i" is less than 10?
Logged

"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.

Austin, TX
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6141
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

Offline Offline
Newbie
*
Karma: 0
Posts: 31
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

yeah i know about the if (i==0) break smiley

now the counter it goes down normally, until it reaches ten
in that moment it prints to the lcd
10
90
80
70
.
.
.
90
80
70
60
50
40
30
20
10

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?
« Last Edit: June 07, 2012, 06:49:13 am by nikolakos » Logged

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 33
Posts: 1260
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

AWOL has answered that already

If i<10 print (" ")
Logged

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25875
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It may even be as simple as always printing a space after your number
Logged

"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.

Offline Offline
Newbie
*
Karma: 0
Posts: 31
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Pages: [1]   Go Up
Jump to: