Neet to speed up the project or do a back buffer...

Hello!
I am making a humidity and temp reading device witch both is going to send data to a MySensors gateway and display the information in a graph on a TFT screen.
The code is not complete yet, but I am starting to see that there will be an ugly presentation of the graph being drawn on the screen.
From the beginning I was using a Arduino Due, but the MySensors lib do not compile when I'm using the Due. So I had to switch to a Arduino Mega 2560 and then I saw the ugly presentation.
I am planing to read the temp and humidity values between once every 5 second to once every 30 min, depending how the settings is.
Is there anyway to optimize the code or is it possible to draw a "back buffer" and just switch the "old screen" against a new?

void screenUpd() {
  TFTscreen.fillScreen(0x0000);  //Clear screen to black

  
  TFTscreen.setTextColor(ST7735_GREEN);
  TFTscreen.setCursor(0, 0);
  TFTscreen.print("Humid %");
  TFTscreen.setCursor(0, 12);
  TFTscreen.print((float)DHT.humidity);

  TFTscreen.setTextColor(ST7735_RED);
  TFTscreen.setCursor(45, 0);
  TFTscreen.print("Temp");
  TFTscreen.setCursor(45, 12);
  TFTscreen.print((float)DHT.temperature);

  
  for (count=wTFT-2; count>=2; count--)  //Graph
    {
     TFTscreen.drawLine(count, map(sensorArray[count], 100, 0, 26, 125), count-1, map(sensorArray[count-1], 100,0, 25, 126), ST7735_GREEN);
     TFTscreen.drawLine(count, map(sensorArray2[count], 60, -20, 26, 125), count-1, map(sensorArray2[count-1], 60, -20, 25, 126), ST7735_RED);
    }  
    
  if (intersectEn == 1) {  //Draw Intersection if enabled.
  
    TFTscreen.setTextColor(ST7735_BLUE);
    TFTscreen.setCursor(80, 0);
    TFTscreen.print("Intersection");
    TFTscreen.setTextColor(ST7735_RED);
    TFTscreen.setCursor(105, 12);
    TFTscreen.print("C");
    TFTscreen.setTextColor(ST7735_GREEN);
    TFTscreen.setCursor(150, 12);
    TFTscreen.print("%");
  
    TFTscreen.setTextColor(ST7735_RED);
    TFTscreen.setCursor(85, 12);
    TFTscreen.print(sensorArray2[intersect]);
    TFTscreen.setTextColor(ST7735_GREEN);
    TFTscreen.setCursor(130, 12);
    TFTscreen.print(sensorArray[intersect]);
    TFTscreen.drawLine(intersect, 26, intersect, 125, ST7735_WHITE);

  }
  
  TFTscreen.drawRect(0, 25, 160, 102, ST7735_WHITE);  //Border
  TFTscreen.drawLine(1, map(50, 100, 0, 26, 125), 3, map(50, 100, 0, 26, 125), ST7735_GREEN);  //Index dot
  TFTscreen.drawLine(158, map(0, 60, -20, 26, 125), 156, map(0, 60, -20, 26, 125), ST7735_RED);
}

Thanks in advance.

BR Tim

Edit::
Forgot to mention that I am using this libs.
#include <Adafruit_GFX.h>
#include <gfxfont.h>
#include <Adafruit_ST7735.h>

Edit::
Forgot to mention that I am using this libs.

We would have known that if you had posted your code.

But I did.
It is the screen update that is slow. And I posted all the code for that.

TimpaH:
But I did.

No, you posted what is known as a "snippet"

I don´t know if anyone cares but I manage to make the scrolling graph a lot smoother.

