Weird characters on my display.

Hi all, this is my first post here but I have used this forum at great length to solve many problems I have faced - so thank you to all those who have unknowingly helped.

There is a new problem I face that I have not been able to solve - one that is a little difficult to search for so I am posting here.

Essentially, I have some odd characters showing on my screen where there should be none. I have attached two images which show what I mean:

ard1.jpg shows what the problem looks like

ard2.jpg shows what it should look like (though they are slightly different given that they were taking at slightly different stages of development, it should be clear what the problem is)

The hardware I am using is pretty much the really cheap, eBay versions of a 3.2 touch LCD, Arduino Mega2560 and display shield.

My code is below, and undoubtedly, there will be all sorted of issues with structure, syntax, wasted memory and so on. Since this is my first ever code I expect it to, in many ways, suck, but I would be VERY happy to hear some advice, particularly on reading the DS18B20 sensors - this seems to take ages and I cant seem to get it to happen faster.

I have commented out some items, just to check they werent causing the problem, so as of right now, the issue is still there. I have included the font files, just in case they are what is causing the problem. The font files all came from the Rinky-Dink website.

Thanks, my code is below.

#include <SPI.h>
#include <SD.h>
#include <OneWire.h>
#include <DallasTemperature.h>

// This program requires the UTFT library.
#include <UTFT.h>

// Declare which fonts we will be using
extern uint8_t SevenSegNumFont[];
extern uint8_t SevenSegSmall[];
extern uint8_t BigSymbols[];
extern uint8_t SmallSymbols[];
extern uint8_t arial[];
extern uint8_t arialB[];



// Remember to change the model parameter to suit your display module!
UTFT tft(ITDB32S,38,39,40,41);

// Data wire is plugged into pin 2 on the Arduino
#define ONE_WIRE_BUS 8
#define TEMPERATURE_PRECISION 9
#define BAUD_RATE 115200
 
// (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

File myfile;

int x=0;
float inTemp;
float outTemp;
float ambientTemp;
float EGT;
float effValue;
int effValueScreen;

// For the pressure sensor (MPX5500DP)
int rawValue = 0; // Variable stores value coming from the sensor
int offset = 38; // zero pressure adjust
int fullScale = 500; // max pressure (span) adjust
int boost; // final pressure

//For the plotting of the graph
int previous_boostYaxis;
int boostY_axis=238;
int previous_ambientTempYaxis;
int ambientTempY_axis=238;
int previous_inTempYaxis;
int inTempY_axis=238;
int previous_outTempYaxis;
int outTempY_axis=238;
int x_axis=0;
// position of the line on screen
int xPos = 0;
int plot;



void setup(void)
{
  // start serial port
  Serial.begin(115200);
   
  // Use this initializer if you're using a 1.8" TFT
  
  tft.InitLCD(); // initialize screen
  tft.clrScr();

  // Start up the OneWire library
  sensors.begin(); 
  sensors.setResolution(9);
  //setupSD();
  
// write the static content to the screen
  // set the font color to white
  tft.setColor(255, 255, 255);
  // draw some boxes
  tft.setColor(80,80,80);
  tft.fillRect(0,0,319, 30);
  tft.setColor(150,150,150);
  tft.drawLine(0,30,319,30);
  // set the font size
  tft.setFont(arialB);
  // write, colour & position the text labels
  tft.setColor(0, 255, 0);
  tft.print("Amb", 2, 40);
  tft.setColor(255, 0, 0);
  tft.print("Pre", 110, 40);
  tft.setColor(0, 0, 255);
  tft.print("Post", 220, 40);
  tft.setColor(200, 200, 200);
  tft.print("Eff (%)", 20, 70);
  tft.print("Boost", 205, 70);
  tft.setColor(150,150,150);
  tft.drawLine(0,148,319,148);
  tft.setColor(30,30,30);
  tft.fillRect(0,149,319,239);
}

