Updating data displayed only if it changes?

I've always had this question in my mind. When sending out data to be displayed on an LCD display (or in my case an OLED display) I have always wondered if there is anything wrong with hammering the registers repeatedly with the same data over and over again. Example:

void loop
{
  if(digitalRead(button))
  {
    display.clearDisplay();
    display.setCursor(0,0);
    display.print("Not Pressed");
    display.display();
  }
  else
  {
    display.clearDisplay();
    display.setCursor(0,0);
    display.print("Pressed");
    display.display();
  }
}

I hate that I am sending the same thing to the display over and over again when really I just need to send it once when the button is pressed and once when the button is released. Because of this I end up doing something that feels convoluted like:

boolean last_button_position = false;
void loop
{
  if(digitalRead(button) && !last_button_position)
  {
    display.clearDisplay();
    display.setCursor(0,0);
    display.print("Not Pressed");
    display.display();
    last_button_position = true;
  }
  
  if(!digitalRead(button) && last_button_position)
  {
    display.clearDisplay();
    display.setCursor(0,0);
    display.print("Pressed");
    display.display();
    last_button_position = false;
  }
}

Now given my propensity to always find the absolute worst way to do something programmatically, I can only assume that this is possibly the worst way to do this. What do you guys do to avoid constantly sending the same data to be displayed over and over again?

I have always wondered if there is anything wrong with hammering the registers repeatedly with the same data over and over again.

They don't know. They don't care.

The problem comes when the clearing and redisplay of data takes a while. If that time doesn't matter (the display doesn't flicker; the code doesn't block), then it doesn't matter. If it does, then clearing the screen and re-displaying the data only when there is a change can improve the performance of the overall program.

Changing only those parts of the display that have actually changed, rather than clearing the whole display can make a difference too. The code would be neater and be easier to maintain if the routine to display data were made into a function with the parameters being the data to display and the coordinates where it should be displayed.

So basically it doesn't matter unless I need my program to run faster. It does get a bit behind when reading serial information coming in so I guess I will put the test in there to see if I need to update the display or not.

Isn't it a matter of

if (new_data != old_data)
{
  // print new_data
  // old_data = new_data
}

? ? ?

Isn't it a matter of

They type of new_data and old_data determine whether the != operator is an appropriate one, but that's the general idea.

That's basically what my second code example above does. I ended up just making a boolean that keeps track of which of two states the system can be in. If the state is 0 and the boolean shows that we are displaying state as 1 then we update the display and vice versa. Seems to work fine for me.

I wondered if anyone had got any further with this topic?

Here in UK we have just quit Daylight Saving time and I realised my Arduino LCD clock was an hour out for several months of the year.

I sorted that thanks to GitHub - JChristensen/Timezone: Arduino library to facilitate time zone conversions and automatic daylight saving (summer) time adjustments..

But now my display has quite a flicker to it, which is
a) Distracting
b) I wonder about the effect on the display?

I attach an image as it wouldn't paste where I wanted it to.

Basically the display is:

Tue 1 Nov 2016
08:49:48 AM GMT

(GMT is Greenwich Mean Time - a hark back to the UK's Imperial Days!)

What would be nice would be if only the changes get output

  • that is easy with the first line as it only changes once a day
  • but rather more complex with the second line, where it would be good to only output the one or two (or more) digits which change

Maybe this is only an intellectual exercise but it has certainly got my brain ticking, and I wondered if anyone had sorted it (or similar)? - I have Googled the Forum and the web

LCD clock.jpg

Are we supposed to see the flickering in the snapshot? I didn't. Looked nice and steady to me.

Writing to the display only when there is new data to write is trivial. All you need to do is keep track of what you wrote last time.

Your code seems to have gone AWOL.

freddie43:
I wondered if anyone had got any further with this topic?

Here in UK we have just quit Daylight Saving time and I realised my Arduino LCD clock was an hour out for several months of the year.

I sorted that thanks to GitHub - JChristensen/Timezone: Arduino library to facilitate time zone conversions and automatic daylight saving (summer) time adjustments..

But now my display has quite a flicker to it, which is
a) Distracting
b) I wonder about the effect on the display?

We cannot locate the problem unless you post your code. And it will save us time if you begin by posting the entire sketch, not just the part you think is the problem.

By the way, I have made a clock using a similar display.
Here is the thread on it: Two-button clock - #6 by odometer - Exhibition / Gallery - Arduino Forum
I have not noticed any flicker on my clock's display, but then again, I did not include the Timezone or any similar library.