2.8" TFT Touch Shield [V1] - Buttons

I am creating buttons on my TFT for relays. I made boxes with the labeled relay uses, along with code if the buttons are touched. It is essentially a simple button, that changes a digital pin to HIGH or LOW depending on touch.

My code essentially follows this rule:

if (p.z > ts.pressureThreshhold) { //If touched
     if(p.x >= 0 && p.x < 70) {  //Within these x-values
       if(p.y >= 196 && p.y < 226)  //Within these y-values

       {  if (Pin == HIGH) {
           digitalWrite(Pin, LOW); //set the Relay OFF
       }
         else  {
           digitalWrite(Pin, HIGH);// set the Relay ON
         
      }
   }
}

The part I want you to look at is void buttons(). It essentially encompasses the area of the code where if the rectangle is touched, something should happen.


#include <stdint.h>
#include <TouchScreen.h> 
#include <TFT.h>
#include "Adafruit_MAX31855.h"
#include <cstddef.h>


#define YP A2   // must be an analog pin, use "An" notation!
#define XM A1   // must be an analog pin, use "An" notation!
#define YM 54   // can be a digital pin, this is A0
#define XP 57   // can be a digital pin, this is A3 

#define TS_MINX 140 
#define TS_MAXX 900
#define TS_MINY 120
#define TS_MAXY 940


int Pump1Pin = 31;  //First Relay Pin
int Pump2Pin = 33;
int FanPin = 35;
int ReturnPin = 37;
int SkimmerPin = 39;
int Halide1Pin = 41;
int Halide2Pin = 43;
int ActinicPin = 45;
int Pump1State;
int Pump2State;
int FansState;
int ReturnState;
int SkimmerState;
int HalideState;
int ActinicState;
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300); //init TouchScreen port pins

void setup()
{

  Tft.init();  //init TFT library
  Serial.begin(38400); 
  pinMode(Pump1Pin, OUTPUT);
  pinMode(Pump2Pin, OUTPUT);
  pinMode(FanPin, OUTPUT);
  pinMode(ReturnPin, OUTPUT);
  pinMode(SkimmerPin, OUTPUT);
  pinMode(Halide1Pin, OUTPUT);
  pinMode(Halide2Pin, OUTPUT);
  pinMode(ActinicPin, OUTPUT);

  
drawScreen(); 
}

void loop()
{
  buttons();
  
      // Print Char to TFT //
  Tft.fillRectangle(200,60,50,15,BLACK);        // Clear old text
  
  Tft.fillRectangle(200,120,60,15,BLACK);        // Clear old text

  Tft.fillRectangle(200,150,60,15,BLACK);       // Clear old text

  Tft.fillRectangle(200,180,60,15,BLACK);       // Clear old text

        
  delay(1000);                                  // delay to read screen

}



// Draw Screen Function //
// Draw all the text and objects that will not change //
void drawScreen(){                
  Tft.drawRectangle(0, 3, 238,30,BLUE);            // HEADER TITLE
  Tft.drawString("Display Data",20,15,2,WHITE);    // header name        
  Tft.drawString("Hi",140,40,1,WHITE);
  Tft.drawString("Low",155,40,1,WHITE);
  Tft.drawString("Current",180,40,1,WHITE);
  Tft.drawString("Seconds:",2,60,2,CYAN);  
  Tft.drawString("Temp",2,90,2,WHITE);
  Tft.drawString("Internal:",20,120,1,WHITE);
  Tft.drawString("External:",20,150,1,WHITE);
  Tft.drawString("pH:",2,170,2,WHITE);
  Tft.drawRectangle(0, 196, 70, 30,BLUE);
  Tft.drawString("Pump 1", 15, 208, 1, WHITE);
  Tft.drawRectangle(80, 196, 70, 30, BLUE);
  Tft.drawString("Pump 2", 95 , 208 , 1, WHITE);
  Tft.drawRectangle(160, 196, 70, 30, BLUE);
  Tft.drawString("Fans", 175, 208, 1, WHITE);
  Tft.drawRectangle(0, 236, 110,30,BLUE);
  Tft.drawString("Return Pump", 15, 248,1,WHITE);
  Tft.drawRectangle(120, 236, 110, 30, BLUE);
  Tft.drawString("Skimmer", 150, 248, 1, WHITE);
  Tft.drawRectangle(0, 276, 110, 30, BLUE);
  Tft.drawString("Halides", 25, 286, 1, WHITE);
  Tft.drawRectangle(120, 276, 110, 30, BLUE);
  Tft.drawString("Actnitcs", 140, 286, 1, WHITE);   
}

