tft.drawString using Array

Hi Everyone.

I need some help on how to use Arrays to print a character.

The tft.drawString is duplicated many times and I want to save program memory to be able to expand

now memory is not a problem but it will become one soon.

I want to print 01 and 02 leading zero up to 09.Program works fine ,just need to save memory.

I use the Adafruit library form Bodmer and have no idea how to implement an Array with the drawstring

library.

Thanks for helping.

//==============================================
//==== colors for '' ILI9163 '' ================
//==============================================
#include "SPI.h"
#include "Adafruit_GFX_AS.h"
#include "Adafruit_ILI9341_AS.h"

#define sclk 13  // Don't change
#define mosi 11  // Don't change
#define cs   10
#define dc   9
#define rst  7  //

Adafruit_ILI9341_AS tft = Adafruit_ILI9341_AS(cs, dc, rst);       // Invoke custom library

long long int counterNow = 0;
long long int counter = 0;

//--------------------------
void setup()
{
  Serial.begin (9600);//
  tft.init();// Setup the LCD
  tft.fillScreen(LBLUE);
  tft.setTextColor(WHITE, BLACK); //fore ground,back ground===removes flicker when update
}

void loop()
{
  counterNow = counter ++;

  if(counterNow > 99)//run up to 99 and the reset to 0
  {
    counter = 0;
  }
  delay(100);
  tft.drawNumber(counterNow, 80, 0, 4);//value,x,y,font

  if (counterNow == 0 )
  {
    tft.drawString("00", 80, 0, 4);
  }
  if (counterNow == 1 )
  {
    tft.drawString("01", 80, 0, 4);
  }
  if (counterNow == 2 )
  {
    tft.drawString("02", 80, 0, 4);
  }
  if (counterNow == 3 )
  {
    tft.drawString("03", 80, 0, 4);
  }
  if (counterNow == 4 )
  {
    tft.drawString("04", 80, 0, 4);
  }
  if (counterNow == 5 )
  {
    tft.drawString("05", 80, 0, 4);
  }
  if (counterNow == 6 )
  {
    tft.drawString("06", 80, 0, 4);
  }
  if (counterNow == 7 )
  {
    tft.drawString("07", 80, 0, 4);
  }
  if (counterNow == 8 )
  {
    tft.drawString("08", 80, 0, 4);
  }
  if (counterNow == 9 )
  {
    tft.drawString("09", 80, 0, 4);
  }

  if (counterNow > 9)
  {
    tft.drawNumber(counterNow, 80, 0, 4);
  }

}

Try this:

  char numAsString[8];

  sprintf(numAsString, "%02d", counterNow); // convert counterNow to a two digit string, with leading 0s if needed.
  tft.drawString(numAsString, 80, 0, 4);

This will work for all values of counterNow from 0 to 32768. Of course, for values over 100, you wont get a 2 character string.

Why not just print the zero if counterNow is less than ten?

Strings are not really my thing but I suspect that you could do something like

tft.drawString("0" + String(counterNow, 80, 0, 4);

Completely untested

Normally you would just use print() but you use an altered version (AS suffix). Any reason to do so? As they state:

It is not recommended to use the functions provided by the Adafruit_GFX_AS class in newly developed code.

Thanks for the replies, I did not intend to bounce this back but a lot happened on this forum before I could reply.

AWOL:

I did venture in that direction but drawing seprate characters brings up a spacing problem so when the

font changes I must re do the x co ordinates,not ver user fiendly or consistanty keep track of the x value.

UKHelibob:

There is a lot of things no realy my thing ,but I will get there someday.

Septillion:

My display is a ILI9163 so the first library that I could sucssesfully modify was the ILI9341 from Bodmer

and from there on I was sort of stuck with the _AS version.

I did find this sketch for the Arduino TFT library,but it does not work in the _AS version.

This is only the lines in concern.

char* leadingZero[]={''01'',''02'',''03'',''04'',''05'',''06'',''07'',''08'',''09''};


if (now.hour() >= 0 && now.hour() < 10)
{
tft.print(leadingZero[now.hour() ] );
}
char* leadingZero[]={''01'',''02'',''03'',''04'',''05'',''06'',''07'',''08'',''09''};

Are you seriously using 2 single quote instead of 1 double quote?

No before it was posted it was 1 double quote and after the post it looks very stretched out. :disappointed_relieved:

Why did you have to "modify" it in the first place?