Touchscreen is giving me same input even when touched

I am currently using SAMD21 with Round display by XIAO for my initial work.

The code below allows me to switch between 3 bitmap, however, I am expecting that the for loop will follow 0-2, but at times when the screen is touched, its staying at the same picture or it goes in reverse. Also, below is the serial monitor showing its not going as expected.

What could have gone wrong? Should I be calibrating the screen first + doing a debounce?

#include <TFT_eSPI.h>
#include <SPI.h>
#define USE_TFT_ESPI_LIBRARY
#include "lv_xiao_round_screen.h"
#include "images.h"

const uint16_t imageWidth = 160;
const uint16_t imageHeight = 160;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);

  pinMode(TOUCH_INT, INPUT_PULLUP);
  Wire.begin();
  
  // Initialise the screen
  tft.init();

  //Set the orientation of screen
  tft.setRotation(0);

  //Fill screen with BLACK 
  tft.fillScreen(TFT_BLACK);

  tft.setSwapBytes(true);

  //Set initial image upon turn-on/load
  tft.pushImage(40, 40, imageWidth, imageHeight, epd_bitmap_Asset_16_10x);

}

void loop() {
  for(int i=0; i<3; i++){
    if(chsc6x_is_pressed()){
      Serial.print("Current Image is ");
      Serial.println(i);
      tft.pushImage(40, 40, imageWidth, imageHeight, epd_bitmap_allArray[i]);
    }
    delay(100);
  }
  
}


Current Image is 0
Dislay is touched.
Current Image is 2
Dislay is touched.
Current Image is 1
Dislay is touched.
Current Image is 1
Dislay is touched.

Change the loop function with:

int n = 0;
int end = 3;

void loop() {
      if(chsc6x_is_pressed()) {
            Serial.print("Current Image is ");
            Serial.println(n);
            tft.pushImage(40, 40, imageWidth, imageHeight, epd_bitmap_allArray[n]);
            n = n + 1;
            if (end) n = 0;
      }
      delay(100);
}

In your code, If you not pressed, after 100 units of waiting increases the i variable. But what you need to do is checking it and if it's true then doing the increment.

I just changed:

if (end) n = 0;

to

if (n==3) n=0;

With this it worked.

I was a bit sleepy there lol. By the way if you're using 3 image epd_bitmap_allArray[n] should have been threw error. So doing n < arraySize should do. In my code's case, n < end

if(n>=(sizeof(epd_bitmap_allArray) / sizeof(epd_bitmap_allArray[0]))){
          n=0;
        }

Added this and it works. Thanks!

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