Go Down

Topic: counting error on tft screen (Read 51 times) previous topic - next topic

araab

I have a working display, showing vaue 'i', and with 2 buttons, the value goes up and down by 1 with each press. Works great, and shows on the display. The error, is when the amount of digits increase to double digits, and then decreases to single digits, the single digit is in the wrong place, and looks like it is counting down in tens, as the second digit '0' remains.

https://www.youtube.com/watch?v=WzyuTswHu3w

Code: [Select]


// EEPROM - Version: Latest
#include <EEPROM.h>

int i = EEPROM.read(1);


#include "Button.h"

//Apples
PushButton button1 = {22};
PushButton button2 = {24};

#include <Adafruit_GFX.h>
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;

#define BLACK   0x0000
#define BLUE    0x001F
#define RED     0xF800
#define GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF

// work in line numbers.  Font height in ht
int16_t ht = 16, top = 3, line, lines = 15, scroll;

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

  uint16_t id = tft.readID();
  tft.begin(id);
  tft.setRotation(0);     //Portrait
  tft.fillScreen(BLACK);
  tft.setTextColor(WHITE, BLACK);
  tft.setTextSize(3);     // System font is 8 pixels.  ht = 8*2=16
  tft.setCursor(85, 10);
  tft.print("Count");

  tft.setTextSize(4);
  tft.setCursor(20, 70);
  tft.print("Apples =");
}

void loop()
{
  tft.setTextSize(4);
  tft.setTextColor(RED, BLACK);


    if (button1.isPressed()) {
      i = i + 1;
    }
    if (button2.isPressed()) {
      i = i - 1;
    }

    tft.setCursor(200, 70);
    tft.print (i);


}


I found this:
char mystring[10]; // Space for 9 characters + null termination
sprintf(mystring, "%d", 1234);
tft.drawString(mystring, 30, 30, 2, YELLOW);

but the error is: drawstring not defined.

Is somebody able to please enlighten me, thanks.

blh64

You are in charge of inserting a blank space either before or after a single digit if you want it to line up with a 2 digit number.
Code: [Select]

#include <EEPROM.h>
#include "Button.h"

int i = EEPROM.read(1);

//Apples
PushButton button1 = {22};
PushButton button2 = {24};

#include <Adafruit_GFX.h>
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;

#define BLACK   0x0000
#define BLUE    0x001F
#define RED     0xF800
#define GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF

// work in line numbers.  Font height in ht
int16_t ht = 16, top = 3, line, lines = 15, scroll;

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

  uint16_t id = tft.readID();
  tft.begin(id);
  tft.setRotation(0);     //Portrait
  tft.fillScreen(BLACK);
  tft.setTextColor(WHITE, BLACK);
  tft.setTextSize(3);     // System font is 8 pixels.  ht = 8*2=16
  tft.setCursor(85, 10);
  tft.print("Count");

  tft.setTextSize(4);
  tft.setCursor(20, 70);
  tft.print("Apples =");
}

void loop()
{
  tft.setTextSize(4);
  tft.setTextColor(RED, BLACK);


    if (button1.isPressed()) {
      i = i + 1;
    }
    if (button2.isPressed()) {
      i = i - 1;
    }

    tft.setCursor(200, 70);
    if ( i < 10 ) tft.print( " " );
    tft.print (i);

}


That will solve one of your problems.  What happens if i goes negative?  You will have to deal with the leading '-' or not.

araab

#2
Nov 22, 2019, 11:37 pm Last Edit: Nov 22, 2019, 11:39 pm by araab
Worked a treat, thanks for taking the time to reply. if it goes negative, it does still get messed up, however, i'm fortunate enough with this project, that this will never happen. If I was to go from hundreds down to 10's, then 'if ( I < 100 ) tft.print ( " " )' ?

It's not important, but I don't suppose you would know how to display a '£' symbol, as I just get a strange character instead.

Go Up