Buonasera a tutti,
mi sono cimentato nella lettura dei valori di un encoder rotativo mostrando il valore letto tramite seriale e un LCD 16x2. Nel momento in cui i risultati sono mostrati solo sul serial monitor ho una lettura rapida e continua dei valori,non perdendo nessun valore.Quando invece mostro i valori sull'LCD la frequenza con cui si aggiornano i valori sul serial monitor e di conseguenza anche sul display è piu bassa,mostrando quindi valori discontinui e perdendo parecchi valori tra il dato mostrato e il successivo.C'è qualche errore nel codice o vi è un altro tipo di problema?
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
// encoder
#define encoderPinA 2
#define encoderPinB 3
volatile long pos = 0;
void setup() {
// put your setup code here, to run once:
lcd.init();
lcd.backlight();
Serial.begin(9600);
pinMode(encoderPinA, INPUT_PULLUP);
pinMode(encoderPinB, INPUT_PULLUP);
// encoder pin on interrupt 0 (pin 2)
attachInterrupt(0, doEncoderA, CHANGE);
// encoder pin on interrupt 1 (pin 3)
attachInterrupt(1, doEncoderB, CHANGE);
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println(pos);
lcd.setCursor(0,0);
lcd.print(pos);
delay(300);
lcd.clear();
}
void doEncoderA() {
// look for a low-to-high on channel A
if (digitalRead(encoderPinA) == HIGH) {
// check channel B to see which way encoder is turning
if (digitalRead(encoderPinB) == LOW) {
pos = pos + 1; // CW
} else {
pos = pos - 1; // CCW
}
} else {// look for a high-to-low on channel A
if (digitalRead(encoderPinB) == HIGH) {// check channel B to see which way encoder is turning
pos = pos + 1; // CW
} else {
pos = pos - 1; // CCW
}
}
}
void doEncoderB() {
// look for a low-to-high on channel B
if (digitalRead(encoderPinB) == HIGH) {
// check channel A to see which way encoder is turning
if (digitalRead(encoderPinA) == HIGH) {
pos = pos + 1; // CW
} else {
pos = pos - 1; // CCW
}
} else { // Look for a high-to-low on channel B
// check channel B to see which way encoder is turning
if (digitalRead(encoderPinA) == LOW) {
pos = pos + 1; // CW
} else {
pos = pos - 1; // CCW
}
}
}
Grazie in anticipo a chiunque mi possa aiutare
