Help With Displaying Long Int's as Char Arrays on a TFT screen

Hi All,

I’m new to displaying things on a TFT screen and I’m using lines similar to this to display just plain text on the screen. Turns out that it want’s everything in a char array, so i can’t print long int’s direct to the screen and I have to use ‘sprintf’ instead. Text like this is fine though.

//Prints Hello World to the screen at position 70 x 96

TFTscreen.text("Hello World",70,96);

I’m having an issue where i have a bit of script that goes off to get some data from a server and i only run it every X many mins/hours/seconds (what ever i set it to). I want a count down on the screen till when it’s going to run next, but it seems that my code just hates life and displays ‘00’ seconds instead of the actual second count. Minutes on the other hand seem to be working fine!

Truncated code that you might be able to go ‘Oh - thats whats wrong’
Whats interesting about this is that the seconds prints correctly to the serial but not to the screen, the screen just returns ‘00 Seconds’

Also, you’ll see 3 IF lines in the code as below. Last one will print seconds fine, second one wont…

    if (checkHrInt > 0) sprintf(checkSec,"%02iH %02iM",checkHrInt,checkMinInt);
    else if (checkMinInt > 0) sprintf(checkSec,"%02iM %02iS",checkMinInt,checkSecInt);
    else sprintf(checkSec,"%02i Seconds",checkSecInt);

checkMillis is defined like this every time the ‘Get From Server’ script runs

if (checkMillis <= millis())
  {
     // Run script to get stuff here
     checkMillis = millis() + 3010000; //Set re-check value here
  }

Header

long checkMillis = 0;
char checkSec[64];
long checkDayInt = 0;
long checkSecInt = 0;
long checkMinInt = 0;
long checkHrInt = 0;

– NEXT BIT IN THE LOOP –

    checkSecInt = (checkMillis-millis())/1000; //convect milliseconds to seconds
    checkMinInt=checkSecInt/60; //convert seconds to minutes
    checkHrInt=checkMinInt/60; //convert minutes to hours
    checkDayInt=checkHrInt/24; //convert hours to days
    checkSecInt=checkSecInt-(checkMinInt*60); 
    checkMinInt=checkMinInt-(checkHrInt*60);
    checkHrInt=checkHrInt-(checkDayInt*24);

    TFTscreen.text(checkSecInt,70,84);
    if (checkHrInt > 0) sprintf(checkSec,"%02iH %02iM",checkHrInt,checkMinInt);
    else if (checkMinInt > 0) sprintf(checkSec,"%02iM %02iS",checkMinInt,checkSecInt);
    else sprintf(checkSec,"%02i Seconds",checkSecInt);
    TFTscreen.text(checkSec,70,96);
    delay(250);
    clearText(checkSec,70,96);
    clearText(checkSecInt,70,84);
    Serial.print("Sec: ");
    Serial.print(checkMillis);
    Serial.print(" Current: ");
    Serial.print(millis());
    Serial.print(" H: ");
    Serial.print(checkHrInt);
    Serial.print(" M: ");
    Serial.print(checkMinInt);
    Serial.print(" S: ");
    Serial.println(checkSecInt);

Yes i know that this is ultra confusing code. I’m just playing around as this is my first time playing with a TFT screen before :slight_smile: I just can’t figure this out and am possibly too tired to see the error of my ways right in front of me.

Thank you in advanced.


Running on an Arduino UNO + Ethernet Shield

Full code if you really must haha

#include <Client.h>
#include <Ethernet.h>
#include <Server.h>
#include <TFT.h>  // Arduino LCD library
#include <SPI.h>


#define cs   10
#define dc   9
#define rst  8



TFT TFTscreen = TFT(cs, dc, rst);


byte mac[] = {
  0x24, 0xAA, 0x02, 0x5E, 0x40, 0x50
};


EthernetClient client;

char myIP[64] = {};
char mySUB[64] = {};
char myDNS[64] = {};
char myGW[64] = {};
char* myIPTemplate = "%d.%d.%d.%d";
String fullXML;
long checkMillis = 0;
char checkSec[64];
long checkDayInt = 0;
long checkSecInt = 0;
long checkMinInt = 0;
long checkHrInt = 0;
/*char* nextCheckText;
int checkSecInt = 0;
char* checkSec;*/

//Time Zone Time URL
// ACDT = Short for Australia\Adelaide
const char* timeUrl = "/?key={KEY}&format=xml&by=zone&zone=ACDT";
const char* timeServer = "api.timezonedb.com";

