Rotary encoder probleme lenteur code

Salut a tous. Je solicite vos apres plusieurs jours a tourner en rond..

J'ai un rotary encoder avec un code qui fonctionne bien et me donne de bons resultats, il m'indique les numeros croissants et decroissants quand je tourne.. c'est tres reactif... Hors j'ai un autre programme ou j'ai repris ce code, modifie, et helas je me retrouve avec qqchose qui fonctionne a moitie.. il a du mal a prendre les Counter clockwise, et compte difficiliement, on dirais que cela est du a une lenteur d'execution.. J'ai donc cherche a limiter les delays, verifier chaque ligne qui pourrait ralentir, c'est mieux mais toujours pas ca... L'arduino uno et les branchements, setup et pins sont les memes dans les 2 cas, cela donc vient purement du code..
Merci de votre aide.. et me permettre d'apprendre ;-0

Voici le premier code qui fonctionne au poil :

void rotary_Encoder () {
  
  //si actif fait clignoter
//tft.fillScreen(ST77XX_BLACK);
  // Read the current state of CLK
  currentStateCLK = digitalRead(rotaryRight);

  // If last and current state of CLK are different, then pulse occurred
  // React to only 1 state change to avoid double count
  if (currentStateCLK != lastStateCLK  && currentStateCLK == 1){

    // If the DT state is different than the CLK state then
    // the encoder is rotating CCW so decrement
    if (digitalRead(rotaryLeft) != currentStateCLK) {
      counter --;
       //  counter = counter - 5;
      currentDir ="CCW";
    tft.setTextWrap(false);
    //==affichage===
   tft.fillScreen(ST77XX_BLUE);
   tft.setTextSize(2);
  tft.setTextColor(ST77XX_GREEN);
   tft.setCursor(20, 30);
  tft.print("ROTARY");
    tft.setCursor(20, 60);
   tft.print("LEFT");
    tft.setCursor(30, 90);
     tft.setTextColor(ST77XX_MAGENTA);     
   tft.setTextSize(3);
   tft.print(counter);
 // delay(500);
    } else {
      // Encoder is rotating CW so increment
     counter ++;
        // counter = counter + 5;
      currentDir ="CW";
      //==affichage===
   tft.fillScreen(ST77XX_BLUE);
   tft.setTextSize(2);
  tft.setTextColor(ST77XX_GREEN);
   tft.setCursor(20, 30);
  tft.print("ROTARY");
    tft.setCursor(20, 60);
   tft.print("RIGHT");
   tft.setCursor(70, 90);
     tft.setTextColor(ST77XX_MAGENTA);     
   tft.setTextSize(3);
   tft.print(counter);
 // delay(500);
    }
//
//    Serial.print("Direction: ");
//    Serial.print(currentDir);
//    Serial.print(" | Counter: ");
//    Serial.println(counter);
  } //fin avoid double count

  // Remember last CLK state
  lastStateCLK = currentStateCLK;

  // Read the button state
  int btnState = digitalRead(rotaryClick);

  //If we detect LOW signal, button is pressed
  if (btnState == LOW) {
    tft.fillScreen(ST77XX_BLACK);
    //if 50ms have passed since last LOW pulse, it means that the
    //button has been pressed, released and pressed again
    if (millis() - lastButtonPress > 50) {
      //==affichage===
   tft.fillScreen(ST77XX_RED);

   tft.setTextSize(2);
  tft.setTextColor(ST77XX_YELLOW);
     tft.setCursor(20, 30);
  tft.print("ROTARY");
    tft.setCursor(20, 60);
   tft.print("CLICK");
   tft.setCursor(30, 90);
     tft.setTextColor(ST77XX_MAGENTA);     
   tft.setTextSize(3);
   tft.print(counter);
 // delay(500);
     // Serial.println("Button pressed!");
    }

    // Remember last button press event
    lastButtonPress = millis();
  }

  // Put in a slight delay to help debounce the reading
  delay(1);
  //===Pour sorti du mode Rotary
  if (digitalRead(Bouton2) == LOW) {
  mode_Rotary = !mode_Rotary;
   tft.fillScreen(ST77XX_BLACK);
  delay(75);
                                   }        
  }

Et celui qui m'embete ici :

//==================PROGRAMME EN COMMUN AFFICHAGE FIXE==============================

void affichage_commun_Aube() {
  tft.setTextWrap(false);
  //==affichage===
   tft.setTextSize(1.9);
  tft.setTextColor(ST77XX_YELLOW,ST77XX_BLACK);
  tft.setCursor(15, 10);
  tft.print("AUBE ");
  //========
    tft.setCursor(22, 27);
  tft.setTextColor(ST77XX_YELLOW);
  tft.print("Heure : ");
 //===================
   tft.setCursor(15, 55);
     tft.setTextSize(1);
  tft.print("SUNSET "); 
  
  
  
  
  }

  //=====================PROGRAMME PARRIES AVEC VARIABLE================================================
