2017-06-14 74 views
1

我想建立一个TFT触摸板menue。现在我的问题是我无法在循环函数中绘制任何东西。Arduino的TFT触摸屏蔽:绘制循环

如果我写的任何抽奖功能在循环屏幕变成白色。我认为这是因为屏幕需要一些时间来建立。所以我加了一个延迟(1000)。但是,然后屏幕每秒闪烁,这显然也不是我想要的。

接下来我们要做的事情是,当我触摸显示画面时,程序停止工作。在下面的代码中,我有三个绘制函数。其中两个正在工作,一个不是。 (见注释)

#include <SeeedTouchScreen.h> 

#include <TFTConsole.h> 
#include <Adafruit_TFTLCD.h> 
#include <Adafruit_GFX.h> 


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

#define TS_MINX 169 
#define TS_MINY 208 
#define TS_MAXX 1781 
#define TS_MAXY 1820 

#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 BLACK 0x0000 
#define BLUE 0x001F 
#define RED  0xF800 
#define GREEN 0x07E0 
#define CYAN 0x07FF 
#define MAGENTA 0xF81F 
#define YELLOW 0xFFE0 
#define WHITE 0xFFFF 

Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET); 
TouchScreen ts = TouchScreen(XP, YP, XM, YM); 
boolean touched = false; 


void setup() { 
    Serial.begin(9600); 
    Serial.print("Starting..."); 

    tft.reset(); 

    tft.begin(0x9325); 

    tft.setRotation(1); 

    tft.fillScreen(BLACK); 

    //Print "PPM CO" Text 
    tft.setCursor(50, 30); 
    tft.setTextColor(GREEN); 
    tft.setTextSize(3); 
    tft.print("Hello World"); //<- this is displayed fine 
    delay(1000); 
} 
boolean first = true; 
void loop() { 
    if (first) { 
    first = false; 
    tft.drawCircle(119, 160, 20, random(0xFFFF)); //<- This is also displayed 
    } 


    Point p = ts.getPoint(); 


    p.x = map(p.x, TS_MINX, TS_MAXX, 0, 240); 
    p.y = map(p.y, TS_MINY, TS_MAXY, 0, 320); 
    if (p.x <= 240 && p.y <= 320 && p.x >= 0 && p.y >= 0) { 
    Serial.println("don't touch me!"); 
    touched = true; 
    } 
    else { 
    touched = false; 
    } 
    if (touched) { 

    tft.drawCircle(119, 180, 20, RED); //<- This is not displayed and makes the screen flash 
    delay(1000); 
    } 


    //tft.fillScreen(BLUE); 
    //delay(500); 

} 

只有当我的代码变量感动不切换回假,如果我停止触摸显示屏内的最后画圆。

有没有人有线索我做错了什么?

更新:我通过触摸显示屏让触摸事件只发生一次,使显示屏停止闪烁。但我仍然有问题,没有任何东西...

void loop() { 
    //Touchposition bestimmen 
    Point p = ts.getPoint(); 
    p.x = map(p.x, TS_MINX, TS_MAXX, 0, 240); 
    p.y = map(p.y, TS_MINY, TS_MAXY, 0, 320); 
    if (p.x <= 240 && p.y <= 320 && p.x >= 0 && p.y >= 0) { 
    if (released) { 
     touched = true; 
     released = false; 
    } 
    } 
    else { 
    //Serial.println("Touch me where I like it!"); 
    touched = false; 
    released = true; 
    } 
    if (touched && !released) { 
    Serial.println("don't touch me!"); 
    drawButton(100, 100, "Manuell"); 
    touched = false; 
    delay(500); 
    } 

} 

回答

0

我注意到,TFT显示屏和触摸屏共享一些模拟引脚。我认为这是造成问题的原因。 所以我把我的代码分成了触摸屏处理的部分。然后我可以重新分配引脚到TFT显示器并绘制到它。我不知道这个理论是否让SENCE,但它的工作:

void loop() { 
    //Touchposition bestimmen 
    if (ts.isTouching()) { 
    Point p = ts.getPoint(); 
    p.x = map(p.x, TS_MINX, TS_MAXX, 0, 240); 
    p.y = map(p.x, TS_MINY, TS_MAXY, 0, 320); 
    if (released) { 
     released = false; 
    } 
    } 
    else { 
    //Serial.println("Touch me where I like it!"); 
    released = true; 
    } 

    if (ts.isTouching() && !released) { 
    //re assing pins to tft because they are also used by the touchscreen 
    Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET); 
    Serial.println("don't touch me!"); 
    drawButton(100, 100, "Manuell"); 
    touched = false; 
    } 
} 

的drawButton funton包含几个平局功能