void checkX_axis(){
  x_axis++;
  if(x_axis==319){ //returns the graph to the left of the screen to scroll across again 
    tft.setColor(30,30,30);
    tft.fillRect(0,149,2,239);
    x_axis=0;
  }
}

void graphErase(){ 
  tft.setColor(150,150,150);
  tft.drawLine(x_axis+12, 149,x_axis+12,239);
  tft.setColor(30,30,30);
  tft.fillRect(x_axis+1, 149,x_axis+11,239);
}

void plotBoost(int previous_Yaxis,int y_axis){
  tft.setColor(200, 200, 200);
  tft.drawLine(x_axis-1,previous_boostYaxis , x_axis, boostY_axis); // x axis
}

void plotAmbientTemp(int previous_ambientTempYaxis,int ambientTempY_axis){
  tft.setColor(0, 255, 0);
  tft.drawLine(x_axis-1,previous_ambientTempYaxis , x_axis, ambientTempY_axis); // x axis
}

void plotInTemp(int previous_inTempYaxis,int inTempY_axis){
  tft.setColor(255, 0, 0);
  tft.drawLine(x_axis-1,previous_inTempYaxis , x_axis, inTempY_axis); // x axis
}

void plotOutTemp(int previous_outTempYaxis,int outTempY_axis){
  tft.setColor(0, 0, 255);
  tft.drawLine(x_axis-1,previous_outTempYaxis , x_axis, outTempY_axis); // x axis
}

//------------------   READ SENSORS AND PERFORM EFFICIENCY CALC   ----------------------------
void loop(void)
{
  sensors.requestTemperatures(); // Send the command to get temperatures
  inTemp = (sensors.getTempCByIndex(0)); //Put sensor data into variables
  outTemp = (sensors.getTempCByIndex(1));
  ambientTemp = (sensors.getTempCByIndex(2));
  effValue = ((inTemp-outTemp) / (inTemp-ambientTemp)*100); //calculate cooler efficiency
  effValueScreen = effValue;
  effValueScreen = constrain(effValueScreen, 0, 100 ); //Hold value (percentage) to within 0-100


//------------------       READ BOOST THEN GRAPH IT     ------------------------------
// For reading the boost...
  rawValue = analogRead(A0);
  boost = (rawValue - offset) * 500.0 / (fullScale - offset); // pressure conversion
  boost=map(boost, 0, 300, 0, 150);
  boost = constrain(boost, 0, 30 ); //Hold value (psi) to within 0-30

// For graphing the boost...
  checkX_axis(); //Check to make sure the 'x' axis is still on-screen
  
  previous_boostYaxis=boostY_axis;
  boostY_axis=(239-boost*3); 
  plotBoost(previous_boostYaxis,boostY_axis);
  
  previous_inTempYaxis=inTempY_axis; 
  inTempY_axis=(239-inTemp*1.5); 
  plotInTemp(previous_inTempYaxis,inTempY_axis);

  previous_outTempYaxis=outTempY_axis; 
  outTempY_axis=(239-outTemp*1.5);
  plotOutTemp(previous_outTempYaxis,outTempY_axis);
  
  previous_ambientTempYaxis=ambientTempY_axis; 
  ambientTempY_axis=(239-ambientTemp*1.5);
  plotAmbientTemp(previous_ambientTempYaxis,ambientTempY_axis);

  graphErase();
  
//setup screen variable colour scheme
  tft.setFont(SevenSegSmall);
  tft.setColor(255, 255, 255);
  tft.setBackColor(0,0,0);

  //print variable values to screen
  tft.printNumI(ambientTemp, 60, 35);
  tft.printNumI(inTemp, 165, 35);
  tft.printNumI(outTemp, 285, 35);

  //Efficiency and Boost variables get a bigger font
  tft.setFont(SevenSegNumFont);
  tft.printNumI(effValue, 35, 90);
  tft.printNumI(boost, 240, 90);

 // writeSD();

//----------------  SETUP THE TOUCH AREAS OF THE SCREEN FOR PAGING   ---------------
  //tft.setFont(BigSymbols);
  //tft.setColor(190,190,190);
  //tft.setBackColor(80,80,80);
  //tft.print("R", 0,0);  //arrow left
  //tft.print("S", 288,0);  //arrow right
}

