I would like to display on LCD the number of pulses after title "LEG".
i have try this code
/* 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
*
*/
const int poten_pin=0;
int Input_val=0;
int encoder0PinA = 8;
int encoder0PinB = 9 ;
int encoder0Pos = 0;
int encoder0PinALast = LOW;
int n = LOW;
#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
void setup() {
lcd.begin(16, 2);
pinMode (encoder0PinA,INPUT);
pinMode (encoder0PinB,INPUT);
Serial.begin (9600);
lcd.setCursor(0, 1);
lcd.print("LEG:");
lcd.setCursor(0, 0);
lcd.print("DEG:");
}
void loop() {
Input_val=analogRead(poten_pin);
n = digitalRead(encoder0PinA);
lcd.setCursor(4, 0);
lcd.print(Input_val);
if ((encoder0PinALast == LOW) && (n == HIGH)) {
if (digitalRead(encoder0PinB) == LOW) {
encoder0Pos--;
} else {
encoder0Pos++;
}
Serial.print (encoder0Pos);
Serial.print ("/");
lcd.setCursor(4, 1);
lcd.print(encoder0Pos);
delay(5);
}
encoder0PinALast = n;
}
seems to work fine when i rotate very slow.But when speed up dont count or lose count.
You think 400P/R are too many for arduino or i can do somethink with the code?
I have never used all encoder signal.
Did you see the arduino's playground?
below a sketch from playground
Interrupt Example (the Encoder interrupts the processor). Uses both Interrupt pins
Code for reading encoder using 2 interrupts on pin 2 & pin3
Note: the code below uses 2 interrupts to read the full resolution of the encoder. The code above used 1 interrupt. It read half the resolution by only checking EncoderPin A for position, but it freed up an interrupt pin.
#define encoder0PinA 2
#define encoder0PinB 3
volatile unsigned int encoder0Pos = 0;
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);
Serial.begin (9600);
}
void loop(){ //Do stuff here }
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) {
encoder0Pos = encoder0Pos + 1; // CW
}
else {
encoder0Pos = encoder0Pos - 1; // CCW
}
}
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) {
encoder0Pos = encoder0Pos + 1; // CW
}
else {
encoder0Pos = encoder0Pos - 1; // CCW
}
}
Serial.println (encoder0Pos, DEC);
// use for debugging - remember to comment out
}
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) {
encoder0Pos = encoder0Pos + 1; // CW
}
else {
encoder0Pos = encoder0Pos - 1; // CCW
}
}
// Look for a high-to-low on channel B
else {
// check channel B to see which way encoder is turning
if (digitalRead(encoder0PinA) == LOW) {
encoder0Pos = encoder0Pos + 1; // CW
}
else {
encoder0Pos = encoder0Pos - 1; // CCW
}
}
}
I posted this code in another thread. It might help you.
const char encTable[16] ={0, 1, -1, -0, -1, 0, -0, 1, 1, -0, 0, -1, -0, -1, 1, 0};//gives -1, 0 or 1 depending on encoder movement
byte encState;//remembering the encoder output and acting as index for encTable[]
byte inp;
volatile long actPos;//encoder position
void setup(){
Serial.begin(115200);
attachInterrupt(0, updateEnc, CHANGE);//monitor pin 2
attachInterrupt(1, updateEnc, CHANGE);//monitor pin 3
}//setup()
void loop(){
Serial.println(actPos);//report positioin twice a second
delay(500);
}//loop()
void updateEnc(){
encState = ((encState<<2)|((PIND>>2)&3))&15;//use encoder bits and last state to form index
actPos += encTable[encState];//update actual position on encoder movement
}//updateEnc()