/* Encoder Library - Basic Example
* http://www.pjrc.com/teensy/td_libs_Encoder.html
*
* This example code is in the public domain.
*/
#include <Encoder.h>
// Change these two numbers to the pins connected to your encoder.
// Best Performance: both pins have interrupt capability
// Good Performance: only the first pin has interrupt capability
// Low Performance: neither pin has interrupt capability
Encoder myEnc(5, 6); // or (2,3) on an Uno for 4 counts/pulse
// avoid using pins with LEDs attached
void setup() {
Serial.begin(115200);
pinMode(5,INPUT_PULLUP);
pinMode(6,INPUT_PULLUP);
Serial.println("Basic Encoder Test:");
}
long oldPosition = -999;
void loop() {
long newPosition = myEnc.read();
if (newPosition != oldPosition) {
oldPosition = newPosition;
Serial.println(newPosition);
}
}
The diode keeps a +12V from entering the Arduino, and the INPUT_PULLUP makes the signal HIGH when the encoder output is +12. And when the encoder output is 0, it wil pull the Arduino pin to 0.
Im trying to create a dosing system i.e. every metre send a HIGH signal to my dosing gun. Assuming I can read the encoder Im going to use push buttons to select whether its every one metre, two metres etc.....
Your encoder has a resolution of 5 pulses per mm (1000 pulses/rev) and you do not need the precision of the complete quadrature count. You are also going in one direction and don't need directional information from the increment/decrement of the quadrature counts.
You could use just one channel of output from the encoder with an interrupt count. One channel read FALLING will be 250 counts per revolution.
I think that reading the z axis output once per revolution or 5 counts per meter will be close enough. You could use code like this:
volatile byte count = 0;
volatile boolean meterFlag = false;
void setup()
{
Serial.begin(115200);
Serial.println("start...");
//z axis pulse interrupt on pin3
pinMode(3, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(3), isrCount, FALLING);
}
void loop()
{
if(meterFlag == true)
{
meterFlag = false;
//do what needs to be done every meter
Serial.println("meter");
}
}
void isrCount()
{
count++;
if (count == 5)
{
meterFlag = true;
count = 0;
}
}
Typically an encoder manufacturer careful enough to describe the resolution as "pulses per revolution", as Sick did on their datasheet, would use "counts per revolution" to spec counting 4x per complete quadrature cycle. It's the evil marketing guys that try to confuse the two.
careful enough to describe the resolution as "pulses per revolution", as Sick did on their datasheet, would use "counts per revolution" to spec counting 4x per complete quadrature cycle
Yes, you are correct.
The resolution of the encoder is not a good match for the meter distance requirements of the application, particularly without the use of interrupts.
Reading the z axis pulse is likely to be a better approach.
I agree with that--the accuracy of using the Z to measure the meter-long intervals will match the accuracy of using A or A&B, but will require 1/1000 of the processing.
I take issue with the getting 250 counts/rev out of couting the falling edge of one channel on a 1000PPR encoder. With an encoder spec of 1000CPR, I agree.
I've seen manufacturers specify PPR where one pulse means a compete quadrature cycle. So, one rising edge and one falling edge on each of the A and B outputs .... of course with a quadrature timing relationship to the edges.
Such marketing can indicate further flaws in the item documentation. In so far the actual encoder datasheet lacks essential information, I'd never buy such an encoder.
Meh ...
They work fine and I prefer to think in terms of quadrature cycles. The concept of a "pulse" is not very useful when you're dealing with two signals (on two different lines) that are in a quadrature phase relationship.