printing previous sensor value prior to current sensor value

Hi guys,

I'm looking for a little help in printing a previous sensor value on the ILI9340 tft prior to the current value. Instead of filling the screen with black to 'refresh' it I want to use the 'print previous' method to 'refresh' only the pixels that have changed etc.

Currently I am drawing a small black rectangle over the text but cant for the life of me figure out how to store the previous value etc.

I have searched around, and found some solutions but as I'm fairly new to C++ and Arduino (2 weeks), my lack of knowledge seems to be the downfall!

Below is the function I wish to modify:

void printTemperature(DeviceAddress deviceAddress, int x, int y, int Set)
{

float tempC = sensors.getTempC(deviceAddress);
float setTemp = Set;
String sepText = "   ";

tft.setCursor(x, y);
tft.fillRect(x, y, 80, 10, ILI9340_BLACK);
tft.setTextColor(ILI9340_WHITE);
tft.print(setTemp);
tft.print(sepText);
tft.print(tempC);

}

The whole sketch is rather large, so if any other information is required, please let me know.

Cheers

Mark

Currently I am drawing a small black rectangle over the text.

Yes that is the way it is normally done.

but cant for the life of me figure out how to store the previous value etc

Basically you can't except by copying all the pixels from that area of the display's buffer into an array. This will take much longer to do than draw the black box so I am not sure it is worth it.

I'm not 100% sure I understand what you're trying to do. It seems you have already displayed the current value, but then before you display the new current value, you want to move the old current value (which is now the previous value) and display it as well?

If so, perhaps before you read the new current value into current value, save the old current value as previous. Then display previous in its place (effectively moving he old current value) and the new current in its place.

That made sense to me when I wrote it.... 8)

That made sense to me when I wrote it

Probably one of those write only sentences. :wink:

Mind you if you saved the previous value you could print that to the display in black before changing it to white to print the new reading. That would still take longer than drawing a black box though.

Thanks for your responses guys.

Grumpy_Mike:
Mind you if you saved the previous value you could print that to the display in black before changing it to white to print the new reading. That would still take longer than drawing a black box though.

That is EXACTLY what I'm trying to do. My theory is that writing the previous value in black first is more efficient as its only redrawing the exact number of pixels required as opposed to a fixed number of pixels with a black rectangle.

JimboZA:
I'm not 100% sure I understand what you're trying to do. It seems you have already displayed the current value, but then before you display the new current value, you want to move the old current value (which is now the previous value) and display it as well?

Yep, you got it, but previous value in black first, then current value in white.

Any modifications to the code would be most welcome :slight_smile:

but previous value in black first, then current value in white.

I didn't get that part first, no

JimboZA:

but previous value in black first, then current value in white.

I didn't get that part first, no

apologies, I'll try to explain it better:

The tft I'm using does not automatically clear or 'refresh' every time a new sensor reading is taken.

So I have a black screen background with white text.

Every time a new sensor reading is taken and the value written to the screen, it is done so over the top of the previous value (resulting in an unreadable mass of white pixels).

What I propose to do is write the previous value to the screen in black first (thus technically erasing the area of any white text), and then write the new sensor value to the screen in the same place in white.

Hope that helps explain it a little better :slight_smile:

You need to define a static varialbe called say lastReading in your function. Initially set it to something impossible like -1.
Then you need to test this value to see if it is -1, if it is then draw the black rectangle if not then print the value of it in reverse colours on the screen.
Then take your reading and print it as normal.
The last thing in your function you set lastReading to the value of the reading you have just taken.

Declaring a variable static means it carries it's value from one call to the next.

Thanks for the explanation Mike. It does make sense, but alas, I cant seem to get it to work.

Here is my revised code:

void printTemperature(DeviceAddress deviceAddress, int x, int y, int Set)
{
#define previousTempC -1

float tempC = sensors.getTempC(deviceAddress);
float setTemp = Set;
String sepText = "   ";

if (previousTempC != tempC) {

  Serial.print(tempC);
  Serial.print(previousTempC);
  
  tft.setCursor(x, y);
  tft.setTextColor(ILI9340_BLACK);
  tft.print(setTemp);
  tft.print(sepText);
  tft.print(previousTempC);


  tft.setCursor(x, y);
  tft.setTextColor(ILI9340_WHITE);
  tft.print(setTemp);
  tft.print(sepText);
  tft.print(tempC);

  previousTempC == tempC;
}
}

it is still returning the previousTempC value as -1.

Also, if I replace the double '==' with a single '=' I get an error

No you need to define the previous temprature as a static float.

Grumpy_Mike:
No you need to define the previous temprature as a static float.

Thanks Mike, that worked a treat. my code now looks like this:

void printTemperature(DeviceAddress deviceAddress, int x, int y, int Set)
{

static float previousTempC = -100;
float tempC = sensors.getTempC(deviceAddress);

float setTemp = Set;
String sepText = "   ";

if (previousTempC != tempC) {

  Serial.println(tempC);
  Serial.println(previousTempC);
  Serial.println();
  
  tft.setCursor(x, y);
  tft.setTextColor(ILI9340_BLACK);
  tft.print(setTemp);
  tft.print(sepText);
  tft.print(previousTempC);


  tft.setCursor(x, y);
  tft.setTextColor(ILI9340_WHITE);
  tft.print(setTemp);
  tft.print(sepText);
  tft.print(tempC);

  previousTempC = tempC;
}
}

There is one small issue which I overlooked - while the sensors are initialising the first of the two probes returns thje value of previousTempC (in this case -100), and then on the second iteration it is correct. This is all well and good in the serial monitor but with printing the pixels in is still screwed up.

Heres the serial monitor bit:

28.25
-100.00

29.19
28.25


28.25
29.19

29.25
28.25

The pairs of readings are current and previous for each of the two probes.

If you have more than one device calling this function then the static float needs to be an array, because you need a different "previous value" for each sensor.

You need to pass in a sensor number from 0 to what ever is the highest sensor. This code assumes you have three sensors and you pass in the numbers 0 to 2:-

void printTemperature(DeviceAddress deviceAddress, int x, int y, int Set, int sensorNumber)
{

static float previousTempC[] = {-100,-100,-100}; // for three sensors
// then when it comes to it
if (previousTempC[sensorNumber]  != tempC) {

// when you print the previous value use
Serial.println(previousTempC[sensorNumber]);

// and then at the end
previousTempC[sensorNumber] = tempC;

Hi Mike, thats great, thankyou ever so much for that.

The full working function is now:

void printTemperature(DeviceAddress deviceAddress, int x, int y, int Set, int sensorNumber)
{

static float previousTempC[] = {-100,-100};
float tempC = sensors.getTempC(deviceAddress);

float setTemp = Set;
String sepText = "   ";

if (previousTempC[sensorNumber]  != tempC) {

  Serial.println(tempC);
  Serial.println(previousTempC[sensorNumber]);
  Serial.println();
  
  tft.setCursor(x, y);
  tft.setTextColor(ILI9340_BLACK);
  tft.print(setTemp);
  tft.print(sepText);
  tft.print(previousTempC[sensorNumber]);


  tft.setCursor(x, y);
  tft.setTextColor(ILI9340_WHITE);
  tft.print(setTemp);
  tft.print(sepText);
  tft.print(tempC);

  previousTempC[sensorNumber] = tempC;
}
}

I still have a lot of learning to do, but this has helped quite abit.

Cheers! :):slight_smile: