Hi,
I would like to ask for some advice on possibly improving my sketch.
I've been playing around with a Keys Rotary Encoder Module/Breakout Board, a 20,4 LCD and the PJRC Encoder Library.
I wanted to achieve 1 count for every detent/click of the Encoder.
I managed to get the sketch working on the LCD too my liking, but kept thinking that it could be improved. So I've tried to improve the sketch. It still works, but I now get +0 instead of 00 for the first reading. Anti Clockwise goes to -1,-2 and Clockwise goes +1,+2 etc.
I've left "commented-out" what the unimproved sketch was, and the last 8 lines of the sketch are my 'so-called' improvements.
/* Encoder Library - Basic Example
http://www.pjrc.com/teensy/td_libs_Encoder.html
This example code is in the public domain.
*/
#include <LiquidCrystal.h>
#include <Encoder.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// Change these two numbers to the pins connected to your encoder.
// Best Performance: both pins have interrupt capability
// Good Performance: only the first pin has interrupt capability
// Low Performance: neither pin has interrupt capability
Encoder myEnc(20, 21);
// avoid using pins with LEDs attached
void setup()
{
lcd.begin(20, 4);
Serial.begin(9600);
Serial.println("Basic Encoder Test:");
lcd.print("Basic Encoder Test:");
}
long oldPosition = -999;
void loop() {
long newPosition = myEnc.read();
if (newPosition != oldPosition) {
oldPosition = newPosition;
Serial.println(newPosition / 4);
lcd.setCursor(0, 1);
lcd.print("Test = ");
/*lcd.setCursor(7, 1);
if (newPosition >= 1) {
lcd.print("+");
}
if (newPosition == 0) {
lcd.print("0");
}
lcd.print(newPosition / 4);
delay(50);
lcd.print(" ");
}
}*/
if (newPosition >= 1 || newPosition ==0)
{
lcd.print("+") || ("0");
}
lcd.print (newPosition / 4);
lcd.print(" ");
}
}
I'd be grateful for some pointers.
Dizzwold.