Problem with drawing lines in TFT_eSPI

Hi. I have a 3.5" 480x320 TFT display with SPI connection to an ESP32. I'm using TFT_eSPI library to communicate with display and Setup 21 but with DC connected to GPIO16. I am drawing a TFT_GOLD line using drawLine. After that, I'm drawing another line but TFT_TRANSPARENT to clear that line. My problem is that a green-ish trace is left behind as you can see in images. I tried using TFT_BLACK but it doesn't do anything. here is my code:

#include <TFT_eSPI.h>
#include <SPI.h>
#include "dial.h"

TFT_eSPI tft = TFT_eSPI();
TFT_eSprite needle = TFT_eSprite(&tft);
TFT_eSprite label = TFT_eSprite(&tft);

void setup() {
  Serial.begin(115200);

  tft.init();
  tft.setRotation(3);
  tft.fillScreen(TFT_BLACK);

  tft.setSwapBytes(true);
  tft.pushImage(0,120,480,80,dial);

  needle.setColorDepth(8);
  needle.createSprite(320,50);
}

int getTopX(int speed) {
  double tmp = speed * 290;
  int topX = (int)(tmp/60 + 0.5);
  return topX;
}

int getBottomX(int speed) {
  double tmp = speed * 160;
  int topX = (int)(tmp/60 + 65.5);
  return topX;
}

void drawNeedle(int last_speed, int speed) {
  // clearing last line:
  int ltopx = getTopX(last_speed);
  int lbtmx = getBottomX(last_speed);
  needle.drawLine(lbtmx,50,ltopx,0, TFT_TRANSPARENT);

  needle.setTextColor(TFT_TRANSPARENT);
  needle.drawString(String(last_speed), 40,60, 7);
  needle.setTextColor(TFT_WHITE);
  needle.drawString(String(speed),40,60,7);

  // drawing new line:
  int topx = getTopX(speed);
  int btmx = getBottomX(speed);
  needle.drawLine(btmx,50,topx,0,TFT_WHITE);

  needle.pushSprite(90,200, TFT_BLACK);
}

int lspeed=0;
void loop() {
  for (int speed= 0; speed < 61; speed++) {
    int start = millis();
    drawNeedle(lspeed, speed);
    lspeed = speed;
    Serial.println(String(millis() - start));
  }
}

the problem i have is that drawing each line/speed should not take mor than 80 ms. currently it takes around 35, so everything is good (speed number not showing is not a problem as it is drawn outside of sprite dimensions. I will fix that later). here are the images:

TFT_TRANSPARENT (Don't mind double lines. That's because i took this image while it was drawing a new line.):

TFT_BLACK (IRL all lines are same color IDK why it is like this in the image:

You must store the old line values, change the color to background (green?), and draw the old line values again... then draw the new line values (white?)

line is rendered by it's speed (so the value you say is the speed) which is saved in the main loop (line 59) and took as a parameter in drawNeedle (line 35). I also fill screen with TFT_BLACK in setup function (line 14) so background should be black. When I try to draw the line with background color, second image appears.

I found the solution. as you can see in line 45, I am preventing TFT_eSPI to update pixels with color black (third parameter in needle.pushSprite(90,200,TFT_BLACK);) and that's why @xfpd 's answer doesn't work.

If you "draw a line with the background color, a second image appears" you are not drawing with the background color. Post #2 is the solution... where I noted green and white being used. You fixated on black as the background color. I did not.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.