Go Down

Topic: I/O control via Touch Screen code (Read 65 times) previous topic - next topic


Sep 27, 2015, 11:58 am Last Edit: Sep 27, 2015, 12:01 pm by esoder
Hello! I have a question to you guys, I'd be very thankful for any help i can get.

I want to control I/O outputs (with delay) on Arduino Mega by pushing graphical buttons on an TFT Touch Display Shield. I don't know how the code should be written to make the Arduino to write an I/O high when a graphical button is pushed on the Display.
I made a flowchart to demonstrate how I would like the project to work in practice. Any help would be largely appreciated, thanks in advance.

Best regards, Emil.


Sep 27, 2015, 02:54 pm Last Edit: Sep 27, 2015, 02:55 pm by giova014

The libraries below are quite good for what you are trying to do:

Download this TFT library: http://www.rinkydinkelectronics.com/library.php?id=51
And this TOUCH library: http://www.rinkydinkelectronics.com/library.php?id=55

Those will work with most modules (Read the Documents that come with them to see the obrigatory connections).

See the examples that come with them and then, come back here and tell what YOU tried to do and what are your questions.


Thanks a lot Giova!

I've read the libraries, but my question remains (I'm a beginner...)
Could you or anyone else please give an straight-forward example code containing:

"Touching a virtual button makes Arduino's ledpin 3 HIGH".

If anyone could provide that code, I think I could figure the rest out myself.

Best regards, Emil


Sep 27, 2015, 04:03 pm Last Edit: Sep 27, 2015, 04:04 pm by giova014
Here is a simple example with one button:

Code: [Select]
#include <UTFT.h>
#include <UTouch.h>

UTFT    myGLCD(ITDB32S, 38, 39, 40, 41);
UTouch  myTouch( 6, 5, 4, 3, 2);

extern uint8_t BigFont[];
int x, y;
bool active;

void setup()
  // Init for TFT

  // Init for TOUCH

  // Define the font that will be used

  //End of TFT+TOUCH setup

void loop()
  drawButton_Default(10, 10, 120, 120, "Button");
  while (1) {
    if (myTouch.dataAvailable()) {

      x = myTouch.getX();
      y = myTouch.getY();

      if (!active && readButton(10, 10, 120, 120)) {
        drawButton_Active(10, 10, 120, 120, "Button");
        digitalWrite(3, HIGH);
        active = true;
    else if (active) {
      drawButton_Default(10, 10, 120, 120, "Button");
      digitalWrite(3, LOW);
      active = false;

void drawButton_Default(int x1, int y1, int x2, int y2, char* label) {

  myGLCD.fillRect(x1, y1, x2, y2);
  myGLCD.drawRect(x1, y1, x2, y2);

  myGLCD.print(label, x1 + (x2 - x1) / 2 - strlen(label)*myGLCD.getFontXsize() / 2, y1 + (y2 - y1) / 2 - myGLCD.getFontYsize() / 2);


void drawButton_Active(int x1, int y1, int x2, int y2, char* label) {

  myGLCD.fillRect(x1, y1, x2, y2);
  myGLCD.drawRect(x1, y1, x2, y2);

  myGLCD.print(label, x1 + (x2 - x1) / 2 - strlen(label)*myGLCD.getFontXsize() / 2, y1 + (y2 - y1) / 2 - myGLCD.getFontYsize() / 2);


bool readButton(int x1, int y1, int x2, int y2) {
  if ( x > x1 && x < x2 && y > y1 && y < y2)
    return true;
  return false;


Thank you very much! That code cleared things up for me.

Go Up