Go Down

Topic: TFTLCD if statements (Read 1 time) previous topic - next topic

b8el018

Jan 23, 2015, 10:34 pm Last Edit: Jan 24, 2015, 11:10 am by b8el018
So I finally got my mcufriend TFTLCD to work and everything seems to work well other than one thing so far.

What I want to happen is turn on an LED from pressing a button on the Display, which I have done fine. But, as soon as I add, for example :
Code: [Select]
tft.fillRect(0, 0, 50, 50, BLUE);
 into the if statement it doesn't respond at all. By that I mean the LED still comes on but that is all, no rectangles drawn.

I put this same Rectangle command into the void setup and it drew the exact rectangle. Can someone help?

Thanks

Here's the snippet that doesn't do what I expect. (Expecting it to draw a Blue rectangle at 0, 0 with a height and width of 50 when pressure is detected within the set area and then draw a black square of the same dimensions at 0, 0 if touch is detected within the next set area).

Code: [Select]
if (p.y > 57 && p.y < 137 && p.x > 100 && p.x < 182) { 
     digitalWrite(12, HIGH);
     tft.fillRect(0, 0, 50, 50, BLUE);
     }
     
   
     
    if (p.y > 156 && p.y < 237 && p.x > 105 && p.x < 188) {
   
     digitalWrite(12, LOW);
     tft.fillRect(0, 0, 50, 50, BLACK);
   
    }



This piece of code is in the loop funtion

AWOL

Quote
here's the code that won't work :
sp "Here's the snippet that doesn't do what I expect"

Quote
This piece of code is in my void loop.
sp. "This piece of code is in the loop function."
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

b8el018

Ok, sorry about that. But have you got any ideas why it isn't doing what I expect? I am expecting it to work to draw a blue rectangle if there is pressure detected within the area I set. This same area is definately correct as it controls LED fine :).

In future I will try to use the proper terms, sorry :)

felixLU

Please, upload here your whole code as an attachment.

May be in some spend time I will work with it.

b8el018

Thanks for helping :)

b8el018

felixLU

Hi b8el018

here you have a code fully functional.

To led driving only uncoment a lines according to port 12. If you use a MEGA please, better using another port from 22 do 53 digital to LED remoting.

In the code you have an anotation, what was wrong and what missed to correct function.

Code: [Select]

#include <Adafruit_GFX.h>    // Core graphics library
#include <Adafruit_TFTLCD.h> // Hardware-specific library
#include <TouchScreen.h>

#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0
#define LCD_RESET A4


#define YP A1  // must be an analog pin, use "An" notation!
#define XM A2  // must be an analog pin, use "An" notation!
#define YM 7   // can be a digital pin
#define XP 6   // can be a digital pin

#define TS_MINX 150
#define TS_MINY 120
#define TS_MAXX 920
#define TS_MAXY 940

// For better pressure precision, we need to know the resistance
// between X+ and X- Use any multimeter to read it
// For the one we're using, its 300 ohms across the X plate
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);


#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 ROZ     0xFBE0
#define GRI     0xBDF7

Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);;

#define MAXPRESSURE = 1000
#define MINPRESSURE = 100
#define BOXSIZE 40
#define PENRADIUS 3



void setup(void) {
  Serial.begin(9600);
  tft.reset();
  uint16_t identifier = tft.readID();
 
  tft.begin(identifier);
  tft.setRotation(1);
  tft.fillScreen(BLACK); // use it for screen background
  // tft.fillRect(0, 0, 500, 500, BLACK);
  tft.setTextColor(WHITE);
  tft.setTextSize(4);
   
  //BUTTON 1----
  tft.fillRect(50, 50, 80, 80, GREEN);
  tft.setTextColor(WHITE);
  tft.setTextSize(4);
  tft.setCursor(65, 75);
  tft.println("ON");
  //BUTTON 2------
  tft.fillRect(150, 50, 80, 80, RED);
  tft.setCursor(157, 75);
  tft.println("OFF");

  //pinMode(12, OUTPUT);
 
 
}

void loop() {

    TSPoint p = ts.getPoint();
     pinMode(XM, OUTPUT);// both must be defined for display
  pinMode(YP, OUTPUT);
   
   p.x = map(p.x, TS_MINX, TS_MAXX, 0, 240);//default is (240, 0) [default puts touch cord. 0=x/y upper right.
   p.y = map(p.y, TS_MINY, TS_MAXY, 0, 320);
   
   
    if (p.z > 100 && p.z < 1000) {
     
   
  if (p.z > ts.pressureThreshhold) {
   Serial.print("X = "); Serial.print(p.x);     
   Serial.print("\tY = "); Serial.print(p.y);  //  \t= space
   Serial.print("\tPressure = "); Serial.println(p.z);
   delay(100); //just to slow down the readings a little bit
   }
   
  if ((p.y > 57) && (p.y < 137) && (p.x > 100) && (p.x < 182)) {  //looks for touch within x/y box area of button 1
     
     //digitalWrite(12, HIGH);
     
   tft.setTextColor(BLACK);
  tft.setTextSize(3); 
     tft.setCursor(70, 150);
  tft.print("LED OFF");
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
    tft.setCursor(70, 150);
  tft.print("LED ON"); 
       
  }
     
   
    if ((p.y > 156) && (p.y < 237) && (p.x > 105) && (p.x < 188)) {
   
    // digitalWrite(12, LOW);
    tft.setTextColor(BLACK);
  tft.setTextSize(3);
   tft.setCursor(70, 150);
  tft.print("LED ON");
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
    tft.setCursor(70, 150);
  tft.print("LED OFF");
    }// off   
  }
delay(50);
}

felixLU

If you dont use a display text, which I have added for better check, please comment it with /* at start and */ at the end of unused text.
Or simply remove it.

Also the same you can use to serial.print actions, that I have used for check your X,Y parameters.

felixLU

If you need to change any parameter or draw any rect. f.e. around my text LED On, LED Off which I have used, than u can add ass following :

Code: [Select]

 if ((p.y > 57) && (p.y < 137) && (p.x > 100) && (p.x < 182)) {  //looks for touch within x/y box area of button 1
     
     //digitalWrite(12, HIGH);
     
   tft.setTextColor(BLACK);
  tft.setTextSize(3); 
     tft.setCursor(70, 150);
  tft.print("LED OFF");
  tft.fillRect(50, 140, 150, 40, BLUE); // here is your rectangular
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
    tft.setCursor(70, 150);
  tft.print("LED ON"); 
       
  }// On
     
   
    if ((p.y > 156) && (p.y < 237) && (p.x > 105) && (p.x < 188)) {
   
    // digitalWrite(12, LOW);
   
    tft.setTextColor(BLACK);
  tft.setTextSize(3);
   tft.setCursor(70, 150);
  tft.print("LED ON");
  tft.fillRect(50, 140, 150, 40, YELLOW); // here is your rectangular
  tft.setTextColor(BLACK);
  tft.setTextSize(3);
    tft.setCursor(70, 150);
  tft.print("LED OFF");
    }// off

b8el018

Thank you so much, I don't have time to try it now, but will definately try it asap.


svit

Hallo, can you show me, your display's library and current sketch?

Go Up