Help with 0.96" SPI LCD change display on button

Hi All,

completely new here (and to programming as well) so if my question is not appropriate here or I am missing something, I’d be happy to change. Anyway, I am having some difficulties getting my code to do exactly what I want. I am using 160x80 TFT display running ST7735 chip along with Trinket M0 and a single push button in Arduino IDE. Since Trinket M0 doesn’t have an SD card slot, I have 4 RGB images which I converted to “code” and basically just store them in controller memory for displaying. Now, what I am trying to is when I power up the system, first image shows up stays there. Then, when I press the button a second image is displayed and it stays there until I press it again and then the third, and then the fourth, and then back to first. Since I didn’t manage to get that to work, I started troubleshooting with two simple texts with a background. I got that to work, but what I noticed is that my text is blinking since it is in the loop and it keeps re-drawing all the time. I want to fix this, so that then I can exchange that with my images, but so far no luck. Do you have any recommendations? Here’s the code I am using:

#include <Adafruit_GFX.h>    // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library for ST7735
#include <SPI.h>


  #define TFT_CS        1
  #define TFT_RST        -1
  #define TFT_DC         0


Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);



const int buttonPin = 2;

int buttonPushCounter = 0;
int buttonState = 0;
int lastButtonState = 0;





void setup() {

  pinMode(buttonPin, INPUT);
  
  Serial.begin(9600);
  
  tft.initR(INITR_MINI160x80);  // Init ST7735S mini display
  tft.invertDisplay(1);
  tft.setRotation(1);

  
//  tft.fillScreen(ST7735_BLACK);
//  testdrawtext("proba", ST77XX_GREEN);
//
// 
//}
//
//void testdrawtext(char *text, uint16_t color) {
//  tft.setCursor(10,10);
//  tft.setTextColor(color);
//  tft.setTextWrap(true);
//  tft.print(text);
}


void loop() {

  buttonState = digitalRead(buttonPin);
  

  if (buttonState != lastButtonState) {
    if (buttonState == HIGH) {
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes: ");
      Serial.println(buttonPushCounter);
    } else {
      Serial.println("off");
    }
    delay(50);
  }


lastButtonState = buttonState;

if (buttonPushCounter % 2 == 0) {
    tft.fillScreen(ST7735_BLACK);
    tft.setCursor(10,10);
    tft.setTextColor(ST77XX_GREEN);
    tft.setTextWrap(true);
    tft.print("Proba");
    delay(50);
} else {
    tft.fillScreen(ST7735_WHITE);
    tft.setCursor(10,10);
    tft.setTextColor(ST77XX_BLACK);
    tft.setTextWrap(true);
    tft.print("Proba 222");
    delay(50);
}

}

I am also trying this now but this also doesn’t work :confused:

#include <Adafruit_GFX.h>    // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library for ST7735
#include <SPI.h>


#define TFT_CS        1
#define TFT_RST        -1
#define TFT_DC         0


Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);


const int buttonPin = 2;

int buttonPushCounter = 0;
int buttonState = 0;
int lastButtonState = 0;


void pic1() {
  if (buttonPushCounter == 4) {
    tft.fillScreen(ST7735_BLACK);
    tft.setCursor(10,10);
    tft.setTextColor(ST77XX_GREEN);
    tft.setTextWrap(true);
    tft.print("Proba");
    //delay(50);
  }
}

void pic2() {
  if (buttonPushCounter == 8) {
    tft.fillScreen(ST7735_WHITE);
    tft.setCursor(10,10);
    tft.setTextColor(ST77XX_BLACK);
    tft.setTextWrap(true);
    tft.print("Proba 222");
    //delay(50);
  }
}



void setup() {

  pinMode(buttonPin, INPUT);
  
  Serial.begin(9600);
  
  tft.initR(INITR_MINI160x80);  // Init ST7735S mini display
  tft.invertDisplay(1);
  tft.setRotation(1);

}


void loop() {

  buttonState = digitalRead(buttonPin);
  if (buttonState != lastButtonState) {
    if (buttonState == HIGH) {
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes: ");
      Serial.println(buttonPushCounter);
    } else {
      Serial.println("off");
    }
    delay(50);
  }
  lastButtonState = buttonState;
  
  void pic1();
  delay(50);
  void pic2();
  delay(50);
}