Pages: [1]   Go Down
Author Topic: Updating data displayed only if it changes?  (Read 543 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Sr. Member
****
Karma: 4
Posts: 289
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

Code:
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:

Code:
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?

Logged

Arduino Uno;
Mega328

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49040
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

East Anglia (UK)
Online Online
Faraday Member
**
Karma: 114
Posts: 4226
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

0
Offline Offline
Sr. Member
****
Karma: 4
Posts: 289
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Arduino Uno;
Mega328

Offline Offline
Edison Member
*
Karma: 65
Posts: 2499
Now, More Than Ever
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Isn't it a matter of
Code:
if (new_data != old_data)
{
  // print new_data
  // old_data = new_data
}
? ? ?
Logged

"Hello, I must be going..."
"You gotta fight -- for your right -- to party!"
Don't react - Read.
"Who is like unto the beast? who is able to make war with him?"

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49040
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Sr. Member
****
Karma: 4
Posts: 289
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Isn't it a matter of
Code:
if (new_data != old_data)
{
  // print new_data
  // old_data = new_data
}
? ? ?

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

Arduino Uno;
Mega328

Pages: [1]   Go Up
Jump to: