I'm working on a proof of concept device for taking inputs intended to light up idiot lights on a gauge cluster and put them on a display. I have a build that's working well enough to do a PoC run, but I wanted to make it a little less crappy. I've got a couple things like a splash screen and a clock because if this works out, a lot of this may go to the final version. I'm also very new at this and playing around.
Originally, I was using the void loop to watch for a digital pin to go high then display a message. That wound up with a lot of clumsy lcd.clear lines, it was slow, and it looked bad. I started looking for another way to do this and found interrupts, but I'm doing it wrong because it's not working.
Avoid using interrupts with writes to the LCD, that takes more time than you want to spend in an interrupt, and you will inevitably have the interrupt occur while doing an unrelated write to the LCD. You are also going to change the position of the LCD cursor in the interrupt, so that an LCD write after returning from the interrupt will go to the wrong position.
The general technique with displaying data on an LCD is to write all the fixed text to the screen once, then (as stated in reply #1) only update the data when it changes. You will need to be careful with positioning when doing this, and making sure the previous data is overwritten by the new data - you likely have already discovered that your method of displaying the hours and minutes will give odd results when the numbers change between single-digit and two-digit numbers, the leading spaces or zeros need to be displayed to keep the data field a constant width.
Hi,
To add to @paulpaulson solution, it would be worth erasing the old data with blank spaces, so when you print the new, if it has less digits/figures than the original print, you will not see the old print in the spaces not occupied by the new.
I hope that makes sense. (Just started my first coffee for the day )
if (oldValue!=newValue) {
oldValue=newValue;
lcd.setCursor (to where old value is printed);
lcd.print(" "); // or what ever number of spaces your value occupies.
lcd.setCursor(to where old value was printed);
lcd.print(newValue);
}
Even better is to format your data so that each time you write it, it always completely overwrites the previous value. This is even more efficient and produces even less flicker.
Note that 'buffer' always needs to be one character longer than you expect, because that is needed for the null character that marks the end of a C-string.
There are many useful formatting controls you can use with sprintf(). Here, I used
% ... insert a value at this point in the string
d ... print the value in decimal
2 ... print the value padded to 2 characters. Normally spaces are used but..
0 ... pad the value on the left with zeros rather than spaces