comment j'me fait trop troller ! mouahahhahaha !
Mais ouais, j'ai dû faire une bêtise quelque part.
Plus sérieusement, en suivant les tuto RC (comme celui donné en lien) ça ne marchait pas du tout et en vérifiant à l'analyseur logique, après filtre (du style 10k/100nF) le signal restait quasiment tout le temps à l'état logique 1.
En regardant l'analyseur sans filtre j'ai vu que les rebonds étaient de l'ordre de la µs et en passant sur un filtre RC (100ohms/100nF) j'ai le résultat tout propre en PJ et mon arduino répond (presque) correctement.
J'ai effectivement eu le soucis avec les Serial mais il n'y en a plus depuis longtemps.
Maintenant, quand je tourne l'encodeur, ma variable n'est pas systématiquement incrémentée et je peux même parfois faire 7 ou 8 pas sans qu'ils ne soient pris en compte.
Niveau oscillo, j'en ai juste un tout vieux, cathodique alors c'est chaud patate de visualiser le signal après filtre.
// Définition des broches de commande du décodeur BCD vers décimal (5411).
static const int A = 7;
static const int B = 6;
static const int C = 5;
static const int D = 4;
// Définition des broche de commande d'affichage des digits
static const int MIN_TEN_PIN = 8;
static const int MIN_ONE_PIN = 9;
static const int SEC_TEN_PIN = 10;
static const int SEC_ONE_PIN = 11;
// Définition des broches de l'encodeur
static const byte ENCODER_PIN_A = 2; // Interruption
static const byte ENCODER_PIN_B = 3;
static const int MIN_TEN = 0;
static const int MIN_ONE = 1;
static const int SEC_TEN = 2;
static const int SEC_ONE = 3;
// Le bouton poussoir permettant de lancer le compte-à-rebourd est relié à une broche d'interruption.
//static const int START_PIN = 3;
// Constante de temps de rebond
static unsigned long debounce = 200;
const static byte ENCODER_DEBOUNCE = 100;
// Dernier instant considéré de changement de valeur du bouton poussoir de compte-à-rebourd pour le temps de rebond
unsigned long previousChange = 0;
static const int DEFAULT_START_TIME = 180; // 3 minutes
int minutes = 3;
int seconds = 0;
void setup() {
Serial.begin(9600);
pinMode(A, OUTPUT);
pinMode(B, OUTPUT);
pinMode(C, OUTPUT);
pinMode(D, OUTPUT);
pinMode(MIN_TEN_PIN, OUTPUT);
pinMode(MIN_ONE_PIN, OUTPUT);
pinMode(SEC_TEN_PIN, OUTPUT);
pinMode(SEC_ONE_PIN, OUTPUT);
//pinMode(START_PIN, INPUT_PULLUP);
//attachInterrupt(digitalPinToInterrupt(START_PIN), countDownStart, FALLING);
pinMode(ENCODER_PIN_A, INPUT_PULLUP);
pinMode(ENCODER_PIN_B, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(ENCODER_PIN_A), changeTime, CHANGE);
attachInterrupt(digitalPinToInterrupt(ENCODER_PIN_B), changeTime, CHANGE);
digitalWrite(A, LOW);
digitalWrite(B, LOW);
digitalWrite(C, LOW);
digitalWrite(D, LOW);
digitalWrite(MIN_TEN_PIN, LOW);
digitalWrite(MIN_ONE_PIN, LOW);
digitalWrite(SEC_TEN_PIN, LOW);
digitalWrite(SEC_ONE_PIN, LOW);
}
volatile int startTime = DEFAULT_START_TIME;
bool isCountdownStarted = false;
int count = 0;
int digit = MIN_TEN;
unsigned long previous = 0;
unsigned long previousCountDown = 0;
unsigned long previousEncoderMillis = 0;
void loop() {
if( (millis() - previous) > 3) {
// SEC_ONE case
int actualDigitPin = SEC_ONE_PIN;
int previousDigitPin = SEC_TEN_PIN;
int digitValue = 15; // ne peut être affiché
// Rappel : si le résultat de l'opération est un nombre décimal, arduino conserve uniquement
// la partie entière sans approximation.
int minTenValue = (startTime / 60) / 10;
int minOneValue = startTime / 60 - minTenValue * 10;
int secTenValue = (startTime - (minTenValue*10 + minOneValue) * 60) / 10;
int secOneValue = startTime - (minTenValue*10 + minOneValue) * 60 - secTenValue * 10;
switch(digit) {
case MIN_TEN:
actualDigitPin = MIN_TEN_PIN;
previousDigitPin = SEC_ONE_PIN;
digitValue = minTenValue;
break;
case MIN_ONE:
actualDigitPin = MIN_ONE_PIN;
previousDigitPin = MIN_TEN_PIN;
digitValue = minOneValue;
break;
case SEC_TEN:
actualDigitPin = SEC_TEN_PIN;
previousDigitPin = MIN_ONE_PIN;
digitValue = secTenValue;
break;
default: // SEC_ONE
digitValue = secOneValue;
}
digitalWrite(previousDigitPin, LOW);
setDigitValue(digitValue);
digitalWrite(actualDigitPin, HIGH);
digit++;
if(digit > 3) digit = MIN_TEN;
previous = millis();
}
/*
if(isCountdownStarted && (millis() - previousCountDown > 1000)) {
startTime--;
if(startTime == 0) isCountdownStarted = false;
previousCountDown = millis();
}
*/
}
volatile byte seqA = 0;
volatile byte seqB = 0;
void changeTime() {
//Ne pas utiliser de serial ici ça ralenti le système
byte a = digitalRead(ENCODER_PIN_A);
byte b = digitalRead(ENCODER_PIN_B);
seqA = ((seqA << 1) | a) & 0b00001111;
seqB = ((seqB << 1) | b) & 0b00001111;
if (seqA == 0b00001001 && seqB == 0b00000011){
startTime--;
} else if ( seqA == 0b00000011 && seqB == 0b00001001 ){
startTime++;
}
}
void countDownStart() {
Serial.println();
previousCountDown = millis();
startTime = DEFAULT_START_TIME;
isCountdownStarted = true;
}
void setDigitValue(int valueToDisplay) {
int a0 = bitRead(valueToDisplay, 0);
int a1 = bitRead(valueToDisplay, 1);
int a2 = bitRead(valueToDisplay, 2);
int a3 = bitRead(valueToDisplay, 3);
digitalWrite(A, a0);
digitalWrite(B, a1);
digitalWrite(C, a2);
digitalWrite(D, a3);
}
void plusOne() {
if ((millis() - previousChange) > debounce) {
count++;
if (count > 9) count = 0;
previousChange = millis();
}
}

