MCUFriend tft.setCursor not working in second loop

Hi Everybody.

I am using this:
UNO with 2.4 TFT touch shield.
Aduino ide 1.8.12.

All the samples works fine , but in this small sketch the cursor values only works in the first loop.

First loop:prints (value2) then prints sevensegment font "1234"
Then there is about 8 seconds that nothing is printed then a massive big "0" sevensegment digit is printed on the screen.

Thanks for any advice.

#define LCD_CS A3 // Chip Select goes to Analog 3
#define LCD_CD A2 // Command/Data goes to Analog 2
#define LCD_WR A1 // LCD Write goes to Analog 1
#define LCD_RD A0 // LCD Read goes to Analog 0
#define LCD_RESET A4 // Can alternately just connect to Arduino's reset pin

#include <SPI.h>          // f.k. for Arduino-1.5.2
#include "Adafruit_GFX.h"// Hardware-specific library
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;
//#include <Adafruit_TFTLCD.h>
//Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);

#include <FreeDefaultFonts.h>
#include <Fonts/FreeSans9pt7b.h>
#include <Fonts/FreeSans12pt7b.h>
#include <Fonts/FreeSerif12pt7b.h>

// Assign human-readable names to some common 16-bit color values:
#define	BLACK   0x0000
#define	BLUE    0x001F
#define	RED     0xF800
#define	GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF

int value2 = 0;

void showmsgXY(int x, int y, int sz, const GFXfont *f, const char *msg)
{
  int16_t x1, y1;
  uint16_t wid, ht;
  // tft.drawFastHLine(0, y, tft.width(), WHITE);
  tft.setFont(f);
  tft.setCursor(x, y);
  tft.setTextColor(GREEN);
  tft.setTextSize(sz);
  tft.print(msg);
}

void setup(void) {
  Serial.begin(9600);
  uint32_t when = millis();
  //    while (!Serial) ;   //hangs a Leonardo until you connect a Serial
  if (!Serial) delay(5000);           //allow some time for Leonardo
  Serial.println("Serial took " + String((millis() - when)) + "ms to start");
  //    tft.reset();                 //hardware reset
  uint16_t ID = tft.readID(); //
  Serial.print("ID = 0x");
  Serial.println(ID, HEX);
  if (ID == 0xD3D3) ID = 0x9481; // write-only shield
  //    ID = 0x9329;                             // force ID
  tft.begin(ID);
  tft.fillScreen(BLACK);
}

void loop(void)
{
  value2 = value2 + 1;
  tft.setTextSize(4);
  tft.setTextColor(RED, BLACK);
  tft.setCursor(50, 10);
  tft.println(value2); //
  delay(1000);

//when "showmsgxy" is commented out the code works fine.

  showmsgXY(5, 180, 1, &FreeSevenSegNumFont, "1234");
 



}

You were attempting to print value2 in FreeSevenSegNumFont but in 4x normal size.

Try:

void loop(void)
{
    value2 = value2 + 1;
    tft.setFont(NULL);  //print value in system 7x5 font.
    tft.setTextSize(4);
    tft.setTextColor(RED, BLACK);
    tft.setCursor(50, 10);
    tft.println(value2); //
    delay(1000);  //so you can see value
    showmsgXY(5, 180, 1, &FreeSevenSegNumFont, "1234");
    delay(1000);  //see 1234 except it never changes
}

or simply put tft.setFont(NULL); at the end of the showmsgXY() function.

Note that FreeFonts only print in transparent mode. If you want to print "5678" you need to draw a fresh background first.

David.

Thanks David.

Now it is working .

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