void setupSD(void){
  Serial.print("Initializing card...");  
  // declare default CS pin as OUTPUT
   pinMode(53, OUTPUT);
   
  if (!SD.begin(4)) {
    Serial.println("initialization of the SD card failed!");
    return;
  }
  Serial.println("initialization of the SDcard is done."); 
  
}



void writeSD(){
   
   myfile = SD.open("sensordata.txt", FILE_WRITE);
    
  if (myfile)
  {
    
    Serial.print(myfile);
    Serial.print("Writing to the text file...");
    myfile.println("Congratulations! You have successfully wrote on the text file.");

    
  
    myfile.close(); // close the file:
    Serial.println("done closing.");
  } else
  {
    // if the file didn't open, report an error:
    Serial.println("error opening the text file!");
  }
  
  // re-open the text file for reading:
  myfile = SD.open("textFile.txt");
  if (myfile)
  {
    Serial.println("textFile.txt:");
    
    // read all the text written on the file
    while (myfile.available()) 
    {
      Serial.write(myfile.read());
    }
    // close the file:
    myfile.close();
  } else 
  {
    // if the file didn't open, report an error:
    Serial.println("error opening the text file!");
  }
}

arial.c (16.1 KB)

arialB.c (16.1 KB)

BigSymbols.c (60.6 KB)

SevenSegmentSmall.c (3 KB)

SevenSegSmall.c (2.99 KB)

SmallSymbols.c (16.1 KB)

Thank you for remembering to use code tags, and for at least trying to give us detailed information.

Unfortunately, there is something important which you did not remember: you did not post the images (ard1.jpg and ard2.jpg) showing the problem.

Apologies, I thought I added them with the font libraries.

They are included in this post.

Nothing jumping out at me as to the cause. I think graphErase() seems to be writing past the edge of the screen, but I doubt it has anything to do with it.

Is your sketch otherwise working fine, just these characters are incorrectly displayed? I'll assume it is for the moment.
Are the characters immediately wrong, or does the problem occur after a time?
Looking at the characters, it is reasonable to assume that they are being read from the wrong block of memory. ie. It is as if SevenSegNumFont is pointing to the wrong place in flash, and seeing some of the other fonts, but also some other maybe program code or something. But how?

I would ask you first to enable "show verbose output during compilation" and "compiler warnings all" in your IDE preferences. Close the IDE down, then reopen and recompile. Save (and post) the full compile output. Maybe there is something interesting there, maybe not.

SevenSegNumFont seems to be the only built in font that you are using. What happens when you replace it with SmallFont or BigFont? I'm guessing it doesn't work. What happens when you replace it with one of the user added fonts arial? I'm guessing it works.

Have you changed anything in your configuration (IDE, library, whatever) since the version that produced the working screenshot?
Is the code that you posted the exact code that produced the non-working screenshot?

So it appears that I have solved the problem.

I had some issues previously with my calculation of the efficiency value; when it was being handled as int, it would not calculate properly. So I changed it to float which sorted it.

