¿ªÔÆÌåÓý

CNC encoder for ubitx


 

#include <RotaryEncoder.h> /*Support for 100 PPR rotary encoder*/
/*/
/*? ? ? ? ? ? ? ? ? ? ? ? ?ROTARY ENCODER? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?*/
/*/
/*We are using an encoder with 100 PPR (Pulses Per Rotation)*/
#define RotaryEncoderPinB 10 /*Encoder pins? - Marked B- on body*/
#define RotaryEncoderPinA 11 /*Marked A- on body*/
#define EncoderRotatedCCW -1 /*Value returned for CCW rotation of encoder*/
/*Setup a RotaryEncoder with 4 steps per pulse for the 2 signal input pins*/
RotaryEncoder encoder(RotaryEncoderPinB, RotaryEncoderPinA, RotaryEncoder::LatchMode::FOUR3);
/*This interrupt routine will be called on any change of one of the input signals*/
static int pos = 0; /*If changed from last pass, encoder has been rotated*/
enum RotaryEncoderDirections /*Which way is the rotary encoder being rotated or not*/
{
? NoRotation, /*No rotation*/
? CwRotation, /*CW rotation*/
? CcwRotation /*CCW rotation*/
};
RotaryEncoderDirections CurrentRotaryEncoderDirection;
/*/
void EncoderInterruptService()
{
? encoder.tick(); /*Call tick() function to check the rotary encoder state*/
}//END EncoderInterruptService
?
/*/
RotaryEncoderDirections CheckEncoderStatus()
{
? encoder.tick(); /*Query the encoder library*/
? int newPos = encoder.getPosition(); /*Get current encoder position*/
? if (pos != newPos) /*Has encoder been rotated*/
? {
? ? if (int(encoder.getDirection()) == EncoderRotatedCCW) /*Query encoder library for rotation direction*/
? ? {
? ? ? pos = newPos; /*Save new rotary encoder position*/
? ? ? return CcwRotation; /*Encoder rotated CCW*/
? ? }
? ? else
? ? {
? ? ? pos = newPos; /*Save new rotary encoder position*/
? ? ? return CwRotation; /*Encoder rotated CW*/
? ? }
? }
? else
? {
? ? return NoRotation; /*ENccoder not rotated*/
? }
}//END CheckEncoderStatus
?
//
void setup()
{
? Serial.begin(9600);
? /*Setup rotary encoder interrupt routines for pin change*/
? attachInterrupt(digitalPinToInterrupt(RotaryEncoderPinB), EncoderInterruptService, CHANGE);
? attachInterrupt(digitalPinToInterrupt(RotaryEncoderPinA), EncoderInterruptService, CHANGE);
}//END setup
?
/*/
/*/
void loop()
{
CurrentRotaryEncoderDirection = CheckEncoderStatus();
? switch(CurrentRotaryEncoderDirection)
? {
? ? case CwRotation:
? ? ? {
? ? ? ?Serial.println("CW");
? ? ? ? ?break;
? ? ? }
? ? case CcwRotation:
? ? ? {
? ? ? ?Serial.println("CCW");
? ? ? ? ?break;
? ? ? }
? ? default:
? ? ? {
? ? ? ? ?break;
? ? ? }
? }
} //END loop
?


Jack, W8TEE
 

There's a library that corrects for that (I forgot who wrote it, but a search should find it), or you can do it in code yourself. Basically, store the count in a static int and reset after 4 clicks instead of one.

Jack, W8TEE

On Wednesday, May 12, 2021, 3:46:49 PM EDT, barry halterman <kthreebo@...> wrote:


Hey fellow ubitx fans, has anyone had luck using a CNC encoder? I purchased one and get 4 pulses for every click of the dial. Not good!
Thanks for any info!
Barry
K3bo

--
Jack, W8TEE


 

Hey fellow ubitx fans, has anyone had luck using a CNC encoder? I purchased one and get 4 pulses for every click of the dial. Not good!
Thanks for any info!
Barry
K3bo