void setup() {
  // put your setup code here, to run once:
  delay( 250 );   // allow some time (250 ms) after powerup and sketch start, for the Wiznet W5100 Reset IC (Ethernet Controller) to release and come out of reset.

  // Start Serial Communication
  Serial.begin(9600);

  // Put this line at the beginning of every sketch that uses the GLCD:
  TFTscreen.begin();

  // clear the screen with a black background
  TFTscreen.background(0, 0, 0);
  //Set the Text Size for the first printed Line
  TFTscreen.setTextSize(1);

  // set the font color
  TFTscreen.stroke(255, 255, 255);
  // Setup the screen
  TFTscreen.text("IP:", 3, 3);
  TFTscreen.text("WAITING FOR DHCP", 30, 3);


  
  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    // Clear waiting for IP Text, then display other text 
    TFTscreen.background(0, 0, 0);
    
    TFTscreen.stroke(255, 255, 255);
    TFTscreen.text("ERROR", 3,3);

    // no point in carrying on, so do nothing forevermore:
    for (;;)
      ;
  }
    
  sprintf(myIP, myIPTemplate, Ethernet.localIP()[0],  Ethernet.localIP()[1],  Ethernet.localIP()[2],  Ethernet.localIP()[3]);
  sprintf(myDNS, myIPTemplate, Ethernet.dnsServerIP()[0],  Ethernet.dnsServerIP()[1],  Ethernet.dnsServerIP()[2],  Ethernet.dnsServerIP()[3]);
  sprintf(myGW, myIPTemplate, Ethernet.gatewayIP()[0],  Ethernet.gatewayIP()[1],  Ethernet.gatewayIP()[2],  Ethernet.gatewayIP()[3]);
  sprintf(mySUB, myIPTemplate, Ethernet.subnetMask()[0],  Ethernet.subnetMask()[1],  Ethernet.subnetMask()[2],  Ethernet.subnetMask()[3]);
  resetScreenIPOnly();
  
  
}



void loop() {
  // put your main code here, to run repeatedly:
  
is())
  {
    
    resetScreenIPOnly();
    TFTscreen.text("Opening Connection", 3, 63);
  
    if (client.connect(timeServer, 80)) {
      // Make a HTTP request… etc etc (removed because of over post character count)
      
      clearText("Opening Connection", 3, 63);
      TFTscreen.text("Connection OK", 3, 63);
      
    } else {
      // If you didn't get a connection to the server:
      clearText("Opening Connection", 3, 63);
      TFTscreen.text("Connection FAILED", 3, 63);
    }
    checkMillis = millis() + 3010000;
    
  } else {
    
    TFTscreen.text("Next check: " , 3, 96);
    
    checkSecInt = (checkMillis-millis())/1000; //convect milliseconds to seconds
    checkMinInt=checkSecInt/60; //convert seconds to minutes
    checkHrInt=checkMinInt/60; //convert minutes to hours
    checkDayInt=checkHrInt/24; //convert hours to days
    checkSecInt=checkSecInt-(checkMinInt*60); 
    checkMinInt=checkMinInt-(checkHrInt*60); 
    checkHrInt=checkHrInt-(checkDayInt*24); 

    TFTscreen.text(checkSecInt,70,84);
    if (checkHrInt > 0) sprintf(checkSec,"%02iH %02iM",checkHrInt,checkMinInt);
    else if (checkMinInt > 0) sprintf(checkSec,"%02iM %02iS",checkMinInt,checkSecInt);
    else sprintf(checkSec,"%02i Seconds",checkSecInt);
    TFTscreen.text(checkSec,70,96);
    delay(250);
    clearText(checkSec,70,96);
    Serial.print("Sec: ");
    Serial.print(checkMillis);
    Serial.print(" Current: ");
    Serial.print(millis());
    Serial.print(" H: ");
    Serial.print(checkHrInt);
    Serial.print(" M: ");
    Serial.print(checkMinInt);
    Serial.print(" S: ");
    Serial.println(checkSecInt);
  }
  if (debug==1) Serial.print(" 3 > ");
    if (debug==1) Serial.println(checkMillis);
}



String xmlTakeParam(String inStr,String needParam)
{
// MORE CODE HERE (removed because of over post character count)
}

void resetScreenIPOnly()
{
  // Clear waiting for IP Text, then display other text 
  TFTscreen.background(0, 0, 0);
  TFTscreen.setTextSize(1);
  TFTscreen.stroke(255, 255, 255);
  //More display code removed here
  TFTscreen.text("-------STATUS-------", 7, 51);
  TFTscreen.text("SUPPLY & SERVICED BY",7, 108);
  TFTscreen.text("xxxx",7, 120);
}

void clearText (char* txt, int x, int y)
{
  TFTscreen.stroke(0, 0, 0);
  TFTscreen.text(txt, x, y);
  TFTscreen.stroke(255, 255, 255);
}
    if (checkHrInt > 0) sprintf(checkSec,"%02iH %02iM",checkHrInt,checkMinInt);
    else if (checkMinInt > 0) sprintf(checkSec,"%02iM %02iS",checkMinInt,checkSecInt);
    else sprintf(checkSec,"%02i Seconds",checkSecInt);

What it the format specifier for a long? Hint: it's NOT i.

PaulS - you are a champ! To be honest I haven't had much to do with char before and the sprintf is brand new to me. I just needed a pointer as to where i was going wrong.

To those wanting the answer, it was 'ld'

Quick google search 'sprintf format codes long' returned a table with a few key ones to look at

thanks again :)