Ok, I need some help. IDE 1.06, Uno, PC Win7Pro.
I started from this Playground info:
Interrupt Library (the Encoder interrupts the processor).
Utilizes any of the ATmega328p pins via the PinChangeInt library.
by GreyGnome
The AdaEncoder library was created for working with basic 2-pin quadrature encoders such as the following:
https://www.adafruit.com/products/377
http://www.sparkfun.com/products/9117
From the Introduction:
This library interfaces with 2-pin encoders (2 pins A and B, then a common pin C). It does not indicate every state change, rather, it reports only when the decoder is turned from one detent position to the next. It is interrupt-driven and designed to be fast and easy to use. The interrupt routine is lightweight, and the programmer is then able to read the direction the encoder turned at their leisure (within reason; what's important is that the library is reasonably forgiving). The library is designed to be easy to use (it bears repeating :-) ) and it is reasonably immune to switch bounce.
See the project page at: http://code.google.com/p/adaencoder/
See a speed discussion at: http://code.google.com/p/adaencoder/wiki/Speed
See the PinChangeInt library project page at: http://code.google.com/p/arduino-pinchangeint/
Here's an example with two encoders connected. Encoder a is connected to pins 2 and 3, b is connected to 5 and 6:
#include <PinChangeInt.h> // necessary otherwise we get undefined reference errors.
#include <AdaEncoder.h>
#define a_PINA 2
#define a_PINB 3
#define b_PINA 5
#define b_PINB 6
int8_t clicks=0;
char id=0;
void setup()
{
Serial.begin(115200);
AdaEncoder::addEncoder('a', a_PINA, a_PINB);
AdaEncoder::addEncoder('b', b_PINA, b_PINB);
}
void loop()
{
encoder *thisEncoder;
thisEncoder=AdaEncoder::genie(&clicks, &id);
if (thisEncoder != NULL) {
Serial.print(id); Serial.print(':');
if (clicks > 0) {
Serial.println(" CW");
}
if (clicks < 0) {
Serial.println(" CCW");
}
}
}
And put the parts for 1 encoder into my sketch on pins 9 (A) & 8 (B).
Went to the two websites and put the libraries into my Prefences path library folder, so I have this for a sketch:
/*
Interrupt Library (the Encoder interrupts the processor).
Utilizes any of the ATmega328p pins via the PinChangeInt library.
by GreyGnome
The AdaEncoder library was created for working with basic 2-pin quadrature encoders such as the following:
https://www.adafruit.com/products/377
http://www.sparkfun.com/products/9117
From the Introduction:
This library interfaces with 2-pin encoders (2 pins A and B, then a common pin C). It does not indicate every state change, rather, it reports only when the decoder is turned from one detent position to the next. It is interrupt-driven and designed to be fast and easy to use. The interrupt routine is lightweight, and the programmer is then able to read the direction the encoder turned at their leisure (within reason; what's important is that the library is reasonably forgiving). The library is designed to be easy to use (it bears repeating :-) ) and it is reasonably immune to switch bounce.
See the project page at: http://code.google.com/p/adaencoder/
See a speed discussion at: http://code.google.com/p/adaencoder/wiki/Speed
See the PinChangeInt library project page at: http://code.google.com/p/arduino-pinchangeint/
Here's an example with two encoders connected. Encoder a is connected to pins 2 and 3, b is connected to 5 and 6:
*/
// modified for just 1 encoder on 9 & 8
#include <ooPinChangeInt.h> // necessary otherwise we get undefined reference errors.
#include <AdaEncoder.h>
#include <SPI.h>
#define a_PINA 9
#define a_PINB 8
int8_t clicks=0;
char id=0;
// test of other pins - press start to light LEDs, press select to turn off LEDs
byte redLed = 14;
byte greenLed = 15;
byte yellowLed = 16;
byte selectPin = 3;
byte startPin = 2;
byte shiftRCK = 19;
byte shiftSCK = 18;
byte shiftSerial = 17;
byte fontArray [] = {
0b00111111, // 0
0b00000110, // 1
0b01011011, // 2
0b01001111, // 3
0b01100110, // 4
0b01101101, // 5
0b01111101, // 6
0b00000111, // 7
0b01111111, // 8
0b01101111, // 9
0b00000000, // blank
};
byte digit1 = 0;
byte digit2 = 1;
void setup(){
pinMode (shiftSCK, OUTPUT);
pinMode (shiftRCK, OUTPUT);
pinMode (shiftSerial, OUTPUT);
pinMode (redLed, OUTPUT);
pinMode (greenLed, OUTPUT);
pinMode (yellowLed, OUTPUT);
pinMode (selectPin, INPUT_PULLUP);
pinMode (startPin, INPUT_PULLUP);
Serial.begin(115200);
AdaEncoder::addEncoder('a', a_PINA, a_PINB);
SPI.begin();
}
void loop(){
if (digitalRead(startPin) == LOW){
digitalWrite (greenLed, HIGH);
digitalWrite (redLed, HIGH);
digitalWrite (yellowLed, HIGH);
}
if (digitalRead (selectPin) == LOW){
digitalWrite (greenLed, LOW);
digitalWrite (redLed, LOW);
digitalWrite (yellowLed, LOW);
}
encoder *thisEncoder;
thisEncoder=AdaEncoder::genie(&clicks, &id);
if (thisEncoder != NULL) {
Serial.print(id);
Serial.print(':');
if (clicks > 0) {
Serial.println(" CW");
digit1 = digit1 + 1;
if (digit1 == 10){
digit1 = 0;
}
digit2 = digit2 + 1;
if (digit2 == 10){
digit2 = 0;
}
digitalWrite (shiftRCK, LOW);
shiftOut(shiftSerial, shiftSCK, MSBFIRST, fontArray[digit2]);
shiftOut(shiftSerial, shiftSCK, MSBFIRST, fontArray[digit1]);
digitalWrite (shiftRCK, HIGH);
}
if (clicks < 0) {
Serial.println(" CCW");
if (digit1 == 0){
digit1 = 10;
}
digit1 = digit1 - 1;
if (digit2 == 0){
digit2 = 10;
}
digit2 = digit2 - 1;
digitalWrite (shiftRCK, LOW);
shiftOut(shiftSerial, shiftSCK, MSBFIRST, fontArray[digit2]);
shiftOut(shiftSerial, shiftSCK, MSBFIRST, fontArray[digit1]);
digitalWrite (shiftRCK, HIGH);
}
}
}