Code Shield Rotary Encoder Test and Demo

This sketch tests and demonstrates the use of the rotary encoder.

/* Read Quadrature Encoder

* Connect Encoder to Pins encoder0PinA, encoder0PinB, and +5V.

*

* Sketch by max wolf / www.meso.net

* v. 0.1 - very basic functions - mw 20061220

*

*/

#define ENCODER_A 14

#define ENCODER_B 15

#define ENCODER_PORT PINC

#define SWITCH 13

#define BUTTON 12

#define RGB_RED 11

#define RGB_GREEN 10

#define RGB_BLUE 9

#define LED 6

#define SERVO 5

#define PIEZO 3

#define RELAY 2

#define POT 2

#define HALL 3

#define THERMISTOR 4

#define PHOTOCELL 5

int val;

int encoder0Pos = 0;

int encoder0PinALast = LOW;

int n = LOW;

void setup() {

pinMode (ENCODER_A,INPUT);

digitalWrite(ENCODER_A, HIGH);

pinMode (ENCODER_B,INPUT);

digitalWrite(ENCODER_B, HIGH);

Serial.begin (9600);

}

void loop() {

n = digitalRead(ENCODER_A);

if ((encoder0PinALast == LOW) && (n == HIGH)) {

if (digitalRead(ENCODER_B) == LOW) {

encoder0Pos--;

}

else {

encoder0Pos++;

}

Serial.print (encoder0Pos);

Serial.print ("/");

}

encoder0PinALast = n;

}