But then the value on-screen was not quite right (can't remember the detail), so I have a line in the code that moved the float back to int. The intention was then to display the int value but I had forgotten to change the tft.print(); to the new int variable.

You can see what I mean in the first few lines of void loop(void). Then after the call to graphErase();, there are three small sections, and in the third I tell the TFT to display the OLD value, rather than the new int.

An odd outcome - something that i fell across by chance, but glad it is sorted.

Appreciate the time of those who looked over it and for your responses.

If I can ever assist someone I will, though I am still such an amateur my advice would be far from reliable!

Thanks again!

arduarn:
.......

Looking at the characters, it is reasonable to assume that they are being read from the wrong block of memory. ie. It is as if SevenSegNumFont is pointing to the wrong place in flash, and seeing some of the other fonts, but also some other maybe program code or something. But how?
........

Arduarn - your response is possibly the correct explanation of the problem. In trying to display a float variable, it looked in an area of memory that held something else in it. Could this be right?

Finally, sorry about so many messages!

I have 'zero's' on my display (see attachment). Can someone please show me how to avoid these leading zeros?

synt4ks:
Arduarn - your response is possibly the correct explanation of the problem. In trying to display a float variable, it looked in an area of memory that held something else in it. Could this be right?

My response might describe the symptoms, but I don't understand how the float could be the cause. I did notice the float parameter to long function, but thought nothing of it.
You've passed a float parameter to a function which expects a long. The compiler will do an implicit conversion which will basically truncate the float and pass the value as a long to the function. The function will only see a long and everything should work as normal in this case.
If the compiler had been unable to do the conversion, the code would not have compiled in the first place.

If that genuinely is the cause, then great, that brings you further on, but I'm scratching my head.

synt4ks:
I have 'zero's' on my display (see attachment). Can someone please show me how to avoid these leading zeros?

You can only display what printNumI() allows you to. It does have a couple of additional parameters though, if you haven't yet tried them:

printNumI(num, x, y[, length[, filler]]);
Print an integer number at the specified coordinates.
You can use the literals LEFT, CENTER and RIGHT as the x-coordinate to align the string on the screen.
Parameters:
Usage:
num:
x:
y:
length:
the value to print (-2,147,483,648 to 2,147,483,647) INTEGERS ONLY
x-coordinate of the upper, left corner of the first digit/sign
y-coordinate of the upper, left corner of the first digit/sign

minimum number of digits/characters (including sign) to display
filler:
filler character to use to get the minimum length. The character will be inserted in front
of the number, but after the sign. Default is ' ' (space).
myGLCD.printNumI(num,CENTER,0); // Print the value of “num”

If it still doesn't do what you want, or is in some way buggy, then you could always modify the library itself.

synt4ks:
Finally, sorry about so many messages!

I have 'zero's' on my display (see attachment). Can someone please show me how to avoid these leading zeros?

In a previous reply, you told us that you had made changes to your code.
What does your code look like now, with those changes?

odometer:
In a previous reply, you told us that you had made changes to your code.
What does your code look like now, with those changes?

Sorry, I didn't see your response until now and have made too many changes for it to be worth posting again.

I thank you all for your help though!

synt4ks:
Sorry, I didn't see your response until now and have made too many changes for it to be worth posting again.

The "too many changes" are exactly what make it worth posting again.
If we don't see what you have now, then we can't help you with what you have now.
Please make a new post to show us your changes. (Do not go back and edit the original post.)

Well he did end up posting the code - just in another thread.
But for completeness I'll repost it here for him:

#include <SPI.h>
#include <SD.h>
#include <OneWire.h>
#include <DallasTemperature.h>

// This program requires the UTFT library.
#include <UTFT.h>
#include <UTFT_DLB.h>

// Declare which fonts we will be using
extern uint8_t SevenSeg[];
extern uint8_t SevenSegSmall[];
extern uint8_t BigSymbols[];
extern uint8_t SmallSymbols[];
extern uint8_t DejaVuSans18[];
extern uint8_t DejaVuSans24[];


// Remember to change the model parameter to suit your display module!
UTFT_DLB tft(ITDB32S,38,39,40,41);

// Data wire is plugged into pin 2 on the Arduino
#define ONE_WIRE_BUS 8
#define TEMPERATURE_PRECISION 9
#define BAUD_RATE 115200
 
// (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

int x=0;
float inTemp;
float outTemp;
float ambientTemp;
float EGT;
float effValue;
int effValueScreen;

// For the pressure sensor (MPX5500DP)
int rawValue = 0; // Variable stores value coming from the sensor
int offset = 38; // zero pressure adjust
int fullScale = 500; // max pressure (span) adjust
float boost; // final pressure

//For the plotting of the graph
int previous_boostYaxis;
int boostY_axis=238;
int previous_ambientTempYaxis;
int ambientTempY_axis=238;
int previous_inTempYaxis;
int inTempY_axis=238;
int previous_outTempYaxis;
int outTempY_axis=238;
int x_axis=0;
// position of the line on screen
int xPos = 0;
int plot;

File dataFile;
const int chipSelect = 53;


void setup(void)
{
  // start serial port
  Serial.begin(115200);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  
// Initialize screen
  tft.InitLCD();
  tft.clrScr();

// Start up the OneWire library
  sensors.begin();
  sensors.setResolution(9);

// write the static content to the screen
  // set the font color to white
  tft.setColor(255, 255, 255);
  // draw some boxes
  tft.setColor(80,80,80);
  tft.fillRect(0,0,319, 30);
  tft.setColor(150,150,150);
  tft.drawLine(0,30,319,30);
  // set the font size
  tft.setFont(DejaVuSans18);
  // write, colour & position the text labels
  tft.setColor(0, 255, 0);
  tft.print("Amb", 5, 40);
  tft.setColor(255, 0, 0);
  tft.print("Pre", 120, 40);
  tft.setColor(0, 0, 255);
  tft.print("Post", 230, 40);
  tft.setColor(200, 200, 200);
  tft.print("Eff (%)", 60, 70);
  tft.print("Boost", 225, 70);
  tft.setColor(150,150,150);
  tft.drawLine(0,148,319,148);
  tft.setColor(30,30,30);
  tft.fillRect(0,149,319,239);


// Check the SD Card
  Serial.print("Initializing SD card...");

  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");  
}





//------------------   READ SENSORS AND PERFORM EFFICIENCY CALC   ----------------------------
void loop(void)
{
  unsigned long start=millis();
  
  sensors.requestTemperatures(); // Send the command to get temperatures
  inTemp = (sensors.getTempCByIndex(0)); //Put sensor data into variables
  outTemp = (sensors.getTempCByIndex(1));
  ambientTemp = (sensors.getTempCByIndex(2));
  effValue = ((inTemp-outTemp) / (inTemp-ambientTemp)*100); //calculate cooler efficiency
  effValueScreen = effValue; //move float value to int
  effValueScreen = constrain(effValueScreen, 0, 100 ); //Hold value (percentage) to within 0-100


//------------------       READ BOOST THEN GRAPH IT     ------------------------------
// For reading the boost...
  rawValue = analogRead(A0);
  boost = (rawValue - offset) * 500.0 / (fullScale - offset); // pressure conversion
  boost=map(boost, 0, 300, 0, 150);
  boost = constrain(boost, 0, 30 ); //Hold value (psi) to within 0-30

// For graphing the boost...
  checkX_axis(); //Check to make sure the 'x' axis is still on-screen
  
  previous_boostYaxis=boostY_axis;
  boostY_axis=(239-boost*3);
  plotBoost(previous_boostYaxis,boostY_axis);
  
  previous_inTempYaxis=inTempY_axis;
  inTempY_axis=(239-inTemp*1.5);
  plotInTemp(previous_inTempYaxis,inTempY_axis);

  previous_outTempYaxis=outTempY_axis;
  outTempY_axis=(239-outTemp*1.5);
  plotOutTemp(previous_outTempYaxis,outTempY_axis);
  
  previous_ambientTempYaxis=ambientTempY_axis;
  ambientTempY_axis=(239-ambientTemp*1.5);
  plotAmbientTemp(previous_ambientTempYaxis,ambientTempY_axis);

  graphErase();
  
//setup screen variable colour scheme
  tft.setColor(255, 255, 255);
  tft.setBackColor(0,0,0);

  //print variable values to screen
  tft.setFont(SevenSegSmall);
//  String ambientTempStr=String(ambientTemp);
  tft.printNumI(ambientTemp, 60, 35);
  tft.printNumI(inTemp, 165, 35);
  tft.printNumI(outTemp, 285, 35);

  //Efficiency and Boost variables get a bigger font
  tft.setFont(SevenSeg);
  tft.printNumI(effValueScreen, 35, 90,1);
  tft.printNumI(boost, 240, 90);

  writeSD();

//----------------  SETUP THE TOUCH AREAS OF THE SCREEN FOR PAGING   ---------------
  //tft.setFont(BigSymbols);
  //tft.setColor(190,190,190);
  //tft.setBackColor(80,80,80);
  //tft.print("R", 0,0);  //arrow left
  //tft.print("S", 288,0);  //arrow right


  unsigned long finish=millis();
  unsigned long total=finish-start;
  Serial.print("Time: ");  
  Serial.println(total);
  
}

void checkX_axis(){
  x_axis++;
  if(x_axis==319){ //returns the graph to the left of the screen to scroll across again
    tft.setColor(30,30,30);
    tft.fillRect(0,149,2,239);
    x_axis=0;
  }
}

void graphErase(){
  tft.setColor(150,150,150);
  tft.drawLine(x_axis+12, 149,x_axis+12,239);
  tft.setColor(30,30,30);
  tft.fillRect(x_axis+1, 149,x_axis+11,239);
}

void plotBoost(int previous_Yaxis,int y_axis){
  tft.setColor(200, 200, 200);
  tft.drawLine(x_axis-1,previous_boostYaxis , x_axis, boostY_axis); // x axis
}

void plotAmbientTemp(int previous_ambientTempYaxis,int ambientTempY_axis){
  tft.setColor(0, 255, 0);
  tft.drawLine(x_axis-1,previous_ambientTempYaxis , x_axis, ambientTempY_axis); // x axis
}

void plotInTemp(int previous_inTempYaxis,int inTempY_axis){
  tft.setColor(255, 0, 0);
  tft.drawLine(x_axis-1,previous_inTempYaxis , x_axis, inTempY_axis); // x axis
}

void plotOutTemp(int previous_outTempYaxis,int outTempY_axis){
  tft.setColor(0, 0, 255);
  tft.drawLine(x_axis-1,previous_outTempYaxis , x_axis, outTempY_axis); // x axis
}



void writeSD(){

  dataFile = SD.open("sensorData.txt", FILE_WRITE);
  Serial.println(dataFile);
  
  if (dataFile)
  {
    dataFile.println(ambientTemp);
    Serial.print(",");
    dataFile.print(inTemp);
    Serial.print(",");
    dataFile.print(outTemp);
    Serial.print(",");
    dataFile.print(effValue);
    Serial.print(",");
    dataFile.print(boost);
    Serial.print(",");
    dataFile.close(); // close the file:
    Serial.println("data written\n\n");
  } else
  {
    // if the file didn't open, report an error:
    Serial.println("\nerror opening the text file!");
  }
  
  // re-open the text file for reading:
  dataFile = SD.open("sensorData.txt");
  if (dataFile)
  {
    Serial.println("sensorData.txt:");
    
    // read all the text written on the file
    while (dataFile.available())
    {
      Serial.write(dataFile.read());
    }
    // close the file:
    dataFile.close();
  } else
  {
    // if the file didn't open, report an error:
    Serial.println("rror opening the file!\n");
  }
}

I notice he has stopped using the "built in to the library" font SevenSegNumFont...

Thanks Arduarn.

My code keeps changing as I learn new things and as I shift things around to keep the logic in my head as close to the logic in the code.

I maintain that I am probably doing a number of things poorly, but as I learn I will improve :slight_smile:

I no longer use the inbuilt SevenSegNum font because I need the decimal point, blank space etc from another font option.