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);
}