void screenUpd() {
  testTime = micros();
  //TFTscreen.fillScreen(0x0000);  //Clear screen to black

  if (sensorArray[0] != sensorArray[1]) {
    TFTscreen.setTextColor(ST7735_GREEN);
    TFTscreen.fillRect(0, 12, 32, 8, ST7735_BLACK);
    TFTscreen.setCursor(0, 12);
    //TFTscreen.print((float)DHT.humidity);
    //TFTscreen.print((float)sensorArray[0]);
    TFTscreen.print(sensorArray[0]);
  }
  if (sensorArray2[0] != sensorArray2[1]) {
    TFTscreen.setTextColor(ST7735_RED);
    TFTscreen.fillRect(45, 12, 32, 8, ST7735_BLACK);
    TFTscreen.setCursor(45, 12);
    //TFTscreen.print((float)DHT.temperature);
    //TFTscreen.print((float)sensorArray2[0]);
    TFTscreen.print(sensorArray2[0]);
  }

  //TFTscreen.fillRect(1, 26, 158, 100, ST7735_BLACK);  //This is faster but not as "smooth" as drawing the graph in black before drawing the new graph
  for (count=wTFT-2; count>=2; count--)  //Graph
    {
     TFTscreen.drawLine(count, mapsensorArray[count+1], count-1, mapsensorArray[count], ST7735_BLACK);
     TFTscreen.drawLine(count, mapsensorArray2[count+1], count-1, mapsensorArray2[count], ST7735_BLACK);
     TFTscreen.drawLine(count, mapsensorArray[count], count-1, mapsensorArray[count-1], ST7735_GREEN);
     TFTscreen.drawLine(count, mapsensorArray2[count], count-1, mapsensorArray2[count-1], ST7735_RED);
    }  
    
  if (intersectEn == 1) {  //Draw Intersection if enabled.
  
    TFTscreen.setTextColor(ST7735_BLUE);
    TFTscreen.setCursor(80, 0);
    TFTscreen.print("Intersection");
    TFTscreen.setTextColor(ST7735_RED);
    TFTscreen.setCursor(105, 12);
    TFTscreen.print("C");
    TFTscreen.setTextColor(ST7735_GREEN);
    TFTscreen.setCursor(150, 12);
    TFTscreen.print("%");
  
    TFTscreen.setTextColor(ST7735_RED);
    TFTscreen.setCursor(85, 12);
    TFTscreen.print(sensorArray2[intersect]);
    TFTscreen.setTextColor(ST7735_GREEN);
    TFTscreen.setCursor(130, 12);
    TFTscreen.print(sensorArray[intersect]);
    TFTscreen.drawLine(intersect, 26, intersect, 125, ST7735_WHITE);

  }
  
  //TFTscreen.drawRect(0, 25, 160, 102, ST7735_WHITE);  //Border
  //TFTscreen.drawLine(1, map(50, 100, 0, 26, 125), 3, map(50, 100, 0, 26, 125), ST7735_GREEN);  //Index dot
  //TFTscreen.drawLine(158, map(0, 60, -20, 26, 125), 156, map(0, 60, -20, 26, 125), ST7735_RED);
  Serial.println(micros()-testTime);  //Shows the time to render the graph
}

void screenRend() {  //only write on setup and special occasions.
  TFTscreen.drawRect(0, 25, 160, 102, ST7735_WHITE);
  TFTscreen.setTextColor(ST7735_GREEN);
  TFTscreen.setCursor(0, 12);
  TFTscreen.print(sensorArray[0]);
  TFTscreen.setCursor(0, 0);
  TFTscreen.print("Humid %");
  TFTscreen.setTextColor(ST7735_RED);
  TFTscreen.setCursor(45, 0);
  TFTscreen.print("Temp");
  TFTscreen.setCursor(45, 12);
  TFTscreen.print(sensorArray2[0]);
}

Adding an image. :slight_smile:

Not sure if this helps but i have written a graphing function that plots temps, or whatever. Update is whenever you post new data to plot and there is soem code to keep the display from flickering during updates. You can have the code for free, the links to the graphing function is in the video. See around the 45 second mark for graphing of a thermistor reading.

I agree though, these displays are very slow, the seems to be no way to write to video memory first the post it to the screen--like my 0.96 OLED display can work.

PS, if you can use a small monochrome display, this one has blazing fast updates--fast enough for a 4kHz oscilloscope. Again feed code at the video links

Hope this helps.

KrisKasprzak:
Not sure if this helps but i have written a graphing function that plots temps, or whatever. Update is whenever you post new data to plot and there is soem code to keep the display from flickering during updates. You can have the code for free, the links to the graphing function is in the video. See around the 45 second mark for graphing of a thermistor reading.

https://www.youtube.com/watch?v=YejRbIKe6e0

I agree though, these displays are very slow, the seems to be no way to write to video memory first the post it to the screen--like my 0.96 OLED display can work.

PS, if you can use a small monochrome display, this one has blazing fast updates--fast enough for a 4kHz oscilloscope. Again feed code at the video links
https://www.youtube.com/watch?v=jYaWjMUfkMg

Hope this helps.

The new code is takes about 198ms to draw the graph.
When I used fillRect to erase the graph I was down at 150ms / update.
But it is a lot smoother to erase the graph by printing the old graph in black just before printing the new one.
You can´t even notice the graph updates. Im very happy with the result.

  for (count=wTFT-2; count>=2; count--)  //Graph
    {
     TFTscreen.drawLine(count, mapsensorArray[count+1], count-1, mapsensorArray[count], ST7735_BLACK);
     TFTscreen.drawLine(count, mapsensorArray2[count+1], count-1, mapsensorArray2[count], ST7735_BLACK);
     TFTscreen.drawLine(count, mapsensorArray[count], count-1, mapsensorArray[count-1], ST7735_GREEN);
     TFTscreen.drawLine(count, mapsensorArray2[count], count-1, mapsensorArray2[count-1], ST7735_RED);
    }

And I moved the map() function to the main loop and add the mapped value to a array instead of calculating the map 8 times every time the for loop runs.