void buttons(){  
  Point p = ts.getPoint();
  p.x = map(p.x, TS_MINX, TS_MAXX, 240, 0);
  p.y = map(p.y, TS_MINY, TS_MAXY, 320, 0);

  if (p.z > ts.pressureThreshhold) {
     if(p.x >= 0 && p.x < 70) {
       if(p.y >= 196 && p.y < 226) 
       {

         if (Pump1State == HIGH) {
           digitalWrite(Pump1Pin, LOW); //set the Relay ON
            Pump1State = 0; }
         else  {
           digitalWrite(Pump1Pin, HIGH);// set the Relay OFF
            Pump1State = 1;
         }
       }
     }///////////////////////////////////////////////////////////////////////////For Pump 1
     
     else if(p.x >= 80 && p.x < 150) {
       if(p.y >= 196 && p.y < 226) 
       {

         if (Pump2State == HIGH) {
           digitalWrite(Pump2Pin, LOW); //set the Relay OFF
            Pump2State = 0; }
         else  {
           digitalWrite(Pump2Pin, HIGH);// set the Relay ON
            Pump2State = 1;
         }
       }
         
     }//For Pump 2
     
     else if(p.x >= 160 && p.x < 230) {
       if(p.y >= 196 && p.y < 226) 
       {

         if (FansState == HIGH) {
           digitalWrite(FanPin, LOW); //set the Relay OFF
            FansState = 0; }
         else  {
           digitalWrite(FanPin, HIGH);// set the Relay ON
            FansState = 1;
         }
       }
         
     }//For Fans
     
     else if(p.x >= 0 && p.x < 110) {
       if(p.y >= 236 && p.y < 266) 
       {

         if (ReturnState == HIGH) {
           digitalWrite(ReturnPin, LOW); //set the Relay OFF
            ReturnState = 0; }
         else  {
           digitalWrite(ReturnPin, HIGH);// set the Relay ON
            ReturnState = 1;
         }
       } //for return pump
      else  {
         if (HalideState == HIGH) {
           digitalWrite(Halide1Pin, LOW); //set the Relay OFF
           digitalWrite(Halide2Pin, LOW);
            HalideState = 0; }
         else  {
           digitalWrite(Halide1Pin, HIGH);// set the Relay ON
           digitalWrite(Halide2Pin, HIGH);
            HalideState = 1;
         }
       }//for halides
         
     }//if touched in these x-values
     
     else if(p.x >= 120 && p.x < 150) {
       if(p.y >= 236 && p.y < 266) //
       {

         if (SkimmerState == HIGH) {
           digitalWrite(SkimmerPin, LOW); //set the Relay OFF
            SkimmerState = 0; }
         else  {
           digitalWrite(SkimmerPin, HIGH);// set the Relay ON
            SkimmerState = 1;
         }
       } //skimmer
       else // Actinic
       {

         if (ActinicPin == HIGH) {
           digitalWrite(ActinicPin, LOW); //set the Relay OFF
            ActinicState = 0; }
         else  {
           digitalWrite(ActinicPin, HIGH);// set the Relay ON
            ActinicState = 1;
         }
       }
      } //For Skimmer
 
   }//Touch 
}

When I run the code it really doesn’t work, except for the very first If statement. I do not know what the problem is. I think I have all my parentheses correct. Let me know what you think.

Hi,

Do you mean it does not recognize your touches?

Looking at your code I am using a different lib I think.
I tested your code and changed some of it and on my Arduino Mega it is working.

Try to change this if it works:

p.x = map(p.x, TS_MINX, TS_MAXX, 0, 240); p.y = map(p.y, TS_MINY, TS_MAXY, 0, 320);

My changes to make it work with the lib I am using and running it on an Arduino Mega are here: http://pastebin.com/XsxzFY4p

I can see the x coords of the touches in your code:

Serial.print("p.x=");
Serial.println(p.x);

cheers,
Gus

Two points: 1) You set the state variable to 1 or 0, and yet you test for HIGH. Good form says that all your statements should be 1 or 0, or HIGH or LOW, not a mix.

  1. Use comment on your braces to mark which open brace belongs to which close brace. For Example:

      if (ActinicPin == HIGH) {
        digitalWrite(ActinicPin, LOW); //set the Relay OFF
         ActinicState = 0; }  //  if(ActinicPIN...
      else  {
        digitalWrite(ActinicPin, HIGH);// set the Relay ON
         ActinicState = 1;
      }  //  else...
    

Makes it much easier to tell if the braces are nested properly.