Problem with 2.8" TFT Adafruit

Hello,

i want to make a couple of menus, so if you press the touchscreen button it will change to another menu.

Each menu will have its own "image.bmp". Problem is that when i push this button the screen record a multiple number of pushes. I tried doing delay, but found out that the screen is saving all bounce and send it 1 after 1 back to arduino. This ends up refreshing the screen multiple times, only if i press very quickly than it works fine. But that happens only 1 - 10 times.

there are 3 touch inputs which will activate > menuDraw1 ,2 ,3

this is the screen: Overview | Adafruit 2.8" TFT Touch Shield v2 - Capacitive or Resistive | Adafruit Learning System

So how to debounce this TFT ?

code:

//This example implements a simple sliding On/Off button. The example
// demonstrates drawing and touch operations.
//
//Thanks to Adafruit forums member Asteroid for the original sketch!
//
#include <Adafruit_GFX.h>
#include <SPI.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <Adafruit_ILI9341.h>
#include <Adafruit_STMPE610.h>

// This is calibration data for the raw touch data to the screen coordinates
#define TS_MINX 150
#define TS_MINY 130
#define TS_MAXX 3800
#define TS_MAXY 4000

#define STMPE_CS 8
Adafruit_STMPE610 ts = Adafruit_STMPE610(STMPE_CS);
#define TFT_CS 10
#define TFT_DC 9
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);

byte RecordOn = 0;

#define FRAME_X 210
#define FRAME_Y 180
#define FRAME_W 100
#define FRAME_H 50

#define REDBUTTON_X FRAME_X
#define REDBUTTON_Y FRAME_Y
#define REDBUTTON_W (FRAME_W/2)
#define REDBUTTON_H FRAME_H

#define GREENBUTTON_X (REDBUTTON_X + REDBUTTON_W)
#define GREENBUTTON_Y FRAME_Y
#define GREENBUTTON_W (FRAME_W/2)
#define GREENBUTTON_H FRAME_H


#define SD_CS 4



byte menuCounter = 0;
boolean menuDraw1 = LOW;
boolean menuDraw2 = LOW;
boolean menuDraw3 = LOW;
int y = 0;
int x = 0;


void drawFrame()
{
  tft.drawRect(120, 120, FRAME_W, FRAME_H, ILI9341_BLACK);
}

void redBtn()
{
  tft.fillRect(120, 120, REDBUTTON_W, REDBUTTON_H, ILI9341_RED);
  tft.fillRect(170, 120, GREENBUTTON_W, GREENBUTTON_H, ILI9341_BLACK);
  drawFrame();
  tft.setCursor(GREENBUTTON_X + 6 , GREENBUTTON_Y + (GREENBUTTON_H / 2));
  tft.setTextColor(ILI9341_WHITE);
  tft.setTextSize(2);
  tft.println("ON");

}

void greenBtn()
{
  tft.fillRect(120, 120, GREENBUTTON_W, GREENBUTTON_H, ILI9341_GREEN);
  tft.fillRect(170, 120, REDBUTTON_W, REDBUTTON_H, ILI9341_BLACK);

  //tft.fillRect(GREENBUTTON_X, GREENBUTTON_Y, GREENBUTTON_W, GREENBUTTON_H, ILI9341_GREEN);
  //tft.fillRect(REDBUTTON_X, REDBUTTON_Y, REDBUTTON_W, REDBUTTON_H, ILI9341_BLUE);

  drawFrame();
  tft.setCursor(REDBUTTON_X + 6 , REDBUTTON_Y + (REDBUTTON_H / 2));
  tft.setTextColor(ILI9341_WHITE);
  tft.setTextSize(2);
  tft.println("OFF");

}

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

  yield();
  Serial.print("Initializing SD card...");
  if (!SD.begin(SD_CS)) {
    Serial.println("failed!");
  }
  Serial.println("OK!");
  tft.setRotation(1);
  tft.fillScreen(ILI9341_BLUE);
  if (!ts.begin()) {
    Serial.println("Unable to start touchscreen.");
  }
  else {
    Serial.println("Touchscreen started.");
  }

  // tft.fillScreen(ILI9341_BLUE);

  // origin = left,top landscape (USB left upper)

  //redBtn();
  //bmpDraw("mainmenu.bmp", 0, 0);
   menuDraw1 = HIGH;
   
}