void menu_Aube_Prog () {
  
//  //position du mot 0 actif / inactif
//   tft.setCursor(45, 10);
//   //changement couleur et surlignage selon position curseur
//   if (pos_Menu_Aube == 0)
//   {  tft.setTextColor(ST77XX_MAGENTA,ST77XX_BLUE); }
//   else
//   { tft.setTextColor(ST77XX_YELLOW,ST77XX_BLACK); }
//  if (aube_Actif == true)
//  {
//    tft.print("ACTIVE");
//  }
//  else if (aube_Actif == false) {
//    tft.print("INACTIVE");
//                                                      }
//
//   tft.setCursor(37, 37);
//     //changement couleur et surlignage selon position curseur
//   if (pos_Menu_Aube == 1)
//   {  tft.setTextColor(ST77XX_MAGENTA,ST77XX_BLUE); }
//   else
//   { tft.setTextColor(ST77XX_YELLOW,ST77XX_BLACK); }
//   tft.print(heureAube);
//      //changement couleur et surlignage selon position curseur
//   if (pos_Menu_Aube == 2)
//   {  tft.setTextColor(ST77XX_MAGENTA,ST77XX_BLUE); }
//   else
//   { tft.setTextColor(ST77XX_YELLOW,ST77XX_BLACK); }
//   tft.print(":"); tft.print(minuteAube);
//
//    tft.setCursor(15, 65);
//    //changement couleur et surlignage selon position curseur
//   if (pos_Menu_Aube == 3)
//   {  tft.setTextColor(ST77XX_MAGENTA,ST77XX_BLUE); }
//   else
//   { tft.setTextColor(ST77XX_YELLOW,ST77XX_BLACK); }
//  if (sunset_Actif == true)
//  {
//    tft.print("ACTIF");
//  }
//  else {
//    tft.print("INACTIF");
//  }
//  //si actif on affiche juste le teps restant
//  if (sunset_Actif == true)
//  { tft.setCursor(90, 70);
//    tft.print("Temps Restant :");
//    byte heure = 11;
//     byte minute = 11;
//   byte hrAube = heure - heureAube;
//   byte minAube = minute - minuteAube;
//      //changement couleur et surlignage selon position curseur
//   if (pos_Menu_Aube == 0)
//   {  tft.setTextColor(ST77XX_MAGENTA,ST77XX_BLUE); }
//   else
//   { tft.setTextColor(ST77XX_YELLOW,ST77XX_BLACK); }
//    tft.print(hrAube);
//       //changement couleur et surlignage selon position curseur
//   if (pos_Menu_Aube == 0)
//   {  tft.setTextColor(ST77XX_MAGENTA,ST77XX_BLUE); }
//   else
//   { tft.setTextColor(ST77XX_YELLOW,ST77XX_BLACK); }
//    tft.print(":");tft.print(minAube);
//  }
//  else if (sunset_Actif == false) {
//    tft.setCursor(10, 97);
//    tft.print("Activer dans : ");
//    tft.print(temporisation_Sunset);
//  }

  //si actif fait clignoter
  //tft.fillScreen(ST77XX_BLACK);
  // Read the current state of CLK
  currentStateCLK = digitalRead(rotaryRight);

  // If last and current state of CLK are different, then pulse occurred
  // React to only 1 state change to avoid double count
  if (currentStateCLK != lastStateCLK  && currentStateCLK == 1) {

    // If the DT state is different than the CLK state then
    // the encoder is rotating CCW so decrement
    if (digitalRead(rotaryLeft) != currentStateCLK) {
      pos_Menu_Aube --;
      //pour eviter depassement du nombre de positions de choix
      //on limite 5 positions (inclut  le 0)par
      if ( pos_Menu_Aube < 0) {
        pos_Menu_Aube = 5;
      }
    
    
 
  } else {
    // Encoder is rotating CW so increment
    pos_Menu_Aube ++;

    if ( pos_Menu_Aube > 4) {
      pos_Menu_Aube = 0;
                           }
     
  }

}

// Remember last CLK state
lastStateCLK = currentStateCLK;

// Read the button state
int btnState = digitalRead(rotaryClick);
    //==affichage===
//reglage selon position pos_Menu_Aube ET click
 //si clic sur position alors
if (btnState == LOW) {
   if (millis() - lastButtonPress > 50) {
switch(pos_Menu_Aube) {
//============================
  //aube actif/inactif
  case 0 :
    aube_Actif = !aube_Actif ;  
  break;
//============================
  //heure aube
  case 1 :

  break;
//============================
 //minute aube
  case 2 :

  break;
//============================
  //====sunset actif/inactif===
  case 3 :
 sunset_Actif = !sunset_Actif ;  
  break;
 //============================
    //heure sunset
  case 4 :
break;
//============================
  //minute sunset
  case 5 :

  break;
   
                       } //fin switch
  
                                    
   } // IF MILLIS >50
  // Remember last button press event
  lastButtonPress = millis();

} //BOUTON PRESS == LOW

// Put in a slight delay to help debounce the reading
//delay(1);
//===Pour sorti du mode Rotary
if (digitalRead(Bouton2) == LOW) {
  menu_Aube = !menu_Aube;
  tft.fillScreen(ST77XX_BLACK);
  delay(75);
}
  //afficahe numero
tft.setCursor(5,110);
tft.setTextSize(2);
 tft.setTextColor(ST77XX_YELLOW,ST77XX_BLACK);
tft.print("Pos : ");tft.print(pos_Menu_Aube);
}

