Ciao tutti,
avrei bisogno del vostro aiuto per capire come risolvere un problema con un touchscreen Adafruit ILI9341.
In poche parole quello che vorrei (per iniziare a capirne il funzionamento) è creare un singolo pulsante bianco che alla pressione diventa blu e ripremendolo torna bianco, insomma il classico funzionamento di un pulsante on-off.
Il problema è che sto provando in tutti i modi ma il risultato è sempre lo stesso, ovvero il touchscreen mostra correttamente il pulsante bianco all'accensione, alla prima pressione diventa blu, ma da questo punto in poi se lo premo nuovamente inizia a switchare velocemente da bianco a blu fermandosi qualche volta sul bianco e altre volte sul blu.
L'unico modo per riuscire a ottenere quel che voglio è premere molto velocemente il pulsante.
Ho anche provato a inserire un delay come ho letto su altri post ma l'unico risultato è che lo "switch automatico" bianco-blu-bianco-blu... diventa più lento.
Non so se si tratta di un problema di bounce o sto sbagliando qualcosa nello sketch che allego.
Grazie in anticipo a tutti
#include <Adafruit_GFX.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_ILI9341.h>
#include <Adafruit_STMPE610.h>
#define TS_MINX 150
#define TS_MINY 130
#define TS_MAXX 3800
#define TS_MAXY 4000
#define MINPRESSURE 10
#define MAXPRESSURE 1000
#define STMPE_CS 8
Adafruit_STMPE610 ts = Adafruit_STMPE610(STMPE_CS);
#define TFT_CS 10
#define TFT_DC 9
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
int IN_Count = 0;
#define FRAME_X 10
#define FRAME_Y 20
#define FRAME_W 100
#define FRAME_H 100
#define IN_BUTTON_X FRAME_X
#define IN_BUTTON_Y FRAME_Y
#define IN_BUTTON_W (FRAME_W)
#define IN_BUTTON_H FRAME_H
#define OUT_BUTTON_X (IN_BUTTON_X + IN_BUTTON_W + 15)
#define OUT_BUTTON_Y FRAME_Y
#define OUT_BUTTON_W (FRAME_W)
#define OUT_BUTTON_H FRAME_H
void drawFrame()
{
tft.drawRect(FRAME_X, FRAME_Y, FRAME_W, FRAME_H, ILI9341_BLACK);
}
void in_Btn()
{
if (IN_Count == 0) {
tft.fillRect(IN_BUTTON_X, IN_BUTTON_Y, IN_BUTTON_W, IN_BUTTON_H, ILI9341_WHITE);
IN_Count = 1;
} else {
tft.fillRect(IN_BUTTON_X, IN_BUTTON_Y, IN_BUTTON_W, IN_BUTTON_H, ILI9341_BLUE);
IN_Count = 0;
}
drawFrame();
tft.setCursor(OUT_BUTTON_X , OUT_BUTTON_Y + (OUT_BUTTON_H / 2));
tft.setTextColor(ILI9341_BLACK);
tft.setTextSize(2);
tft.println("ON");
}
void setup(void)
{
Serial.begin(9600);
tft.begin();
if (!ts.begin()) {
Serial.println("Unable to start touchscreen.");
}
else {
Serial.println("Touchscreen started.");
}
tft.fillScreen(ILI9341_BLACK);
// origin = left,top landscape (USB left upper)
tft.setRotation(2);
in_Btn();
}
void loop()
{
// See if there's any touch data for us
if (!ts.bufferEmpty())
{
delay(500);
// Retrieve a point
TS_Point p = ts.getPoint();
// Scale using the calibration #'s
// and rotate coordinate system
p.x = map(p.x, 130, 4000, tft.width(), 0);
p.y = map(p.y, 150, 3800, tft.height(), 0);
int y = p.y;
int x = p.x;
if ((x > IN_BUTTON_X) && (x < (IN_BUTTON_X + IN_BUTTON_W))) {
if ((y > IN_BUTTON_Y) && (y <= (IN_BUTTON_Y + IN_BUTTON_H))) {
if (p.z > MINPRESSURE || p.z < MAXPRESSURE) {
in_Btn();
}
}
}
}
}
il modo più semplice è usare un flag che una volta attivato impedisca il ripetersi della chiamata alla funz in_Btn() prima che siano passati tot millisecondi...
infatti se osservi l'esempio originale da cui hai tratto questo, c'è un flag RecordOn che viene impostato nella inBtn() ma viene esaminato nel loop
prova anche a togliere maxpressure e minpressure...
e ovviamente il controlo p.z....
Patrick_M:
il modo più semplice è usare un flag che una volta attivato impedisca il ripetersi della chiamata alla funz in_Btn() prima che siano passati tot millisecondi...
infatti se osservi l'esempio originale da cui hai tratto questo, c'è un flag RecordOn che viene impostato nella inBtn() ma viene esaminato nel loop
prova anche a togliere maxpressure e minpressure...
e ovviamente il controlo p.z....
Grazie Patrick per la risposta, ho provato a seguire il tuo consiglio ma ora il risultato è che appena premo il pulsante parte un loop infinito dove continua commutare tra bianco e blu senza interrompersi mai.
Ti posto il listato così magari riesci ad aiutarmi a capire dove ho sbagliato, grazie
#include <Adafruit_GFX.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_ILI9341.h>
#include <Adafruit_STMPE610.h>
#define TS_MINX 150
#define TS_MINY 130
#define TS_MAXX 3800
#define TS_MAXY 4000
#define MINPRESSURE 10
#define MAXPRESSURE 1000
#define STMPE_CS 8
Adafruit_STMPE610 ts = Adafruit_STMPE610(STMPE_CS);
#define TFT_CS 10
#define TFT_DC 9
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
boolean RecordOn = false;
#define FRAME_X 10
#define FRAME_Y 20
#define FRAME_W 100
#define FRAME_H 100
#define IN_BUTTON_X FRAME_X
#define IN_BUTTON_Y FRAME_Y
#define IN_BUTTON_W (FRAME_W)
#define IN_BUTTON_H FRAME_H
#define OUT_BUTTON_X (IN_BUTTON_X + IN_BUTTON_W + 15)
#define OUT_BUTTON_Y FRAME_Y
#define OUT_BUTTON_W (FRAME_W)
#define OUT_BUTTON_H FRAME_H
void drawFrame()
{
tft.drawRect(FRAME_X, FRAME_Y, FRAME_W, FRAME_H, ILI9341_BLACK);
}
void pressed_Btn()
{
tft.fillRect(IN_BUTTON_X, IN_BUTTON_Y, IN_BUTTON_W, IN_BUTTON_H, ILI9341_BLUE);
drawFrame();
tft.setCursor(OUT_BUTTON_X , OUT_BUTTON_Y + (OUT_BUTTON_H / 2));
tft.setTextColor(ILI9341_BLACK);
tft.setTextSize(2);
tft.println("ON");
RecordOn = false;
}
void released_Btn()
{
tft.fillRect(IN_BUTTON_X, IN_BUTTON_Y, IN_BUTTON_W, IN_BUTTON_H, ILI9341_WHITE);
drawFrame();
tft.setCursor(OUT_BUTTON_X , OUT_BUTTON_Y + (OUT_BUTTON_H / 2));
tft.setTextColor(ILI9341_BLACK);
tft.setTextSize(2);
tft.println("ON");
RecordOn = true;
}
void setup(void)
{
Serial.begin(9600);
tft.begin();
if (!ts.begin()) {
Serial.println("Unable to start touchscreen.");
}
else {
Serial.println("Touchscreen started.");
}
tft.fillScreen(ILI9341_BLACK);
// origin = left,top landscape (USB left upper)
tft.setRotation(2);
released_Btn();
}
void loop()
{
TS_Point p = ts.getPoint();
p.x = map(p.x, 130, 4000, tft.width(), 0);
p.y = map(p.y, 150, 3800, tft.height(), 0);
int y = p.y;
int x = p.x;
if (RecordOn)
{
if((x > IN_BUTTON_X) && (x < (IN_BUTTON_X + IN_BUTTON_W))) {
if ((y > IN_BUTTON_Y) && (y <= (IN_BUTTON_Y + IN_BUTTON_H))) {
Serial.println("Red btn hit");
pressed_Btn();
}
}
}
else //Record is off (RecordOn == false)
{
if((x > IN_BUTTON_X) && (x < (IN_BUTTON_X + IN_BUTTON_W))) {
if ((y > IN_BUTTON_Y) && (y <= (IN_BUTTON_Y + IN_BUTTON_H))) {
Serial.println("Green btn hit");
released_Btn();
}
}
}
Serial.println(RecordOn);
}
Ok, rettifico, il problema precedente era dovuto al fatto che per errore avevo eliminato l'istruzione
(!ts.bufferEmpty()) ora il loop non si ripete però il problema rimane, ovvero ogni volta che premo il pulsante continua a commutare per un po' fino a quando non si ferma su uno dei due colori
prova così
praticamente nel loop legge fintanto che ci sono caratteri nel buffer (while (!ts.bufferEmpty()))
prima di proseguire
#include <Adafruit_GFX.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_ILI9341.h>
#include <Adafruit_STMPE610.h>
#define TS_MINX 150
#define TS_MINY 130
#define TS_MAXX 3800
#define TS_MAXY 4000
#define MINPRESSURE 10
#define MAXPRESSURE 1000
#define STMPE_CS 8
Adafruit_STMPE610 ts = Adafruit_STMPE610(STMPE_CS);
#define TFT_CS 10
#define TFT_DC 9
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
boolean RecordOn = false;
#define FRAME_X 10
#define FRAME_Y 20
#define FRAME_W 100
#define FRAME_H 100
#define IN_BUTTON_X FRAME_X
#define IN_BUTTON_Y FRAME_Y
#define IN_BUTTON_W (FRAME_W)
#define IN_BUTTON_H FRAME_H
#define OUT_BUTTON_X (IN_BUTTON_X + IN_BUTTON_W + 15)
#define OUT_BUTTON_Y FRAME_Y
#define OUT_BUTTON_W (FRAME_W)
#define OUT_BUTTON_H FRAME_H
void drawFrame() {
tft.drawRect(FRAME_X, FRAME_Y, FRAME_W, FRAME_H, ILI9341_BLACK);
}
void pressed_Btn() {
tft.fillRect(IN_BUTTON_X, IN_BUTTON_Y, IN_BUTTON_W, IN_BUTTON_H, ILI9341_BLUE);
drawFrame();
tft.setCursor(OUT_BUTTON_X , OUT_BUTTON_Y + (OUT_BUTTON_H / 2));
tft.setTextColor(ILI9341_BLACK);
tft.setTextSize(2);
tft.println("ON");
RecordOn = false;
}
void released_Btn() {
tft.fillRect(IN_BUTTON_X, IN_BUTTON_Y, IN_BUTTON_W, IN_BUTTON_H, ILI9341_WHITE);
drawFrame();
tft.setCursor(OUT_BUTTON_X , OUT_BUTTON_Y + (OUT_BUTTON_H / 2));
tft.setTextColor(ILI9341_BLACK);
tft.setTextSize(2);
tft.println("ON");
RecordOn = true;
}
void setup(void) {
Serial.begin(9600);
tft.begin();
if (!ts.begin()) {
Serial.println("Unable to start touchscreen.");
} else {
Serial.println("Touchscreen started.");
}
tft.fillScreen(ILI9341_BLACK);
// origin = left,top landscape (USB left upper)
tft.setRotation(2);
released_Btn();
}
void loop() {
// See if there's any touch data for us
TS_Point p ;
while (!ts.bufferEmpty()) {
// Retrieve a point
p = ts.getPoint();
// Scale using the calibration #'s
// and rotate coordinate system
}
p.x = map(p.x, 130, 4000, tft.width(), 0);
p.y = map(p.y, 150, 3800, tft.height(), 0);
int y = p.y;
int x = p.x;
if (RecordOn) {
if ((x > IN_BUTTON_X) && (x < (IN_BUTTON_X + IN_BUTTON_W))) {
if ((y > IN_BUTTON_Y) && (y <= (IN_BUTTON_Y + IN_BUTTON_H))) {
Serial.println("Red btn hit");
pressed_Btn();
}
}
} else { //Record is off (RecordOn == false)
if ((x > IN_BUTTON_X) && (x < (IN_BUTTON_X + IN_BUTTON_W))) {
if ((y > IN_BUTTON_Y) && (y <= (IN_BUTTON_Y + IN_BUTTON_H))) {
Serial.println("Green btn hit");
released_Btn();
}
}
}
Serial.println(RecordOn);
}
Grazie Patrick per il tuo aiuto però non è cambiato nulla
Il pulsante cambia stato solo se premuto molto velocemente, se ad esempio tengo pigiato il pulsante questo continua a cambiare stato fino a quando non lo rilascio e si ferma indifferentemente su bianco o blu.
ho provato a cercare altre info e nell'esempio con la libreria ho trovato scritto questo:
// Option #2 - use hardware SPI, connect to hardware SPI port only!
// SDI to MOSI, SDO to MISO, and SCL to SPI CLOCK
// on Arduino Uno, that's 11, 12 and 13 respectively
// Then pick a CS pin, any pin is OK but we suggest #10 on an Uno
// tie MODE to 3.3V and POWER CYCLE the STMPE610 (there is no reset pin)
//Adafruit_STMPE610 touch = Adafruit_STMPE610(STMPE_CS);
l'ultima riga è quella che usi tu quindi devi usare i pin
11 MOSI con SDI
12 MISO con SDO
13 CLOCK con SCL
e quindi il pin
CS che può essere uno qualunque
domanda sono collegati così?
dopodichè fai una prova con touched
void loop() {
// See if there's any touch data for us
TS_Point p ;
if (ts.touched()) { //<---------------------------
// Retrieve a point
p = ts.getPoint();
// Scale using the calibration #'s
// and rotate coordinate system
}
inoltre potresti provare a cambiare la sensibilità del touch mettendo nel begin() un valore tra 0 e 255
begin(threshvalue) wish a number from 0-255 to set the touch threshhold