void loop()
{

  // tft.drawBitmap(0, 0, "menu.bmp", 320, 240, ILI9341_BLACK);


  // See if there's any  touch data for us
  
  if (!ts.bufferEmpty())
 {
    // Retrieve a point
    TS_Point p = ts.getPoint();
     delay(200);
    // Scale using the calibration #'s
    // and rotate coordinate system
    p.x = map(p.x, TS_MINY, TS_MAXY, 0, tft.height());
    p.y = map(p.y, TS_MINX, TS_MAXX, 0, tft.width());
     y = tft.height() - p.x;
     x = p.y;
    //bmpDraw("menu.bmp",0,0);
    
    
       if ((x > 0) && (x < 30)) {
      if ((y > 200) && (y < 240)) {


        Serial.println("R = 1");
      
       
        menuDraw1 = HIGH;
      y = 0;
     x = 0;
       
      }
    }



    if ((x > 290) && (x < 320)) {
      if ((y > 200) && (y < 240)) {
        Serial.println("R = 2");
        
        menuDraw2 = HIGH;
     y = 0;
     x = 0;
      
     
      }
    }
    
       if ((x > 150) && (x < 230)) {
      if ((y > 200) && (y < 240)) {
        Serial.println("R = 3");
        
        menuDraw3 = HIGH;
         
   y = 0;
     x = 0;

      
      }
    }

   

  










 



  
    
      if (menuDraw3 == HIGH) {
        bmpDraw("mainmenu.bmp", 0, 0);
        
         tft.setCursor(5, 55);    ///////////    -->,  ^^
    tft.setTextColor(ILI9341_BLACK, ILI9341_WHITE);
    tft.setTextSize(1);
    tft.print("Temperatuur =");
    tft.print(menuDraw1);
      tft.print(" ");
    tft.print(menuDraw2);
      tft.print(" ");
    tft.println(menuDraw3);
    

    tft.setCursor(5, 45);       ///////////    -->,  ^^
    tft.setTextColor(ILI9341_BLACK, ILI9341_WHITE);
    tft.setTextSize(1);
    tft.print("Vochtigheid=");
    tft.println("46 ");


menuDraw3=LOW;


      }

    
  
      if (menuDraw1 == HIGH) {
        tft.fillScreen(ILI9341_BLUE);
        
           tft.setCursor(5, 55);    ///////////    -->,  ^^
    tft.setTextColor(ILI9341_BLACK, ILI9341_WHITE);
    tft.setTextSize(1);
    tft.print("Temperatuur =");
    tft.print(menuDraw1);
      tft.print(" ");
    tft.print(menuDraw2);
      tft.print(" ");
    tft.println(menuDraw3);

    tft.setCursor(5, 45);       ///////////    -->,  ^^
    tft.setTextColor(ILI9341_BLACK, ILI9341_WHITE);
    tft.setTextSize(1);
    tft.print("Vochtigheid=");
    tft.println("46 ");

    tft.setCursor(5, 35);      ///////////    -->,  ^^
    tft.setTextColor(ILI9341_BLACK, ILI9341_WHITE);
    tft.setTextSize(1);
    tft.print("Afzuiging=");
    tft.println("100 ");

    tft.setCursor(5, 25);         ///////////    -->,  ^^
    tft.setTextColor(ILI9341_BLACK, ILI9341_WHITE);
    tft.setTextSize(1);
    tft.print("NachtMode=");
    tft.println(" nee");


menuDraw1=LOW;

     


      
     
    }

   

      if (menuDraw2 == HIGH) {

        tft.fillScreen(ILI9341_GREEN);
        
              tft.setCursor(5, 55);    ///////////    -->,  ^^
    tft.setTextColor(ILI9341_BLACK, ILI9341_WHITE);
    tft.setTextSize(1);
    tft.print("Temperatuur =");
    tft.print(menuDraw1);
      tft.print(" ");
    tft.print(menuDraw2);
      tft.print(" ");
    tft.println(menuDraw3);
    
               tft.setCursor(5, 45);       ///////////    -->,  ^^
    tft.setTextColor(ILI9341_BLACK, ILI9341_WHITE);
    tft.setTextSize(1);
    tft.print("Vochtigheid=");
    tft.println("46 ");

    
   menuDraw2=LOW;

      
    }
  }

 
  


}

i found some code that working with maximum of 3 buttons.....

I have been trying to make more buttons by trying duplicate the code, but the result are not what i expected.

