Program reads rotary encoder correctly without bouncing in this code:
int val = 0;
byte clkPin = 3;
byte dtPin = 2;
bool lastClk;
bool lastDt;
void setup() {
pinMode(clkPin,INPUT);
pinMode(dtPin,INPUT);
lastClk = digitalRead(3);
lastDt = digitalRead(2);
val = 0;
attachInterrupt(digitalPinToInterrupt(clkPin), clkRise, RISING);
Serial.begin(9600);
}
void loop() {
}
void clkRise(){
lastClk = HIGH;
lastDt = digitalRead(dtPin);
if(lastDt == LOW){
val--;
Serial.println(val);
} else{val++; Serial.println(val);}
}
but when i use lcd to show the result, i get massive bounces (i think) in this code:
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
LiquidCrystal_I2C lcd(0x27,20,4);
int val = 0;
byte clkPin = 3;
byte dtPin = 2;
bool lastClk;
bool lastDt;
void setup() {
lcd.begin();
pinMode(clkPin,INPUT);
pinMode(dtPin,INPUT);
lastClk = digitalRead(3);
lastDt = digitalRead(2);
val = 0;
attachInterrupt(digitalPinToInterrupt(clkPin), clkRise, RISING);
Serial.begin(9600);
lcd.home();
}
void loop() {
lcd.print(val);
}
void clkRise(){
lastClk = HIGH;
lastDt = digitalRead(dtPin);
if(lastDt == LOW){
val--;
Serial.println(val);
} else{val++;}
}
it increases and decreases by random numbers between 0-5.I use KY-040 R.E. it has built-in 10k pull up resistors.
This is what i use to debounce:
A or B pin of R.E--10k R----------------------------------Arduino INPUT PIN
|
|
=0.1uF C
|
---------------------GND
lcd.print(val);
. Something likedelay(50);
. Also, if the R.E. has in internal pull-up of 10k, the series resistor between the A/B pin should be lower than 10k. Otherwise the maximum voltage is only 2.5V (Vcc/2). – Gerben Mar 19 '16 at 15:27val
has changed. – Gerben Mar 19 '16 at 16:13