I'm just dipping my toe into the whole Arduino stuff. I'm not a programmer by trade, but have poked around in it here and there. I'm not great, but I comprehend the basics.
I got a kit that included a bunch of widgets, inputs and outputs. I've got a 16x2 LCD screen hooked up and working using code I've found in a couple of tutorials (forgive me, I don't even know which one I'm using at this point).
I've also gotten a rotary encoder hooked up and working using a couple of more tutorials.
So, I tried the next step, of combining the two. I've got the rotary encoder position counter working, and outputting to the LCD screen. When I rotate right, the number goes up, and when I rotate left, the number goes down.
But if I twist the encoder quickly in either direction, the LCD turns to garbage, and doesn't return.
I've looked around and have seen some stuff about rotary encoders skipping numbers (which also happens), but I haven't seen much about it outputting garbage. I should note that the serial monitor also shows garbage data sometimes, but not the same stuff the LCD is showing.
Here's a video:
And here's my current code:
// include the library code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
#define clkPin 0
#define dtPin 1
#define swPin 2 //Click Button
int encoderVal = 0;
int encoderOldVal = 1;
static int oldA = HIGH;
static int oldB = HIGH;
void setup() {
pinMode(clkPin, INPUT);
pinMode(dtPin, INPUT);
pinMode(swPin, INPUT);
digitalWrite(swPin, HIGH);
Serial.begin(9600);
Serial.println("Arduino Running. . . ");
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("Position:");
}
void loop() {
int change = getEncoderTurn();
encoderOldVal = encoderVal;
encoderVal = encoderVal + change;
if(digitalRead(swPin) == LOW)
{
encoderVal = 0;
}
if(encoderVal != encoderOldVal) {
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 1);
// print the number of seconds since reset:
lcd.print(encoderVal);
}
}
int getEncoderTurn(void)
{
int result = 0;
int newA = digitalRead(clkPin);
int newB = digitalRead(dtPin);
if (newA != oldA || newB != oldB)
{
// something changed
if (oldA == HIGH && newA == LOW)
{
result = (oldB * 2 - 1);
}
}
oldA = newA;
oldB = newB;
return result;
}