The case switches "rotation", but i dont see rotation increment anywhere in the code.
Does somebody knows how to implement more buttons into the code ?

/*
 Demonstrates class Adafruit_GFX_Button
 For Adafruit 2.8" TFT LCD - resistive.
*/

#include <SPI.h>
#include <Wire.h>
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#include "Adafruit_STMPE610.h"
#include <SD.h>

// Default values for Adafruit shield v2.
#define STMPE_CS 8
#define TFT_DC 9
#define TFT_CS 10
#define SD_CS 4

Adafruit_STMPE610 ts = Adafruit_STMPE610(STMPE_CS);
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);


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

#define TS_MINX 150
#define TS_MINY 130
#define TS_MAXX 3800
#define TS_MAXY 4000

// Rotations 0,2 = portrait  : 0->USB=right,upper : 2->USB=left,lower
// Rotations 1,3 = landscape : 1->USB=left,upper  : 3->USB=right,lower
byte rotation = 1; //(0->3)
const byte _numBtns = 3;
const byte _numBtns2 = 8;

Adafruit_GFX_Button btnMain[_numBtns];
Adafruit_GFX_Button btnInst[_numBtns2];
int btnColor[8] = {RED, GREEN, BLUE, CYAN, MAGENTA, YELLOW, WHITE, BLACK};

TS_Point p;
int x, y;

byte menuCounter = 0;
boolean menuDraw1 = LOW;
boolean menuDraw2 = LOW;
boolean menuDraw3 = LOW;



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


  Serial.print("Initializing SD card...");
  if (!SD.begin(SD_CS)) {
    Serial.println("failed!");
  }
  Serial.println("OK!");
  tft.setRotation(1);
  tft.fillScreen(ILI9341_BLUE);
  bmpDraw("mainmenu.bmp", 0, 0);

  if (!ts.begin()) {
    Serial.println("Couldn't start touchscreen controller");
    while (1);
  }
  tft.setRotation(rotation);

  btnMain[0].initButton( &tft, 254, 18, 50, 50, BLACK, btnColor[0], BLACK, "0",  3 );
  btnMain[1].initButton( &tft, 254, 95, 50, 50, BLACK, btnColor[1], BLACK, "1",  3 );
  btnMain[2].initButton( &tft, 254, 175, 50, 50, BLACK, btnColor[2], BLACK, "2", 3 );
 
 /*
  btnMain[3].initButton( &tft, 2, 45, 73, 70, BLACK, btnColor[0], BLACK, "0",    3 );
  btnMain[4].initButton( &tft, 82, 45, 73, 70, BLACK, btnColor[1], BLACK, "1",   3 );
  btnMain[5].initButton( &tft, 162, 45, 73, 70, BLACK, btnColor[1], BLACK, "2",  3 );
  btnMain[6].initButton( &tft, 242, 45, 73, 70, BLACK, btnColor[1], BLACK, "0",  3);
  btnMain[7].initButton( &tft, 2, 125, 73, 70, BLACK, btnColor[1], BLACK, "1",   3);
  btnMain[8].initButton( &tft, 82, 125, 73, 70, BLACK, btnColor[1], BLACK, "2",  3 );
  btnMain[9].initButton( &tft, 162, 125, 73, 70, BLACK, btnColor[1], BLACK, "0", 3 );
  btnMain[10].initButton( &tft, 242, 125, 73, 70, BLACK, btnColor[1], BLACK, "1",3 );

*/
  btnInst[0].initButton( &tft, 2, 45, 73, 70, BLACK, btnColor[0], BLACK, "0",  3 );
  btnInst[1].initButton( &tft, 82, 45, 73, 70, BLACK, btnColor[1], BLACK, "1", 3 );
  btnInst[2].initButton( &tft, 162, 45, 73, 70, BLACK, btnColor[2], BLACK, "2",3 );
  btnInst[3].initButton( &tft, 242, 45, 73, 70, BLACK, btnColor[3], BLACK, "3",3 );
  btnInst[4].initButton( &tft, 2, 125, 73, 70, BLACK, btnColor[4], BLACK, "4", 3);
  btnInst[5].initButton( &tft, 82, 125, 73, 70, BLACK, btnColor[5], BLACK, "5",3 );
  btnInst[6].initButton( &tft, 162, 125, 73, 70, BLACK, btnColor[6], BLACK,"6",3 );
  btnInst[7].initButton( &tft, 242, 125, 73, 70, BLACK, btnColor[7], BLACK,"7",3 );




}




