Some suggested changes to your sketch
#include <Adafruit_GFX.h>
#include <Adafruit_ST7789.h>
#include <SPI.h>
#define TFT_MOSI 23 // Data out
#define TFT_SCLK 18 // Clock out
#define TFT_CS 22 //
#define TFT_DC 21 //
#define TFT_RST -1 // pin# 2
Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST);
int newHz;
int previousHz;
const byte xPosHz = 40; // use variables for screen positions.
const byte yPosHz = 0; //Adjust these to suit your screen layout
const byte incButtonPin = 12; //give inputs sensible names
const byte decButtonPin = 14;
void setup()
{
tft.init(170, 320); // high, with
tft.setRotation(3); // Adjust rotation as needed (0-3),0 = viertical, 1= horyzontal upside down, 2= viertical 180 deg , 3=horyzontal normal,
tft.fillScreen(ST77XX_BLUE);
tft.setTextWrap(false); // Disable text wrapping
tft.setTextSize(4); // Set text size
tft.setCursor(0, yPosHz);
tft.setTextColor(ST77XX_YELLOW); //print the text once
tft.print("Hz = ");
pinMode(incButtonPin, INPUT_PULLUP); //amp ++
pinMode(decButtonPin, INPUT_PULLUP); // amp --
}
void loop()
{
if (digitalRead(incButtonPin) == LOW)
{
newHz++;
}
if (digitalRead(decButtonPin) == LOW)
{
newHz--;
}
if (previousHz != newHz)
{
tft.setCursor(xPosHz, yPosHz);
tft.setTextColor(ST77XX_BLUE); // background color
tft.print(previousHz);
tft.setCursor(xPosHz, yPosHz);
tft.setTextColor(ST77XX_YELLOW);
tft.print(newHz);
previousHz = newHz;
}
delay(300);
}
I cannot test this as I don't have the hardware but it compiles. You need to adjust the value of the xPosHz variable to suit your screen depending on how many pixels the fixed text uses
Using variable for the x, y coordinates mans that you do not have to change the values in multiple places in the sketch