1
General Mach Discussion / Re: How fast can you send G90 and get accurate feedback on the current XYZ position
« on: August 27, 2018, 09:44:26 AM »
Here's the Arduino Sketch for the encoder. Maybe someone might find it useful.
Code: [Select]
#include <digitalWriteFast.h> // library for high performance reads and writes by jrraines
// see http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1267553811/0
// and http://code.google.com/p/digitalwritefast/
// Quadrature encoder
#define c_EncoderPinA 8
#define c_EncoderPinB 7
volatile bool _EncoderASet;
volatile bool _EncoderBSet;
volatile long _EncoderTicks = 0;
volatile bool _lastATick;
volatile bool _lastBTick;
void setup()
{
Serial.begin(115200);
pinMode(c_EncoderPinA, INPUT); // sets pin A as input
digitalWrite(c_EncoderPinA, LOW); // turn on pullup resistors
pinMode(c_EncoderPinB, INPUT); // sets pin B as input
digitalWrite(c_EncoderPinB, LOW); // turn on pullup resistors
}
void loop()
{
_EncoderASet = digitalReadFast(c_EncoderPinA); // read the input pin
_EncoderBSet = digitalReadFast(c_EncoderPinB); // read the input pin
// If the previous and the current state of the outputA are different, that means a Pulse has occured
if (_EncoderASet != _lastATick)
{
// If the outputB state is different to the outputA state, that means the encoder is rotating clockwise
if (_EncoderBSet != _EncoderASet) {
_EncoderTicks ++;
}
else
{
_EncoderTicks --;
}
_lastATick = _EncoderASet;
_lastBTick = _EncoderBSet;
Serial.print(_EncoderTicks);
Serial.print("\n");
}
}