Go Down

Topic: Simplest Arduino Timer (Read 300 times) previous topic - next topic

welshman

Had to clear an lcd display at a regular interval to remove invalid characters. Simplest solution i came up with was
Code: [Select]

if ((millis()/100)%10 ==1) {lcd.clear();}


which simply clears the screen every second.

Simple.

el_supremo

Code: [Select]
which simply clears the screen every second.
Maybe. But does it clear it ONCE every second?

If that statement is in the loop() function, it could potentially be clearing the screen dozens or even hundreds of times in a row because it will be true for 100 milliseconds out of every second (depends upon how long lcd.clear() takes).
Consider this:
Code: [Select]
void loop(void)
{
  if ((millis()/100)%10 ==1) {lcd.clear();}
}


Pete
Don't send me technical questions via Private Message.

welshman

#2
Sep 12, 2017, 11:14 pm Last Edit: Sep 13, 2017, 12:01 am by welshman
Had to clear an lcd display at a regular interval to remove invalid characters. Simplest solution i came up with was
Code: [Select]

if ((millis()/100)%10 ==1) {lcd.clear();}


which simply clears the screen every second.

Simple.
Code: [Select]
which simply clears the screen every second.
Maybe. But does it clear it ONCE every second?

If that statement is in the loop() function, it could potentially be clearing the screen dozens or even hundreds of times in a row because it will be true for 100 milliseconds out of every second (depends upon how long lcd.clear() takes).
Consider this:
Code: [Select]
void loop(void)
{
  if ((millis()/100)%10 ==1) {lcd.clear();}
}


Pete
In practice in the main loop it appears to simply do as stated and only runs ever second because millis 1 second decimal position is only 1 every 1 second. EDIT - i see what you mean, if the loop took less than 100th of a second to complete it could run it multiple times per second.

in that case you could include a second if statement that compared the last digit of millis to 0 as well... still would be pretty simple.

if ((millis() / 100) % 10 == 1 && millis() % 10 == 0)
{
        lcd.clear();
 }

el_supremo

NOPE. That will reduce the number of times per second that it could match but it still doesn't guarantee once per second.
In the original case, the lcd would be cleared when the last three digits of millis() have the form 1xx (where x is don't care) which occurs for 100 milliseconds of every second. Now you've changed it to when the last 3 digits match the form 1x0 which occurs for a total of 10 milliseconds every second. Even if you made it look for a match with 100, this still occurs for 1 millisecond every second and if lcd.clear() is faster than that it will still occur multiple times. You should not be writing code which makes assumptions like that.

Pete
P.S. Why is this in Exhibition/Gallery? It's not a working/tested/complete project nor even a good example. It doesn't do what was claimed. It probably belongs in Programming Questions.

P.P.S.
Quote
Had to clear an lcd display at a regular interval to remove invalid characters
Wouldn't it have been easier and better to prevent invalid characters being written in the first place?
Don't send me technical questions via Private Message.

Go Up