Advertisement
nikolas77

bouton tactile pour servomoteur

Apr 15th, 2022
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.33 KB | None | 0 0
  1. #include <Adafruit_GFX.h>
  2. #include <SPI.h>
  3. #include <Wire.h>
  4. #include <Adafruit_ILI9341.h>
  5. #include <Adafruit_STMPE610.h>
  6.  
  7. #define TS_MINX 150
  8. #define TS_MINY 130
  9. #define TS_MAXX 3800
  10. #define TS_MAXY 4000
  11.  
  12. #define STMPE_CS 8
  13. Adafruit_STMPE610 ts = Adafruit_STMPE610(STMPE_CS);
  14.  
  15. #define TFT_CS 10
  16. #define TFT_DC 9
  17. Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
  18.  
  19. #define PressionMin 2
  20. #define PressionMax 1000
  21. unsigned long tempoPression = 0;
  22.  
  23. //*********************************************************************
  24. #define BrocheDroite 22
  25. boolean etatBoutonDroite = false;
  26. boolean LastetatBoutonDroite = true;
  27. //*********************************************************************
  28. #define BrocheGauche 23
  29. boolean etatBoutonGauche = false;
  30. boolean LastetatBoutonGauche = true;
  31.  
  32.  
  33. void setup() {
  34.  
  35. Serial.begin(9600);
  36.  
  37. pinMode(BrocheDroite, OUTPUT);
  38. pinMode(BrocheGauche, OUTPUT);
  39.  
  40. tft.begin();
  41. tft.setRotation(3);
  42. tft.fillScreen(ILI9341_WHITE);
  43.  
  44. if (!ts.begin()) {
  45. while (1);
  46. }
  47. }
  48.  
  49. void loop() {
  50.  
  51. GestionTactile();
  52. GestionBouton();
  53. }
  54. //*********************************************************************
  55. //*** Fonction Gestion Bouton ***********************************************
  56. //********************************************************************************
  57. void GestionBouton(){
  58.  
  59. if (LastetatBoutonDroite != etatBoutonDroite)
  60. {
  61. uint16_t coulBoutonDroite = ILI9341_GREEN;
  62. if (etatBoutonDroite) {coulBoutonDroite = ILI9341_BLUE;}
  63.  
  64. tft.setTextColor(ILI9341_BLACK);
  65. tft.setTextSize(2);
  66. tft.fillRoundRect(225, 70, 85, 40, 12, coulBoutonDroite);
  67. tft.drawRoundRect(223, 68, 89, 44, 12, coulBoutonDroite);
  68. tft.setCursor(235, 85); tft.print(F("Droite"));
  69. LastetatBoutonDroite = etatBoutonDroite;
  70. }
  71. //*******************************************************************************
  72. if (LastetatBoutonGauche != etatBoutonGauche)
  73. {
  74. uint16_t coulBoutonGauche = ILI9341_RED;
  75. if (etatBoutonGauche) {coulBoutonGauche = ILI9341_BLUE;}
  76.  
  77. tft.setTextColor(ILI9341_BLACK);
  78. tft.setTextSize(2);
  79. tft.fillRoundRect(15, 70, 85, 40, 12, coulBoutonGauche);
  80. tft.drawRoundRect(13, 68, 89, 44, 12, coulBoutonGauche);
  81. tft.setCursor(23, 85); tft.print(F("Gauche"));
  82. LastetatBoutonGauche = etatBoutonGauche;
  83. }
  84. }
  85. //*********************************************************************************
  86. //*** Fonction Gestion Tactile de l'écran TFT *************************************
  87. //*********************************************************************************
  88. void GestionTactile (){
  89.  
  90. if (ts.bufferEmpty()) {
  91. return;
  92. }
  93.  
  94. TS_Point p = ts.getPoint();
  95.  
  96. p.x = map(p.x, TS_MINX, TS_MAXX, 0, tft.width());
  97. p.y = map(p.y, TS_MINY, TS_MAXY, 0, tft.height());
  98.  
  99. Serial.print(p.x);
  100. Serial.print(" , ");
  101. Serial.println(p.y);
  102. //**********************************************************************************
  103. if (millis() - tempoPression >= 1000ul && tempoPression != 0 || tempoPression == 0)
  104. {
  105. if (p.x > 230 && p.x < 270 && p.y > 20 && p.y < 70) //Droite
  106. {
  107. etatBoutonDroite = !etatBoutonDroite;
  108. digitalWrite(BrocheDroite, etatBoutonDroite);
  109. }
  110. //**********************************************************************************
  111. if (p.x > 230 && p.x < 270 && p.y > 90 && p.y < 150) //Gauche
  112. {
  113. etatBoutonGauche = !etatBoutonGauche;
  114. digitalWrite(BrocheGauche, etatBoutonGauche);
  115. }
  116. tempoPression = millis();
  117. }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement