I'm using a DDM-247 Rotary Encoder, which is connected to Pin 2 and 3 of Arduino Uno.
Values are display on a I2C-LCD.
https://www.buerklin.com/de/drehimpulsgeber/p/75e512
It works great with the Encoder.h library from Paul. Thank you for your work.
Every click in both directions is recognised.
Problem:
If i add a counter variable "count" the value of "count" shows a different reading from the variable "newPosition". The value starts the same, after a few clicks, it shows a number always lower than "newPosition". It seems that not every click is counted in "count". But "newPosition" seems to be always right.
What is the problem here?
Values are display on a I2C-LCD.
https://www.buerklin.com/de/drehimpulsgeber/p/75e512
It works great with the Encoder.h library from Paul. Thank you for your work.
Every click in both directions is recognised.
Problem:
If i add a counter variable "count" the value of "count" shows a different reading from the variable "newPosition". The value starts the same, after a few clicks, it shows a number always lower than "newPosition". It seems that not every click is counted in "count". But "newPosition" seems to be always right.
What is the problem here?
Code:
/* Encoder Library - Basic Example
http://www.pjrc.com/teensy/td_libs_Encoder.html
This example code is in the public domain.
Basic_RotaryEncoder_LCD_v0.1
Arduino Uno
*/
#include <Encoder.h>
Encoder myEnc(2, 3);
long newPosition =0;
long oldPosition = 0;
long count = 0;
// LCD Display
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
void setup()
{
// Serial.begin(250000);
// Serial.println("Basic Encoder Test:");
//LCD
lcd.init();
lcd.backlight();
lcd.print(count);
}
void loop()
{
readEnc();
}
void readEnc()
{
newPosition = myEnc.read()/2; // divide by 2, because every rotary click doubles the value
// ignore Values < 0
if (newPosition < 0)
myEnc.write(0);
if (newPosition != oldPosition)
{
count++;
oldPosition = newPosition;
lcdUpdate();
}
}
void lcdUpdate()
{
lcd.print(" ");
lcd.setCursor(0, 0);
lcd.print(count);
lcd.print("ms ");
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(newPosition);
lcd.print("ms");
}