Here's what I'm trying to do as part of a larger project. I have an LCD screen and a rotary encoder hooked up to my Arduino. I want to display the position of the encoder as a number and as the encoder turns clockwise the number goes up and when the encoder turns counter clockwise the number goes down. My code, while in my head should work for this, doesn't. I didn't write any of this myself except to add the lcd.print(encoderPos); into the interrupts. The rest of the code comes from the rotary encoder section of the playground and the LCD library examples
#include <LiquidCrystal.h>
// encoder setup
#define encoder0PinA 6
#define encoder0PinB 7
int encoderPos = 0;
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup()
{
pinMode(encoder0PinA, INPUT);
pinMode(encoder0PinB, INPUT);
// encoder pin on interrupt 0 (pin 2)
attachInterrupt(0, doEncoderA, CHANGE);
// encoder pin on interrupt 1 (pin 3)
attachInterrupt(1, doEncoderB, CHANGE);
// set up the LCD's number of rows and columns:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("hello, world!");
}
void loop()
{
lcd.clear();
// Turn off the cursor:
lcd.noCursor();
delay(500);
// Turn on the cursor:
lcd.cursor();
delay(500);
}
void doEncoderA()
{
// look for a low-to-high on channel A
if (digitalRead(encoder0PinA) == HIGH)
{
// check channel B to see which way encoder is turning
if (digitalRead(encoder0PinB) == LOW)
{
encoderPos = encoderPos + 1; // CW
lcd.print(encoderPos);
}
else
{
encoderPos = encoderPos - 1; // CCW
lcd.print(encoderPos);
}
}
else // must be a high-to-low edge on channel A
{
// check channel B to see which way encoder is turning
if (digitalRead(encoder0PinB) == HIGH)
{
encoderPos = encoderPos + 1; // CW
lcd.print(encoderPos);
}
else
{
encoderPos = encoderPos - 1; // CCW
lcd.print(encoderPos);
}
}
}
void doEncoderB()
{
// look for a low-to-high on channel B
if (digitalRead(encoder0PinB) == HIGH)
{
// check channel A to see which way encoder is turning
if (digitalRead(encoder0PinA) == HIGH)
{
encoderPos = encoderPos + 1; // CW
lcd.print(encoderPos);
}
else
{
encoderPos = encoderPos - 1; // CCW
lcd.print(encoderPos);
}
}
// Look for a high-to-low on channel B
else
{
// check channel B to see which way encoder is turning
if (digitalRead(encoder0PinA) == LOW)
{
encoderPos = encoderPos + 1; // CW
lcd.print(encoderPos);
}
else
{
encoderPos = encoderPos - 1; // CCW
lcd.print(encoderPos);
}
}
}