Created
July 16, 2025 15:00
-
-
Save ypelletier/37be10a0d77ffd9765667b34abd8a528 to your computer and use it in GitHub Desktop.
Démonstration du shield TFT d'Adafruit avec Arduino Uno
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /************************************************************************************* | |
| Démonstration du shield TFT d'Adafruit et Arduino Uno | |
| 2 boutons permettent de contrôler un chronomètre. | |
| Pour plus d'infos: | |
| https://electroniqueamateur.blogspot.com/2025/07/utilisation-dun-shield-tft-ecran.html | |
| ***************************************************************************************/ | |
| //bibliothèques essentielles | |
| #include <Adafruit_GFX.h> | |
| #include <Adafruit_ILI9341.h> | |
| #include <Adafruit_TSC2007.h> | |
| // données pour la calibration du capteur tactile | |
| // varie légèrement d'un écran à l'autre | |
| #define TS_MINX 270 | |
| #define TS_MINY 270 | |
| #define TS_MAXX 3740 | |
| #define TS_MAXY 3600 | |
| #define TS_MIN_PRESSURE 200 | |
| #define TFT_CS 10 | |
| #define TFT_DC 9 | |
| Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC); | |
| Adafruit_TSC2007 ts; | |
| const uint16_t nombreDeBoutons = 2; | |
| Adafruit_GFX_Button boutons[nombreDeBoutons]; | |
| bool enMarche = 0; // devient 1 quand le chronometre est en marche | |
| unsigned long debut; // contenu de millis() au démarrage du chronomètre | |
| uint16_t boutonChoisi = 0; | |
| uint16_t precedent = 0 ; // valeur du chrono déjà affichée à l'écran | |
| void setup(void) { | |
| Serial.begin(115200); // pour le débogage seulement | |
| tft.begin(); // démarrage de l'affichage à l'écran. | |
| if (!ts.begin()) { // démarrage du capteur tactile | |
| Serial.println("Probleme de demarrage du touch screen controller"); | |
| while (1); | |
| } | |
| Serial.println("Touchscreen en fonction"); | |
| tft.fillScreen(ILI9341_BLACK); // on remplit tout l'écran d'un fond noir | |
| tft.setRotation(1); // mode paysage | |
| // Affichage du mot "chronomètre" dans le haut de l'écran | |
| tft.setCursor(60, 20); | |
| tft.setTextColor(ILI9341_WHITE); | |
| tft.setTextSize(3); | |
| tft.println("CHRONOMETRE"); | |
| // définition et affichage des 2 boutons | |
| const uint16_t xBouton [nombreDeBoutons] = { tft.width() / 3, 2 * tft.width() / 3 }; // coordonnée x du centre de chaque bouton | |
| const char texteBouton [nombreDeBoutons][10] = { "START", "STOP" }; | |
| for (uint8_t i = 0; i < nombreDeBoutons; i++) { | |
| boutons[i].initButton(&tft, | |
| xBouton[i], // position x du centre du bouton | |
| 110, // position y du centre du bouton | |
| 80, // largeur | |
| 50, // hauteur | |
| ILI9341_RED, // couleur du contour | |
| ILI9341_GREEN, //couleur de remplissage | |
| ILI9341_BLACK, // couleur du texte | |
| texteBouton[i], // texte | |
| 2); //taille du texte | |
| boutons[i].drawButton(); | |
| } | |
| } | |
| void loop() { | |
| uint16_t x, y, z1, z2, temp; | |
| if (!boutonChoisi) { // si on attend qu'un bouton soit touché... | |
| // on vérifie si une touche à été détectée: | |
| if (ts.read_touch(&x, &y, &z1, &z2) && (z1 > TS_MIN_PRESSURE)) { | |
| // pour débogage: on affiche les cordonnées brutes dans le moniteur série: | |
| Serial.print("Coordonnees brutes: ("); | |
| Serial.print(x); Serial.print(", "); | |
| Serial.print(y); Serial.print(")"); | |
| // transposition en coordonnées sur l'écran 240 X 320 pixels | |
| if (x < TS_MINX) { | |
| x = TS_MINX; | |
| } | |
| if (x > TS_MAXX) { | |
| x = TS_MAXX; | |
| } | |
| if (y < TS_MINY) { | |
| y = TS_MINY; | |
| } | |
| if (y > TS_MAXY) { | |
| y = TS_MAXY; | |
| } | |
| x = map(x, TS_MINX, TS_MAXX, 0, tft.height()); | |
| y = map(y, TS_MINY, TS_MAXY, 0, tft.width()); | |
| // conversion des données touch en mode paysage | |
| temp = x; | |
| x = y; | |
| y = 240 - temp; | |
| // pour débogage: on affiche les cordonnées converties dans le moniteur série: | |
| Serial.print(" Coordonnees converties: ("); | |
| Serial.print(x); Serial.print(", "); | |
| Serial.print(y); Serial.println(")"); | |
| // un bouton a-t-il été cliqué? | |
| for (uint8_t i = 0; i < nombreDeBoutons; i++) { | |
| if (boutons[i].contains(x, y)) { | |
| boutons[i].press(true); // tell the button it is pressed | |
| } else { | |
| boutons[i].press(false); // tell the button it is NOT pressed | |
| } | |
| } | |
| for (uint8_t i = 0; i < nombreDeBoutons; i++) { | |
| if (boutons[i].isPressed()) { | |
| //Serial.print("Bouton "); | |
| Serial.print("Appui sur le bouton #"); // pour le débogage | |
| Serial.println(boutonChoisi); | |
| boutonChoisi = i + 1 ; | |
| } | |
| } | |
| } // if (!boutonChoisi) | |
| if (boutonChoisi) { | |
| boutons[boutonChoisi - 1].drawButton(true); | |
| Serial.print("Appui sur le bouton #"); // pour le débogage | |
| Serial.println(boutonChoisi); | |
| if (boutonChoisi == 1) { // mise en marche du chrono | |
| enMarche = 1; | |
| debut = millis(); | |
| } | |
| if (boutonChoisi == 2) { // on arrète le chrono | |
| enMarche = 0; | |
| debut = millis(); | |
| } | |
| delay(200); //anti-rebond | |
| boutons[boutonChoisi - 1].drawButton(false); | |
| boutonChoisi = 0; | |
| } | |
| } | |
| dessineChrono(); | |
| } | |
| void dessineChrono (void) { | |
| uint16_t temps; | |
| if (enMarche) { | |
| temps = (millis() - debut) / 1000; | |
| if (temps - precedent) { // inutile de redessiner si le temps à afficher n'a pas changé | |
| tft.fillRect(120, 170, 100, 50, ILI9341_BLACK); | |
| tft.setCursor(130, 170); | |
| tft.setTextColor(ILI9341_WHITE); | |
| tft.setTextSize(4); | |
| tft.println(temps); | |
| precedent = temps; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment