Étape 1: Scetch
#include / / bibliothèque graphique de base
#include / / spécifiques au matériel bibliothèque #include
#define YP A1 / / doit être une broche analogique, utilisez la « Une » notation ! #define XM A2 / / doit être une broche analogique, utilisez la « Une » notation ! #define YM 7 / / peut être une broche numérique #define XP 6 / / peut être un axe numérique
#define TS_MINX 150 #define TS_MINY 120 #define TS_MAXX 920 #define TS_MAXY 940
Pour améliorer la précision pression, nous avons besoin de connaître la résistance / / entre X + et X utiliser n’importe quel multimètre pour le lire / / pour celui que nous utilisons, ses 300 ohms à travers le X plaque tactile ts = écran tactile (XP, YP, XM, YM, 300) ;
Assigner des noms lisibles par l’homme à certaines valeurs communes de couleur 16-bit : #define noir 0 x 0000 #define BLUE 0x001F #define rouge 0xF800 #define vert 0x07E0 #define CYAN 0x07FF #define MAGENTA 0xF81F #define jaune 0xFFE0 #define blanc 0xFFFF
SWTFT tft ;
#define BOXSIZE 40 #define PENRADIUS 3 int oldcolor, currentcolor ;
void setup(void) {Serial.begin(9600) ; Serial.println(F("Paint!")) ; TFT.Reset() ; identificateur d’uint16_t = tft.readID() ;
Serial.Print (F ("puce pilote LCD:")) ; Serial.println (identificateur, HEX) ;
TFT.Begin(identifier) ;
tft.fillScreen(BLACK) ;
tft.fillRect (0, 0, BOXSIZE, BOXSIZE, rouge) ; tft.fillRect (BOXSIZE, 0, BOXSIZE, BOXSIZE, jaune) ; tft.fillRect (BOXSIZE * 2, 0, BOXSIZE, BOXSIZE, vert) ; tft.fillRect (BOXSIZE * 3, 0, BOXSIZE, BOXSIZE, CYAN) ; tft.fillRect (BOXSIZE * 4, 0, BOXSIZE, BOXSIZE, bleu) ; tft.fillRect (BOXSIZE * 5, 0, BOXSIZE, BOXSIZE, MAGENTA) ; tft.fillRect (BOXSIZE * 6, 0, BOXSIZE, BOXSIZE, blanc) ; tft.drawRect (0, 0, BOXSIZE, BOXSIZE, blanc) ; currentColor = rouge ; pinMode (13, sortie) ; }
#define MINPRESSURE 10 #define MAXPRESSURE 1000
void loop() {digitalWrite (13, HIGH); / / Point récemment rebaptisé TSPoint dans la bibliothèque de l’écran tactile / / si vous utilisez une ancienne version de la bibliothèque, utilisez le / / a commenté définition à la place. Point p = ts.getPoint() ; TSPoint p = ts.getPoint() ; digitalWrite (13, faible) ;
logé dans la pins, vous aurez besoin de fixer l’orientation de l’écran tactile broches //pinMode (XP, sortie) ; pinMode (XM, sortie) ; pinMode (YP, sortie) ; pinMode (YM, sortie) ;
Nous avons quelques pression minimale que nous considérons comme « valables » / / pression de 0 ne signifie aucune pression !
Si (p.z > MINPRESSURE & & p.z < MAXPRESSURE) {/ * Serial.print ("X =") ; Serial.Print(p.x) ; Serial.Print ("\tY =") ; Serial.Print(p.y) ; Serial.Print ("\tPressure =") ; Serial.println(p.z) ; * / if (p.y < (TS_MINY-5)) {Serial.println("erase"); / / Appuyez sur le bas de l’écran pour effacer tft.fillRect (0, BOXSIZE, tft.width(), tft.height ()-BOXSIZE, noir);} / / Echelle de 0 -> 1023 à tft.width p.x = tft.width ()-(carte (p.x, TS_MINX, TS_MAXX, tft.width(), 0)) ; p.y = tft.height ()-(carte (p.y, TS_MINY, TS_MAXY, tft.height(), 0)) ; / * Serial.print("(") ; Serial.Print(p.x) ; Serial.Print (",") ; Serial.Print(p.y) ; Serial.println(")") ; * / if (p.y < BOXSIZE) {oldcolor = currentcolor ;
Si (p.x < BOXSIZE) {currentcolor = rouge ; tft.drawRect (0, 0, BOXSIZE, BOXSIZE, WHITE);} ElseIf (p.x < BOXSIZE * 2) {currentcolor = jaune ; tft.drawRect (BOXSIZE, 0, BOXSIZE, BOXSIZE, WHITE);} ElseIf (p.x < BOXSIZE * 3) {currentcolor = vert ; tft.drawRect (BOXSIZE * 2, 0, BOXSIZE, BOXSIZE, WHITE);} ElseIf (p.x < BOXSIZE * 4) {currentcolor = CYAN ; tft.drawRect (BOXSIZE * 3, 0, BOXSIZE, BOXSIZE, WHITE);} ElseIf (p.x < BOXSIZE * 5) {currentcolor = bleu ; tft.drawRect(BOXSIZE*4 0, BOXSIZE, BOXSIZE, BLANC) ; } else if (p.x < BOXSIZE * 6) {currentcolor = MAGENTA ; tft.drawRect (BOXSIZE * 5, 0, BOXSIZE, BOXSIZE, WHITE);}
Si (oldcolor! = currentcolor) {si (oldcolor == rouge) tft.fillRect (0, 0, BOXSIZE, BOXSIZE, rouge); if (oldcolor == jaune) tft.fillRect (BOXSIZE, 0, BOXSIZE, BOXSIZE, jaune); if (oldcolor == vert) tft.fillRect (BOXSIZE * 2, 0, BOXSIZE, BOXSIZE, vert); if (oldcolor == CYAN) tft.fillRect (BOXSIZE * 3, 0, BOXSIZE, BOXSIZE, CYAN); if (oldcolor == bleu) tft.fillRect (BOXSIZE * 4, 0, BOXSIZE, BOXSIZE, BLUE); if (oldcolor == MAGENTA) tft.fillRect (BOXSIZE * 5, 0, BOXSIZE, BOXSIZE, MAGENTA);}} Si (((p.y-PENRADIUS) > BOXSIZE) & & ((p.y+PENRADIUS) < tft.height())) {tft.fillCircle (p.x, p.y, PENRADIUS, currentcolor);}} }