I'm a total novice at arduino, and am mostly a copy/paste coder at this point - so of course this makes my goal harder.
I do not understand how to properly use the encoder with so many wires. I can sort of understand that the positive and negative are probably opposite signals from each other, sort of error protection or something? I know that the Z= and Z- are roattion counters at either 90, 180, or some other offset angle to allow ticking full rotations. But am unsure about this all and am reading a lot of material that is beyond me. I'm stumped.
I can install the following code on an UNO and the UNO reads the encoder just fine off the A+ and B+ lines, with increasing count for one direction of rotation and decreasing count for the other direction. So it is reflecting the movement as it should...
BUT- I am getting 8000 pulses per shaft revolution rather than the 2000 pulses per revolution in the documentation. So something's not right here. I should see 2000 pulses per revolution... is this because of the lack of use of the other wires? Or something else?
Code i'm using for just A+ and B+:
/*
*Quadrature Decoder
*/
#include "Arduino.h"
#include <digitalWriteFast.h>
// Quadrature encoders
// Left encoder
#define c_LeftEncoderInterruptA 0
#define c_LeftEncoderInterruptB 1
#define c_LeftEncoderPinA 2
#define c_LeftEncoderPinB 3
#define LeftEncoderIsReversed
volatile bool _LeftEncoderASet;
volatile bool _LeftEncoderBSet;
volatile bool _LeftEncoderAPrev;
volatile bool _LeftEncoderBPrev;
volatile long _LeftEncoderTicks = 0;
void setup()
{
Serial.begin(9600);
// Quadrature encoders
// Left encoder
pinMode(c_LeftEncoderPinA, INPUT); // sets pin A as input
digitalWrite(c_LeftEncoderPinA, LOW); // turn on pullup resistors
pinMode(c_LeftEncoderPinB, INPUT); // sets pin B as input
digitalWrite(c_LeftEncoderPinB, LOW); // turn on pullup resistors
attachInterrupt(c_LeftEncoderInterruptA, HandleLeftMotorInterruptA, CHANGE);
attachInterrupt(c_LeftEncoderInterruptB, HandleLeftMotorInterruptB, CHANGE);
}
void loop()
{
Serial.print("Encoder Ticks: ");
Serial.print(_LeftEncoderTicks);
Serial.print(" Revolutions: ");
Serial.print(_LeftEncoderTicks/2000.0); //2000 Counts Per Revolution, per the engineering manual
Serial.print("\n");
}
// Interrupt service routines for the left motor's quadrature encoder
void HandleLeftMotorInterruptA(){
_LeftEncoderBSet = digitalReadFast(c_LeftEncoderPinB);
_LeftEncoderASet = digitalReadFast(c_LeftEncoderPinA);
_LeftEncoderTicks+=ParseEncoder();
_LeftEncoderAPrev = _LeftEncoderASet;
_LeftEncoderBPrev = _LeftEncoderBSet;
}
// Interrupt service routines for the right motor's quadrature encoder
void HandleLeftMotorInterruptB(){
// Test transition;
_LeftEncoderBSet = digitalReadFast(c_LeftEncoderPinB);
_LeftEncoderASet = digitalReadFast(c_LeftEncoderPinA);
_LeftEncoderTicks+=ParseEncoder();
_LeftEncoderAPrev = _LeftEncoderASet;
_LeftEncoderBPrev = _LeftEncoderBSet;
}
int ParseEncoder(){
if(_LeftEncoderAPrev && _LeftEncoderBPrev){
if(!_LeftEncoderASet && _LeftEncoderBSet) return 1;
if(_LeftEncoderASet && !_LeftEncoderBSet) return -1;
}else if(!_LeftEncoderAPrev && _LeftEncoderBPrev){
if(!_LeftEncoderASet && !_LeftEncoderBSet) return 1;
if(_LeftEncoderASet && _LeftEncoderBSet) return -1;
}else if(!_LeftEncoderAPrev && !_LeftEncoderBPrev){
if(_LeftEncoderASet && !_LeftEncoderBSet) return 1;
if(!_LeftEncoderASet && _LeftEncoderBSet) return -1;
}else if(_LeftEncoderAPrev && !_LeftEncoderBPrev){
if(_LeftEncoderASet && _LeftEncoderBSet) return 1;
if(!_LeftEncoderASet && !_LeftEncoderBSet) return -1;
}
}
.
.
.
Can someone explain the function of, and how to use, all eight wires? Explain why I'm getting 4X the counts that I should with that code above? PLEASE?
.
.
.