Problème de variables avec la lecture d'encodeurs rotatifs

Bonjour, j'essaye de faire s'afficher deux variable differentes sur un écran oled pour ensuite finaliser un projet de controller MIDI avec deux rotary encoder mais j'ai un problème de logique auquel je ne trouve pas de solution : avec le code ci-dessous, dès que D6/9 est enfoncé, newRight/Leftstate est mis a jour mais a chaque itération de la fonction loop newRight/Left devient égale à newRight/Leftstate. Le problème vient du faire que dans la boucle, elle sont égale toutes les deux à knobRight/Left.read() mais c'est ma seule façon de lire l'état des encoders, si quelqu'un à une idée de comment régler le problème, je suis preneur. Merci !

Le code : `#include <SPI.h>
#include <MIDIUSB.h>
#include <Encoder.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Fonts/FreeSans9pt7b.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 32

#define OLED_RESET 4
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

Encoder knobLeft(1, 0);
Encoder knobRight(7, 8);

long positionLeft = -999;
long positionRight = -999;

const int N_ENCODERS = 2;

int lastEncoderValue[N_ENCODERS] = {0};
int lastEncoderMidiValue[N_ENCODERS] = {0};
int encoderValue[N_ENCODERS] = {0};
int encoderMidiValue[N_ENCODERS] = {0};

const int pinD6_left = 6;
const int pinD9_right = 9;

long newLeftstate = 0;
long newRightstate = 0;

void Text(String text, int x, int y, int size) {
display.setTextSize(size);
display.setTextColor(WHITE);
display.setCursor(x, y);
display.println(text);
}

void oledscreen() {
display.clearDisplay();
Text(String(positionLeft), 22, 31, 1);
Text(String(newLeftstate), 22, 13, 1);
Text(String(positionRight), 82, 31, 1);
Text(String(newRightstate), 82, 13, 1);
display.display();
}

void setup() {
pinMode(pinD6_left, INPUT_PULLUP);
pinMode(pinD9_right, INPUT_PULLUP);
//newLeftstate = constrain(newLeftstate, 1, 16);
//newRightstate = constrain(newRightstate, 1, 16);
display.setFont(&FreeSans9pt7b);
Serial.begin(9600);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
oledscreen();
}

void loop() {

int statePinD6 = digitalRead(pinD6_left);
int statePinD9 = digitalRead(pinD9_right);

long newLeft, newRight;
newLeft = knobLeft.read() / 4;
newRight = knobRight.read() / 4;

if (statePinD6 == LOW) {
newLeftstate = knobLeft.read()/4;
}

if (statePinD9 == LOW) {
newRightstate = knobRight.read() / 4;
}

// Limiter les valeurs entre 0 et 100
if (newLeft < 0) {
newLeft = 0;
knobLeft.write(0); // Bloquer le compteur
} else if (newLeft > 100) {
newLeft = 100;
knobLeft.write(400); // Bloquer le compteur à 100 (400 ticks avec le facteur de division par 4)
}

if (newRight < 0) {
newRight = 0;
knobRight.write(0); // Bloquer le compteur
} else if (newRight > 100) {
newRight = 100;
knobRight.write(400); // Bloquer le compteur à 100 (400 ticks avec le facteur de division par 4)
}

if (newLeft != positionLeft && statePinD6 == HIGH) {
Serial.print("Gauche = ");
Serial.print(newLeft);
Serial.print(", Droite = ");
Serial.print(newRight);
Serial.println();
positionLeft = newLeft;

encoderMidiValue[0] = map(positionLeft, 0, 100, 0, 100); // Update MIDI value for left encoder

midiEventPacket_t midiPositionLeft;
midiPositionLeft.header = 0x09; // Note On, channel 1
midiPositionLeft.byte1 = 0xB0;  // Control Change, channel 1
midiPositionLeft.byte2 = 1;  // CC number 1 for left position
midiPositionLeft.byte3 = encoderMidiValue[0];  // Value of left position
MidiUSB.sendMIDI(midiPositionLeft);  // Send MIDI message
MidiUSB.flush();

}

if (newRight != positionRight && statePinD9 == HIGH) {
Serial.print("Gauche = ");
Serial.print(newLeft);
Serial.print(", Droite = ");
Serial.print(newRight);
Serial.println();
positionRight = newRight;

encoderMidiValue[1] = map(positionRight, 0, 100, 0, 100); // Update MIDI value for right encoder

midiEventPacket_t midiPositionRight;
midiPositionRight.header = 0x09; // Note On, channel 1
midiPositionRight.byte1 = 0xB0;  // Control Change, channel 1
midiPositionRight.byte2 = 2;  // CC number 2 for right position
midiPositionRight.byte3 = encoderMidiValue[1];  // Value of right position
MidiUSB.sendMIDI(midiPositionRight);  // Send MIDI message
MidiUSB.flush();

}
oledscreen();
}
`

:warning: La rédaction de votre message ne répond pas aux critères attendus (balises de code, circuit...)

lisez les recommandations listées dans "Les bonnes pratiques du Forum Francophone” et éditez (petit crayon sous votre post) votre premier post pour le corriger (ne repostez pas le code dans un second post)

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