void loop() {

  readButtonsMain();

}









void btnHandler(byte btnHit) {

  
 if(menuCounter==0){ 
  switch (btnHit) {

    case 0:

      bmpDraw("mainmenu.bmp", 0, 0);
      
      break;

    case 1:
      tft.fillScreen(ILI9341_GREEN);
    
      break;

    case 2:

     tft.fillScreen(ILI9341_BLUE);
   
     
     menuCounter=1;
   

      break;


  }
 }
 

 
  else if(menuCounter==1){  
 
   
   
  switch (btnHit) {

    case 0:


        tft.fillScreen(ILI9341_BLACK);
      break;

    case 1:
     
        tft.fillScreen(ILI9341_GREEN);

      break;

    case 2:

      
   tft.fillScreen(ILI9341_YELLOW);

    

      break;

      case 3:

     tft.fillScreen(ILI9341_BLUE);
    

      break;


  case 4:

    tft.fillScreen(ILI9341_BLACK);
    

      break;


  case 5:

      tft.fillScreen(ILI9341_CYAN);
    
    

      break;


  case 6:

     tft.fillScreen(ILI9341_PURPLE);
    

      break;

  case 7:
      btnHit=0;
      menuCounter=0;
    

      break;

  }
 }
}


void readButtonsMain(){
  
  
  
    if (!ts.bufferEmpty()) { 
   p = ts.getPoint();
  switch (rotation){
  case 0:
  x = map(p.x, TS_MINX, TS_MAXX, 0, tft.width());
  y = map(p.y, TS_MINY, TS_MAXY, 0, tft.height());
  break;
  case 1:
  // p.x, p.y reversed //
   x = map(p.y, TS_MINY, TS_MAXY, 0, tft.width());
   y = map(p.x, TS_MAXX, TS_MINX, 0, tft.height());
   break;
  case 2:
  x = map(p.x, TS_MAXX, TS_MINX, 0, tft.width());
  y = map(p.y, TS_MAXY, TS_MINY, 0, tft.height());
  break;
  case 3:
 //  p.x, p.y reversed //
  x = map(p.y, TS_MAXY, TS_MINY, 0, tft.width());
  y = map(p.x, TS_MINX, TS_MAXX, 0, tft.height());
  break;
  case 4:
 //  p.x, p.y reversed //
   x = map(p.x, TS_MAXX, TS_MINX, 0, tft.width());
  y = map(p.y, TS_MAXY, TS_MINY, 0, tft.height());
  break;
 
   case 5:
 //  p.x, p.y reversed //
  x = map(p.y, TS_MAXY, TS_MINY, 0, tft.width());
  y = map(p.x, TS_MINX, TS_MAXX, 0, tft.height());
  break;
 
   case 6:
 //  p.x, p.y reversed //
  x = map(p.x, TS_MAXX, TS_MINX, 0, tft.width());
  y = map(p.y, TS_MAXY, TS_MINY, 0, tft.height());
  break;
 
   case 7:
 //  p.x, p.y reversed //
  x = map(p.y, TS_MAXY, TS_MINY, 0, tft.width());
  y = map(p.x, TS_MINX, TS_MAXX, 0, tft.height());
  break;
 
   case 8:
 //  p.x, p.y reversed //
  x = map(p.x, TS_MAXX, TS_MINX, 0, tft.width());
  y = map(p.y, TS_MAXY, TS_MINY, 0, tft.height());
  break;
 
   case 9:
 //  p.x, p.y reversed //
  x = map(p.y, TS_MAXY, TS_MINY, 0, tft.width());
  y = map(p.x, TS_MINX, TS_MAXX, 0, tft.height());
  break;

 }

 while (ts.touched()) {
  for (uint8_t b=0; b<_numBtns; b++) {
    
    if(menuCounter==0){
   if (btnMain[b].contains(x,y)) {
      btnMain[b].press(true);
      btnHandler(b);
    } else {
      btnMain[b].press(false); 
    }
    }//end menucounter=0
    
     if(menuCounter==1){
   if (btnInst[b].contains(x,y)) {
      btnInst[b].press(true);
      btnHandler(b);
    } else {
      btnInst[b].press(false); 
    }
    }//end menucounter=0
    
   }
  }
}


  
}