Page 1 of 1

Rotary Encoder Kit RE08A: How to count pulse and test code

PostPosted: Fri May 09, 2014 11:58 am
by RothmanAwesome
//This is an example on how to count the pulse
from the RE08A


/* Read RE08A Encoder
* Connect GND of RE08A to GND of Arduino
*Connect +5V of RE08A
*Connect Sig of RE08A to Pin 11 of Arduino
*Connect +leg of LED to Pin8
*Connect the -leg of LED to GND of Arduino

* Sketch by Rothman Noh
* v. 0.1 - very basic functions - mw 20061220
*
*/


int ledPin = 8; // choose the pin for the Reference LED
int encoderPin= 11;// Sig from RE08 to pin 11 of Arduino
int val = 0;
int currentState = LOW;
int previousState = LOW;
int counter = 0;
int n = LOW;

void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(encoderPin, INPUT); // declare pushbutton as input
Serial.begin (9600);
}

void loop() {
n = digitalRead(encoderPin);
if ((previousState == LOW) && (n == HIGH))
{
if (digitalRead(currentState) == LOW)
{
counter--;
}
else
{
counter++;
}
Serial.print (counter);
Serial.print ("/"); // To open Serial Monitor push ( Ctrl+Shift+M)
}
previousState= n;
digitalWrite(ledPin, n);//LED will detect any change in 'n'
}