Go Down

Topic: print multiple variables on LCD? (Read 2 times) previous topic - next topic

valkir

Greetings forum,
i'd like to present you with a problem i'm having:
i'm working on a timer of sorts, and since i'm god-awful at code, i'm re-working the "hello world" example (which works fine with my wires).
here's what i have right now:


void setup() {
    lcd.begin(16, 2);
  lcd.print("Time:"); 
}

void loop() {
    lcd.setCursor (0 ,1);
  int time1;
  for(time1=0; time1<10; time1++)
    { lcd.print(time1);
      delay(1000);
      lcd.clear();
    }

so the idea is that there's a time1 variable which goes from 0 to 10, with increments each second. it's supposed to look like this on the LCD:
Time:
  0-10

here's the problem:  at first the LCD says "Time" on the first line, and a "0" on the second line. as soon as the 1 sec delay is up, the screen clears from the lcd.clear function and then the counter moves up on the first row where the "time" was supposed to be.
from there on the counter still works and goes up to 10. then the first line is cleared and "0" is printed on the second line where it was supposed to be, then that is cleared too and the counting resumes on the first line, starting from 1.

here's some more code that didn't work, portrayed in words:
there are supposed to be 3  2-digit numbers printed on the second line of the ldc, and they are supposed to be logically linked with each other like this:
" int a
int b
int c
a++
if a =10, b++
if b =10, c++
if c = 10, a=b=c=0
so it's like a clock that only counts 10 seconds, 10 minutes and 10 hours. After that maybe do a short beep on a buzzer to indicate the start of a new cycle.
 
i tried setting the cursor to a different place for each number to be printed but that went horribly, with numbers getting printed everywhere on the screen with seemingly no order of appearance.

this last bit of logical binding between the numbers also proves to be an issue, since it looks like the software doesn't like if-s in the void.loop() - and returns a message along the lines of "variable is not defined in this scope". it's OK with for-s though. i guess i should also ask, out of general curiosity, where should i do such logical operations -in the setup, where i define variables, or in the loop- where the lcd.print is done?

that's the just of it, and i'm sorry if i made you facepalm so hard your head hurts. but it is what it is, and if i don't ask how it's supposed to be done i'm not going to get anything done on this project.

anyway, any feedback and/or suggestions will be appreciated!

and remember to have a great day! :)

Riva

Problem is your resetting the cursor to line 1 with the lcd.clear (along with clearing the word Time: )
You either need to put all the lcd.print code in the for loop or don't clear the screen as per example below.
Remember also that the arduino internal clock that delay is derived from is not that accurate over longer durations and the way your code is written means the countdown will take more than a second because the calls to setCursor & print will add to the 1 second delay.

Code: [Select]
void loop() {
  for(int time1=0; time1<10; time1++) { 
    lcd.setCursor (0 ,1);   //Set cursor to start of second line
    lcd.print("  ");        //Blank previous text there
    lcd.setCursor (0 ,1);   //Reset cursor to start of line
    lcd.print(time1);       //Print time
    delay(1000);            //Delay approx 1 second
  }
}
http://forum.arduino.cc/index.php?action=unread;boards=5,67,10,11,66,12,15,17,21,22,23,24,25,29;ALL

valkir

i see, thanks for the quick response and the info, Riva :)
another thing i thought to ask is - what syntax does the lcd.print accept?
can i do something like " lcd.print(time1, time2, time3)" ? or do i need to set the cursor to a different spot for each variable and print them out one at a time?
also: would i be able to correct for the delay introduced by the setCursor and printing by reducing the delay by a couple of ms? i guess i need to do some research on the amount of time the processor needs for each operation.
i was interested in the arduino's timer capabilities before, and i know that more accurate "time counting" can be done with external triggering and more circuitry, but for this simple application i dont think a miss-count of 1-2 seconds a day would sink the ship.

Riva


another thing i thought to ask is - what syntax does the lcd.print accept?
can i do something like " lcd.print(time1, time2, time3)" ? or do i need to set the cursor to a different spot for each variable and print them out one at a time?

I think the .print for LCD is same as for Serial so only one value at a time and cursor to next print position
http://arduino.cc/en/Reference/LiquidCrystal

Quote

also: would i be able to correct for the delay introduced by the setCursor and printing by reducing the delay by a couple of ms? i guess i need to do some research on the amount of time the processor needs for each operation.
i was interested in the arduino's timer capabilities before, and i know that more accurate "time counting" can be done with external triggering and more circuitry, but for this simple application i dont think a miss-count of 1-2 seconds a day would sink the ship.

You would adjust to correct the time by reducing the delay but maybe a better idea would be to use something like the Time library http://www.arduino.cc/playground/Code/Time This will take some of the drudgery of timekeeping off your hands (no pun intended)
http://forum.arduino.cc/index.php?action=unread;boards=5,67,10,11,66,12,15,17,21,22,23,24,25,29;ALL

valkir

holy cow, thanks! it looks like that library will tackle the time-keeping for me. Now i just need to fit all the printing together and it's good to go! :)

robtillaart

Code: [Select]
if a =10, b++
if b =10, c++
if c = 10, a=b=c=0


should at least be

Code: [Select]

if (a == 10) b++;
if (b == 10) c++;
if (c == 10) a = b = c = 0

consistent spacing, ;'s and esp. note the difference between compare == and assignment =
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Go Up