Go Down

Topic: Change display in TFT (Read 89 times) previous topic - next topic

paulo_ferreiraa

Hi, i am making a project which i have a principal menu with 2 buttons, one with "Water" and another one with "Room".
When i press the button water i want to clean de principal menu and show the menuWater, inside the menuWater i have a button called "Return" and when pressed this button i want to show the principal menu again. The problem is, when i press the "Water" button the principal menu disappears but instead of showing the menuWater the tft shows a white screen.
Aprecciate any help, ask me if you didnĀ“t understand something.

Code: [Select]
// Touch screen library with X Y and Z (pressure) readings as well
// as oversampling to avoid 'bouncing'
// This demo code returns raw readings, public domain

#include <stdint.h>
#include "TouchScreen.h"
#include <Adafruit_GFX.h>    // Libreria de graficos
#include <Adafruit_TFTLCD.h>

#define YP A2  // must be an analog pin, use "An" notation!
#define XM A3  // must be an analog pin, use "An" notation!
#define YM 8   // can be a digital pin
#define XP 9   // can be a digital pin

#define LCD_CS A3   // Definimos los pines del LCD
#define LCD_CD A2   // para poder visualizar elementos graficos
#define LCD_WR A1
#define LCD_RD A0
#define LCD_RESET A4

#define  BLACK   0x0000  // Definimos los colores
#define BLUE    0x001F  // que utilizaremos para
#define RED     0xF800  // el texto y los elementos graficos
#define GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF

TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET); // Instancia LCD

boolean waterState = LOW;


void setup(void) {
  Serial.begin(9600);
  tft.begin(0x9341);
  principalMenu();
}

void loop(void) {
  int x;
  TSPoint p = ts.getPoint();

  if (p.z > ts.pressureThreshhold) {
     Serial.print("X = "); Serial.print(p.x);
     Serial.print("\tY = "); Serial.print(p.y);
     Serial.print("\tPressure = "); Serial.println(p.z);
     x = localPressPrincipal(p.x,p.y);
     }
  delay(100);
}
//Function to detect where i pressed in the principal menu
int localPressPrincipal(int x, int y){
  if(x>225 && x<375 && y>345 && y<725){ //Water Button
    delay(1000);
    waterMenu(20.1);
    principalMenu();   
    return 1;
  }
  if(x>500 && x<660 && y>350 && y<760){ //Room button
    Serial.println("Room");
    return 2;
  } 
}

int localPressWater(int x, int y){
  if(x>500 && x<660 && y>350 && y<790){ //Turn on the water
    Serial.println("Water turned on");
    return 3;
  }
  if(x>710 && x<870 && y>350 && y<835){//Return button
    return 4;
  }
 
}
//Displays the waterMenu
void waterMenu(float waterTemp){
  tft.fillScreen(BLACK);
  tft.setCursor(20,30);
  tft.setTextSize(2);
  tft.print("Temperature: ");
  tft.println(waterTemp);
  tft.setCursor(60,240);
  tft.setTextSize(4);
  tft.drawRect(50,220,160,70,RED);
  tft.print("Return");
  tft.setCursor(60,150);
  tft.drawRect(50,130,140,70,RED);
  tft.println("Turn on");
  if(waterState == LOW){
      tft.setTextSize(2);
      tft.setCursor(25,70);
      tft.println("Water turned of");
    }
  loopWater();
  return;
}
//This function was done to hold the waterMenu on the screen while the return button is not pressed
void loopWater(){
  int x = 1;
  while(x!=4){//x!= 4 because 4 is when the return button is pressed
    TSPoint a = ts.getPoint();
    if (a.z > ts.pressureThreshhold) {
       x = localPressWater(a.x,a.y);
       Serial.println(x);
    }
    delay(100);
  }
  return;
  }

void principalMenu(){
  tft.fillScreen(BLACK);
  tft.setCursor(60,35);
  tft.setTextSize(4);
  tft.println("Water");
  tft.drawRect(50,15,140,70,RED);
  tft.setCursor(60,150);
  tft.drawRect(50,130,140,70,RED);
  tft.println("Room");
}

david_prentice

#1
Feb 03, 2016, 07:05 pm Last Edit: Feb 03, 2016, 07:07 pm by david_prentice
Look at my replies in the recent "White screen" thread.

The TFT and Touch share some pins.

Incidentally,   that thread also had Touch on A2, A3 pins.    This seems a very odd design.
Which display,   and where did you buy it?    Please post a link.

David.

paulo_ferreiraa

#2
Feb 03, 2016, 07:20 pm Last Edit: Feb 03, 2016, 07:36 pm by paulo_ferreiraa
Thank you for your answer but i think that's not the problem, because if i show de princpalMenu and print Water or Room to the Serial when the buttons are pressed it works perfectly.
This problem happens when i change from the principalMenu() to waterMenu().
I bought this display on ebay it's a 2.4 tft.

EDIT: It was the problem, thank you so much.

david_prentice

I tried your sketch with my Touch pins.    Yes,   restoring pinMode() stopped the white screen.

I am pleased that it worked for you too.

It would be of interest to others to see a link to your Ebay item.   Especially because of the unusual Touch pins.

I also suggest that you use the map() function to translate your getPoint() values into pixel coordinates.

David.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy