0

I made simple 3 red buttons like this:

#include <SPI.h>

#include "Adafruit_GFX.h"     
#include "Adafruit_ILI9341.h" 
#include "URTouch.h"          

#define TFT_DC 9              
#define TFT_CS 10             
#define TFT_RST 8             
#define TFT_MOSI 11           
#define TFT_CLK 13            
#define TFT_IRQ  7          

Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_CLK, 

#define t_SCK 3               
#define t_CS 4                
#define t_MOSI 5              
#define t_MISO 6             
#define t_IRQ 7               

URTouch ts(t_SCK, t_CS, t_MOSI, t_MISO, t_IRQ); 

void setup() {

  tft.begin();
  tft.fillScreen(BLACK);

  tft.fillRoundRect (12,15,30,80,5,RED);//50,100,30,80,5,RED
  tft.fillRoundRect (50,15,30,80,5,RED);
  tft.fillRoundRect (88,15,30,80,5,RED);

  delay(500);
}

And this the part where I need help:

void loop()
{
  int x, y;                         

  while(ts.dataAvailable())         
  {
    ts.read();                //-----------> this is the whole thing      
    x = ts.getX();                  
    y = ts.getY();                  

  }

What I need is, to know how to detect and read the pressure of the button area, so that I can run different tasks.

| improve this question | | | | |
2

The fillRoundRect function format is (initial_Xpoint,initial_Ypoint,length,width,corner_radius,color)

Therefore, you already have the information about the location of the button itself. Next, check if the x and y value is within the button area.

void setup() {

  tft.begin();
  tft.fillScreen(BLACK);

  tft.fillRoundRect (12,15,30,80,5,RED);  //button1
  tft.fillRoundRect (50,15,30,80,5,RED);  //button2
  tft.fillRoundRect (88,15,30,80,5,RED);  //button3


  delay(500);
}

void loop()
{
  int x, y, pressedbutton=0;                         

  if(ts.dataAvailable())         
  {
    ts.read();                //-----------> this is the whole thing      
    x = ts.getX();                  
    y = ts.getY();                  

    if ((x>=12) && (x<=12+30) && (y>=15) && (y<=15+80)) pressedbutton = 1; else
    if ((x>=50) && (x<=50+30) && (y>=15) && (y<=15+80)) pressedbutton = 2; else
    if ((x>=88) && (x<=88+30) && (y>=15) && (y<=15+80)) pressedbutton = 3; 
  }

  ...
  ...
  ...
 }
| improve this answer | | | | |
0

I'm not 100% sure what you ask, but I assume you want to perform a task depending on what button has been pressed.

You can do this by checking x and y, e.g.

switch (x)
{
case 0: 
   switch (y)
   {
   case 0: // Button (0,0) is pressed)
      perform_task_A();
      break;
   case 1:
      perform_task_B();
      break;
   ... // etc.
   }
   break;
case 1:
   switch(x)
       // Etc. as above but use different tasks.

Than create for each task a function

void perform_task_A()
{
   // Do your code
}

If you want to change 'functionality' depending on a button press, e.g. assume perform_task_A should read a digit, than create a new switch statement there to read a digit key.

If you want more complicated menu structures/input entering, check into state diagrams to keep track where you are in the UI-flow and act on the current state AND the pressed button.

| improve this answer | | | | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.