commencez par indenter le code pour qu'on voit la structure... c'est illisible tel quel
(et virez le code en commentaires quand vous postez ici, il n'apporte que plus de confusion)

ensuite tout dépend aussi du reste du code qu'on ne voit pas... que fait la loop() ?

pourquoi réinventer la roue... utilisez une bibliothèque comme la encoder library pour gérer l'encodeur... si vous prenez des pins qui supportent les interruptions vous ne manquerez pas de clicks

Bonsoir

et Button pour gérer le bouton poussoir de l'encodeur.

Cordialement
jpbbricole

ou pour consommer français la librairie Button dans easyRun de @bricoleau

:slight_smile:

Bonsoir, merci pour les reponses.. En effet, je vais essayer de clarifier le code... Le revoici je suis en train de paufiner cela ca avance, je viens de trouver qqchose qui m'a fait avance, mais c'est pas encore gagne ;-]

La loop fait ceci : (elle boucle sur le void a l'appui du bouton, jusque la aucun soucis, avec l'autre code c'est le meme ca fonctionne...

  // Read the button state
  int btnState = digitalRead(rotaryClick);

  //If we detect LOW signal, button is pressed
  if (btnState == LOW) {
    menu_Aube = !menu_Aube;
    delay(120);
    //dessin de fond draw Rect
    testtriangles();
    affichage_commun_Aube();
  }
  //fin dessin de fond

  while (menu_Aube == true) {
    menu_Aube_Prog() ;
  }

et envoie vers mon void precedent, ici modifie :

 //=====================PROGRAMME Rotary encoder Action================================================
void menu_Aube_Prog () {
 //affichage_menu_Aube ();
  // Read the current state of CLK
  currentStateCLK = digitalRead(rotaryRight);

  // If last and current state of CLK are different, then pulse occurred
  // React to only 1 state change to avoid double count
  if (currentStateCLK != lastStateCLK  && currentStateCLK == 1) {

    // If the DT state is different than the CLK state then
    // the encoder is rotating CCW so decrement
    if (digitalRead(rotaryLeft) != currentStateCLK) {
      pos_Menu_Aube --;
      //pour eviter depassement du nombre de positions de choix
      //on limite 5 positions (inclut  le 0)par
      if ( pos_Menu_Aube < 0) {
        pos_Menu_Aube = 5;
      }
     // tft.fillScreen(ST77XX_BLACK);
affichage_menu_Aube ();

    
 
  } else {
    // Encoder is rotating CW so increment
    pos_Menu_Aube ++;

    if ( pos_Menu_Aube > 4) {
      pos_Menu_Aube = 0;
                           }
//===partie affichage====
//tft.fillScreen(ST77XX_BLACK);
affichage_menu_Aube ();
                    
     
  }

} //SI CA TOURNE ET D'1 PAS SEULEMENT

// Remember last CLK state
lastStateCLK = currentStateCLK;

// Read the button state
int btnState = digitalRead(rotaryClick);
    //==affichage===
//reglage selon position pos_Menu_Aube ET click
 //si clic sur position alors
if (btnState == LOW) {
   if (millis() - lastButtonPress > 50) {
switch(pos_Menu_Aube) {
//============================
  //aube actif/inactif
  case 0 :
    aube_Actif = !aube_Actif ;  
    //TEST POUR ACTIVER LA LECTURE
  //   currentStateCLK = 10;
     
  break;
//============================
  //heure aube
  case 1 :

  break;
//============================
 //minute aube
  case 2 :

  break;
//============================
  //====sunset actif/inactif===
  case 3 :
  
  break;
 //============================
    //heure sunset
  case 4 :
  sunset_Actif = !sunset_Actif ; 
break;
//============================
  //minute sunset
  case 5 :

  break;
   
                       } //fin switch
  
                                    
   } // IF MILLIS >50
  // Remember last button press event
  lastButtonPress = millis();

} //BOUTON PRESS == LOW

// Put in a slight delay to help debounce the reading
delay(1);

//===Pour sorti du mode Rotary
if (digitalRead(Bouton2) == LOW) {
  menu_Aube = !menu_Aube;
  tft.fillScreen(ST77XX_BLACK);
  delay(75);
}
   //afficahe numero
tft.setCursor(105,110);
tft.setTextSize(1,1);
 tft.setTextColor(ST77XX_RED,ST77XX_BLACK);
tft.print(pos_Menu_Aube);       
}

Ce dernier code fonctionne finalement, j'ai pu resoudre une partie du probleme en separant l'affichage avec un autre void qui est inclus a chaque fois qu'un cran est detecte :

//==========================PARTIE affichage=====================================================
void affichage_menu_Aube () {
   //==================affichage AUBE ==================
   //position du mot 0 actif / inactif
   tft.setCursor(45, 10);
    tft.setTextSize(1);
   //changement couleur et surlignage selon position curseur
   if (pos_Menu_Aube == 0)
   {  tft.setTextColor(ST77XX_MAGENTA,ST77XX_BLUE); tft.setTextSize(1,1);}
   else
   { tft.setTextColor(ST77XX_YELLOW,ST77XX_BLACK); tft.setTextSize(1); }
  if (aube_Actif == true)
  {
    tft.print("ACTIVE   ");
  }
  else if (aube_Actif == false) {
    tft.print("INACTIVE");
                                                      }

   tft.setCursor(37, 37);
     //changement couleur et surlignage selon position curseur
   if (pos_Menu_Aube == 1)
   {  tft.setTextColor(ST77XX_MAGENTA,ST77XX_BLUE); tft.setTextSize(1,1);}
   else
   { tft.setTextColor(ST77XX_YELLOW,ST77XX_BLACK); tft.setTextSize(1);}
   tft.print(heureAube);  
    tft.setTextColor(ST77XX_YELLOW,ST77XX_BLACK); 
   tft.print(":");
      //changement couleur et surlignage selon position curseur
   if (pos_Menu_Aube == 2)
   {  tft.setTextColor(ST77XX_MAGENTA,ST77XX_BLUE);tft.setTextSize(1,1); }
   else
   { tft.setTextColor(ST77XX_YELLOW,ST77XX_BLACK);tft.setTextSize(1); }
 tft.print(minuteAube);
//==================affichage SUNSET=======================
    tft.setCursor(55, 75);
    //changement couleur et surlignage selon position curseur
   if (pos_Menu_Aube == 3)
   {  tft.setTextColor(ST77XX_MAGENTA,ST77XX_BLUE); tft.setTextSize(1,1);}
   else
   { tft.setTextColor(ST77XX_YELLOW,ST77XX_BLACK);tft.setTextSize(1); }
  if (sunset_Actif == true)
  {
    tft.print("ACTIF");
  }
  else {
    tft.print("INACTIF");
  }
  //si actif on affiche juste le teps restant
  if (sunset_Actif == true)
  {   tft.setTextColor(ST77XX_YELLOW,ST77XX_BLACK);
    tft.setCursor(7, 92);
    tft.print("Temps Restant :");
    byte heure = 11;
     byte minute = 11;
   byte hrAube = heure - heureAube;
   byte minAube = minute - minuteAube;
      //changement couleur et surlignage selon position curseur
   if (pos_Menu_Aube == 4)
   {  tft.setTextColor(ST77XX_MAGENTA,ST77XX_BLUE);tft.setTextSize(1,1); }
   else
   { tft.setTextColor(ST77XX_YELLOW,ST77XX_BLACK);tft.setTextSize(1); }
   tft.setTextColor(ST77XX_YELLOW,ST77XX_BLACK);
    tft.print(hrAube);  tft.print(":");
       //changement couleur et surlignage selon position curseur
   if (pos_Menu_Aube == 4)
   {  tft.setTextColor(ST77XX_MAGENTA,ST77XX_BLUE); tft.setTextSize(1,1);}
   else
   { tft.setTextColor(ST77XX_YELLOW,ST77XX_BLACK); tft.setTextSize(1);}
  tft.print(minAube);
  }
  else if (sunset_Actif == false) {
     tft.setTextColor(ST77XX_YELLOW,ST77XX_BLACK);
    tft.setCursor(7, 92);
    tft.print("Activer dans :");
     if (pos_Menu_Aube == 4)
   {  tft.setTextColor(ST77XX_MAGENTA,ST77XX_BLUE); tft.setTextSize(1,1);}
   else
   { tft.setTextColor(ST77XX_YELLOW,ST77XX_BLACK);tft.setTextSize(1); }
    tft.print(temporisation_Sunset);
  }
tft.setTextColor(ST77XX_YELLOW,ST77XX_BLACK); tft.print(" min");
}

Une grande partie de mon soucis de rapidite d'execution se trouve la, c'est a dire qu'en faisant ainsi, ca incremente et decremente bien a chaque cran... mais ca ne m'arrange pas car je veux comme a l'origine que l'affichage se fasse tout le temps, pas seulement si un cran est detecte. En mettant l'affichage hors condition comme avant ca ne marche pas, enfin si car ca detecte un signal, mais tres lentement / aleatoirement..
Voila voila.. merci pour les bibliotheques je vais voir ca aussi..
et desole pour les accents (clavier qwerty ;-0)

Un ctrl-T dans l’IDE pour indenter le code serait un plus….

Votre code qui gère l’encodeur un peu partout c’est pas génial.

Faudrait restructurer le code, C’est typiquement une définition de programme qui se prête bien à la programmation par machine à états (cf mon tuto éventuellement)

Ce sont mes premières en demande sur le forum, je penserai à indenter la prochaine fois (disons que c'est lisible pour moi comme cela, si j'indente automatiquement ça ne me convient pas très bien, mais c'est peut-être une erreur, je vais essayer de m'y faire.
Du coup j'ai bien utilisé la bibliothèque donnée pour tester, mais le résultat était pire, bien que plus rapide, je me retrouve avec des comptes de 4 en 4 (et pas réussi à passer en 1)..

j'ai fini par trouver la solution, en effet comme vous le dites, il semble que l'encodeur a besoin d'une action "rapide" pour que la réponse soit efficace.. En fouillant j'ai trouvé la solution via les Interrupts.. En mettant un interrupt sur l’action de l'encoder (utilisables uniquement sur pin 2 et 3 sur arduino nano / uno). J'ai enfin ce que je voulais, une réponse rapide et efficace de mon encoder avec l'affichage écran.
Encore merci pour l'aide et le tuto !!
Voici le code que je partage si ça peut aider : (en tournant le rotary, on vient activer l'interrupt qui active directement le sous programme "decoder" sans condition. Le compte et decompte du compteur rotary, vient afficher une position sur l’écran en surbrillance, et au clic activer la fonction en surbrillance, pour un réglage d'heure, temporisation, activation / desactivation).
Voili voulou..

//V3.2 --> sortie du mode rotary automatique apres millis()
#include <Adafruit_GFX.h>    // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library for ST7735
#include <Adafruit_ST7789.h> // Hardware-specific library for ST7789
//#define ST77XX_BLACK 0x0000
//#define ST77XX_WHITE 0xFFFF
//#define ST77XX_RED 0xF800
//#define ST77XX_GREEN 0x07E0
//#define ST77XX_BLUE 0x001F
//#define ST77XX_CYAN 0x07FF
//#define ST77XX_MAGENTA 0xF81F
//#define ST77XX_YELLOW 0xFFE0
//#define ST77XX_ORANGE 0xFC00
#include <SPI.h>
//pour encoder
//int counter; //et enleve de setup = 0
int counter = 0 ;
boolean goingUp = false;
boolean goingDown = false;
//Tft definition
#define TFT_CS        10
#define TFT_RST        8 // Or set to -1 and connect to Arduino RESET pin
#define TFT_DC         9
#define TFT_BlackScreen  7

Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);



float p = 3.1415926;

boolean mode_Grille = false;

int counter_x = 0;
int counter_y = 0;
//pour changement X/Y
boolean counter_select = 0;

//====pour menu Aube=====
//rotary
#define rotaryClick 4
#define rotaryRight 2
#define rotaryLeft 3
unsigned long lastButtonPress = 0;
//variables menu aube
boolean menu_Aube = false;
boolean aube_Actif = false ;
boolean sunset_Actif = false ;
//heure aube
byte heureAube = 7;
byte minuteAube = 20 ;
//heure sunset
byte heureSunset = 7;
byte minuteSunset = 20 ;
unsigned long temporisation_Sunset = 15;
unsigned long temporisation_Sunset_Sec = 0;
unsigned long last_time = 0; // Temps antérieur
//int tempsRestantTempoSec = 0;
boolean mode_Reglage_MeAu = false ;
unsigned long DebutCompteurMenuAube = 0 ;


//Boutons et leds
#define Bouton1 A0
#define Bouton2 A1
//Peut être utilisé pour sortie PWM (PINS TESTS SHIELD)
#define ledRouge 5
#define ledBleue 6
//Pour ajout tests
#define test1 A2
#define test2 A3
#define test3 A4
#define test3 A5

//definition taille TFT SCREEN
int Tft_x = 128;
int Tft_y = 128;

void setup() {
  //  counter = 0;
  //====================TFT=====================
  //=========================================
  Serial.begin(9600);

  // OR use this initializer (uncomment) if using a 1.44" TFT:
  tft.initR(INITR_144GREENTAB); // Init ST7735R chip, green tab

  //tft.setSPISpeed(40000000);
  pinMode(TFT_CS, OUTPUT);
  pinMode(TFT_BlackScreen, OUTPUT);
  //active le back Screen (et non le bLack screen)
  digitalWrite(TFT_BlackScreen, HIGH);
  // if the screen is flipped, remove this command
  tft.setRotation(1);
  tft.fillScreen(ST77XX_BLACK);
  //=========================================

  //Pour encoder - bouton - leds
  // Set encoder pins as inputs
  pinMode(rotaryRight, INPUT);
  pinMode(rotaryLeft, INPUT);
  // encoder pin on interrupt 0 (pin 2)
  attachInterrupt(0, decoder, FALLING);

  pinMode(rotaryClick, INPUT_PULLUP);

  pinMode(ledRouge, OUTPUT);
  pinMode(ledBleue, OUTPUT);
  pinMode(Bouton1, INPUT_PULLUP);
  pinMode(Bouton2, INPUT_PULLUP);
}

void loop() {
  //===texte de base si on revient
  //=========================================
  tft.setTextSize(2);
  tft.setCursor(10, 20);
  tft.setTextColor(ST77XX_MAGENTA);
  tft.print("Menu AUBE");
  tft.setCursor(30, 80);
  tft.setTextSize(2);
  tft.setTextColor(ST77XX_WHITE);
  tft.print("TEST");

  //**********************************************************
  //******************PASSAGE EN MODE GRILLE******************
  //**********************************************************
  if (digitalRead(Bouton1) == LOW) {
    //si bouton appuye on passe en mode grille
    mode_Grille = !mode_Grille;
    tft.fillScreen(ST77XX_BLACK);
    //================aFFICHAGE DE LA GRILLE 1 FOIS===================
    //===============(car tres gourmant en memoire si mis dans la boucle====
    tft.setTextSize(1);
    tft.setTextColor(ST77XX_CYAN);
    //boucle for pour afficher ligne X
    for (int x = 0; x <= Tft_x; x = x + 25) {
      tft.setCursor(x, 0);
      tft.print(x);
    }
    //boucle for pour afficher ligne Y
    for (int y = 0; y <= Tft_y; y = y + 25) {
      //si ecran non casse alors :
      // tft.setCursor(0, y);
      //Sinon :
      tft.setCursor(Tft_x - 21, y);
      tft.print(y);
    }
  } // fin if bouton LOW
  //---------------fin affichage grille---------------
  while (mode_Grille == true) {

  }


  //******************************************************
  //****************PASSAGE EN MODE MENU AUBE****************
  //******************************************************
  // Read the button state
  int btnState = digitalRead(rotaryClick);

  //If we detect LOW signal, button is pressed
  if (btnState == LOW) {
    menu_Aube = !menu_Aube;
    DebutCompteurMenuAube = millis();

    delay(120);
    //dessin de fond draw Rect
    testtriangles();
    affichage_commun_Aube();
  }
  //fin dessin de fond

  while (menu_Aube == true) {
    menu_Aube_Prog() ;
  }



}
//================================================================
//=======----------------Programme Principal--------------========
void menu_Aube_Prog () {
 affichage_menu_Aube ();

 //using while statement to stay in the loop for continuous
//interrupts
while(goingUp==1) // CW motion in the rotary encoder
{
goingUp=0; // Reset the flag
counter ++;
if (counter>5) { counter = 0 ; }
Serial.println(counter);
}

while(goingDown==1) // CCW motion in rotary encoder
{
goingDown=0; // clear the flag
counter --;
if (counter<0) { counter = 5 ; }
Serial.println(counter);
}

//=====================================================================
 //================================PARTIE CLIC==========================
// Read the button state
int btnState = digitalRead(rotaryClick);
    //-----affichage-------
//reglage selon position counter ET click
 //si clic sur position alors
if (btnState == LOW) {
   if (millis() - lastButtonPress > 50) {
switch(counter) {
//============================
  //aube actif/inactif
  case 0 : 
  delay(180);
    aube_Actif = !aube_Actif ;  
     
  break;
//============================
  //heure aube
  case 1 :
  delay(180);
 mode_Reglage_MeAu = true ;
 changer_Heure_aube();
  break;
//============================
 //minute aube
  case 2 :
   delay(180);
  mode_Reglage_MeAu = true ;
changer_Minute_aube();
  break;
//============================
  //====sunset actif/inactif===
  case 3 :
   delay(180);
  mode_Reglage_MeAu = true ;
   sunset_Actif = !sunset_Actif ; 
   break;
 //============================
    //Temporisation sunset
  case 4 :
   delay(180);
  mode_Reglage_MeAu = true ;
 changer_Tempo_sunset();
break;
//============================
  //vide
  case 5 :

  break;

                       } //fin switch
  
                                    
   } // IF MILLIS >50
  // Remember last button press event
  lastButtonPress = millis();

} //BOUTON PRESS == LOW

// Put in a slight delay to help debounce the reading
delay(1);


//************pour sortir du mode reglage****************
int nbreSecondes = 5;
if (millis()-DebutCompteurMenuAube > nbreSecondes*1000)
{     menu_Aube = false ;
 tft.fillScreen(ST77XX_BLACK);
} 
delay(75); 

  
  
} //fin void PROG PRINCIPAL


//================================================================================================
//==========================PARTIE affichage=====================================================
//===============================================================================================

//==================PROGRAMME EN COMMUN AFFICHAGE FIXE==============================

void affichage_commun_Aube() {
  tft.setTextWrap(false);
  //==affichage===
   tft.setTextSize(1);
  tft.setTextColor(ST77XX_YELLOW,ST77XX_BLACK);
  tft.setCursor(15, 10);
  tft.print("AUBE ");
  //========
    tft.setCursor(22, 27);
  tft.setTextColor(ST77XX_YELLOW);
  tft.print("Heure : ");
 //===================
   tft.setCursor(15, 75);
     tft.setTextSize(1);
  tft.print("SUNSET "); 
  
  
  
  
  }

//=======================================================================================================
//===================-----------------  Affichage Specifique variables-------------======================
//=======================================================================================================  

void affichage_menu_Aube () {
   //==================affichage AUBE ==================
   //position du mot 0 actif / inactif
   tft.setCursor(45, 10);
    tft.setTextSize(1);
   //changement couleur et surlignage selon position curseur
   if (counter == 0)
   {  tft.setTextColor(ST77XX_YELLOW,ST77XX_BLUE); tft.setTextSize(1,1);}
   else
   { tft.setTextColor(ST77XX_YELLOW,ST77XX_BLACK); tft.setTextSize(1); }
  if (aube_Actif == true)
  {
    tft.print("ACTIVE   ");
  }
  else if (aube_Actif == false) {
    tft.print("INACTIVE");
                                                      }

   tft.setCursor(30, 37);
     //changement couleur et surlignage selon position curseur
   if (counter == 1)
   {  tft.setTextColor(ST77XX_YELLOW,ST77XX_BLUE); tft.setTextSize(1,1);}
   else
   { tft.setTextColor(ST77XX_YELLOW,ST77XX_BLACK); tft.setTextSize(1);}
   if (heureAube<=9) {tft.print("0"); }
   tft.print(heureAube); 
    tft.setTextColor(ST77XX_YELLOW,ST77XX_BLACK); 
   tft.print(":");
      //changement couleur et surlignage selon position curseur
   if (counter == 2)
   {  tft.setTextColor(ST77XX_YELLOW,ST77XX_BLUE);tft.setTextSize(1,1); }
   else
   { tft.setTextColor(ST77XX_YELLOW,ST77XX_BLACK);tft.setTextSize(1); }
   if (minuteAube<=9) {tft.print("0"); }
 tft.print(minuteAube);
//==================affichage SUNSET=======================
    tft.setCursor(55, 75);
    //changement couleur et surlignage selon position curseur
   if (counter == 3)
   {  tft.setTextColor(ST77XX_YELLOW,ST77XX_BLUE); tft.setTextSize(1,1);}
   else
   { tft.setTextColor(ST77XX_YELLOW,ST77XX_BLACK);tft.setTextSize(1); }
  if (sunset_Actif == true)
  {
    tft.print("ACTIF  ");
  }
  else {
    tft.print("INACTIF");
  }
  //si actif on affiche juste le teps restant
  if (sunset_Actif == true)
  {   tft.setTextColor(ST77XX_YELLOW,ST77XX_BLACK);
    tft.setCursor(3, 92);
    tft.print("Dans: "); 
 // tft.setCursor(17, 112);
       unsigned long time_now = millis();  // Temps actuel
    
     // Et que 1 sec s'est écoulée
    if(time_now - last_time >= 1000) {
     temporisation_Sunset_Sec = temporisation_Sunset_Sec-1 ;
        last_time = time_now;
                                      }
    tft.setTextColor(ST77XX_WHITE,ST77XX_BLACK);
    tft.print(temporisation_Sunset_Sec); 
     tft.setTextColor(ST77XX_YELLOW,ST77XX_BLACK);
    tft.print(" secondes      ");
 
  }
  else if (sunset_Actif == false) {
     tft.setTextColor(ST77XX_YELLOW,ST77XX_BLACK);
    tft.setCursor(3, 92);
    tft.print("Activer dans: ");
     if (counter == 4)
   {  tft.setTextColor(ST77XX_YELLOW,ST77XX_BLUE); tft.setTextSize(1,1);}
   else
   { tft.setTextColor(ST77XX_YELLOW,ST77XX_BLACK);tft.setTextSize(1); }
    tft.print(temporisation_Sunset);
  }
tft.setTextColor(ST77XX_YELLOW,ST77XX_BLACK); tft.print(" min ");
}

//==========================================================================================================
  //=====================PROGRAMME Rotary encoder Action================================================
void decoder()
//very short interrupt routine 
//Remember that the routine is only called when pin1
//changes state, so it's the value of pin2 that we're
//interrested in here
{
//Si routine detectee, alors on reset la tempo compteur (pour remttre le delai a 0)
DebutCompteurMenuAube = millis();
//puis on  verifie le sens
if (digitalRead(rotaryRight) == digitalRead(rotaryLeft))
{
goingUp = 1; //if encoder channels are the same, direction is CW
}
else
{
goingDown = 1; //if they are not the same, direction is CCW
}
}


//=========================================PROGRAMMES POUR CHANGEMENT AU CLIC======================================================
//============heure aube===========
void changer_Heure_aube() {

  while (mode_Reglage_MeAu == true) {
   tft.setCursor(30, 37);
  tft.setTextColor(ST77XX_RED,ST77XX_BLUE);
  if (heureAube<=9) {tft.print("0"); }
   tft.print(heureAube);  
 //using while statement to stay in the loop for continuous
//interrupts
//detachInterrupt(digitalPinToInterrupt(0));
//decoder();
while(goingUp==1) // CW motion in the rotary encoder
{
goingUp=0; // Reset the flag
heureAube ++;
if(heureAube>23) { heureAube = 0 ; }
}

while(goingDown==1) // CCW motion in rotary encoder
{
goingDown=0; // clear the flag
heureAube --;
if(heureAube<1) { heureAube = 0 ; }
}
delay(1);
//pour sortir du mode reglage
int btnState = digitalRead(rotaryClick);
    //==affichage===
//reglage selon position counter ET click
 //si clic sur position alors
if (btnState == LOW) { mode_Reglage_MeAu = false ;} 
delay(150); 
  } // fin while mode reglage

  } // fin void heure Aube
//===========Minute aube=============
  void changer_Minute_aube() {
 while (mode_Reglage_MeAu == true) {
   tft.setCursor(48, 37);
  tft.setTextColor(ST77XX_RED,ST77XX_BLUE);
  if (minuteAube<=9) {tft.print("0"); }
   tft.print(minuteAube);  
 //using while statement to stay in the loop for continuous
//interrupts
//detachInterrupt(digitalPinToInterrupt(0));
//decoder();
while(goingUp==1) // CW motion in the rotary encoder
{
goingUp=0; // Reset the flag
minuteAube ++;
if(minuteAube>59) { minuteAube = 0 ; }
}

while(goingDown==1) // CCW motion in rotary encoder
{
goingDown=0; // clear the flag
minuteAube --;
if(minuteAube<1) { minuteAube = 0 ; }
}
delay(1);
//pour sortir du mode reglage
int btnState = digitalRead(rotaryClick);
    //==affichage===
//reglage selon position counter ET click
 //si clic sur position alors
if (btnState == LOW) { mode_Reglage_MeAu = false ;} 
delay(150); 
  } // fin while mode reglage
  } //fin void mnute aube
//===========tempo sunset=============
  void changer_Tempo_sunset() {
     while (mode_Reglage_MeAu == true) {
     tft.setCursor(88, 92);
  tft.setTextColor(ST77XX_RED,ST77XX_BLUE);
   tft.print(temporisation_Sunset);  
 //using while statement to stay in the loop for continuous
//interrupts
//detachInterrupt(digitalPinToInterrupt(0));
//decoder();
while(goingUp==1) // CW motion in the rotary encoder
{
goingUp=0; // Reset the flag
temporisation_Sunset = temporisation_Sunset + 10;
if(temporisation_Sunset>60) { temporisation_Sunset = 10 ; }
temporisation_Sunset_Sec = (temporisation_Sunset / 60) ;
//tempsRestantTempoSec = temporisation_Sunset_Sec ;
}

while(goingDown==1) // CCW motion in rotary encoder
{
goingDown=0; // clear the flag
temporisation_Sunset = temporisation_Sunset - 10;
if(temporisation_Sunset<10) { temporisation_Sunset = 10 ; }
temporisation_Sunset_Sec = (temporisation_Sunset / 60) ;
//tempsRestantTempoSec = temporisation_Sunset_Sec ;
}
delay(1);

//pour sortir du mode reglage
int btnState = digitalRead(rotaryClick);
    //==affichage===
//reglage selon position counter ET click
 //si clic sur position alors
if (btnState == LOW) { mode_Reglage_MeAu = false ;} 
delay(150); 
  } // fin while mode reglage
  
  }

oui c'est parce qu'il compte toutes les transitions (encodeur quadratique). il suffit de diviser par 4 quand vous regardez le résultat

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.