Hi.
My first post, so my apologies if i done something incorrectly (I did read the sticky)
I have a project where i've used code based on the sketch below to move stepper motors with 600 PPR optical encoders. (the encoders are similar to this: https://www.amazon.ca/Encoder-Photoelectric-Incremental-Rotary-2-Phases/dp/B07JM9YRTQ/ref=sr_1_2?dchild=1&keywords=600+ppr+encoder&qid=1634762153&sr=8-2).
The project is using STSPIN820 drivers from pololu. This driver allows full-step, half-step, 1/4-step, 1/8-step, 1/16-step, 1/32-step, 1/128-step, and 1/256-step.
Everything works fine, except I wish I used 50 or 100 PPR encoders. The 600 PPR steppers move the steppers too fast unless I'm microstepping 1/8 or slower. I could even use speeds slower than 1/256.
Is there a simple way to change the script to ignore a given number of pulses from the 600 PPR ROE, to effectively make it a 50 or 100 PPR roe? This is less expensive than buying newer encoders for the project.
thanks in advance
/*******Interrupt-based Rotary Encoder Sketch*******
by Simon Merrett, based on insight from Oleg Mazurov, Nick Gammon, rt, Steve Spence
*/
const int pinA = 2; // Our first hardware interrupt pin is digital pin 2
const int pinB = 3; // Our second hardware interrupt pin is digital pin 3
const int stepPin = 10; // pin for pulsing a step to the stepper driver
const int dirPin = 9; // pin for setting stepper driver direction
const bool CW = 0; // clockwise rotation direction (viewed from encoder body out toward shaft--like CNC Machines)
const bool CCW = 1; // counter-clockwise rotation direction (see above, these directions appear reverse when viewed from shaft side)
//#define SLEEP 12 // Pin 12 connected to SLEEP pin
volatile byte aFlag = 0; // let's us know when we're expecting a rising edge on pinA to signal that the encoder has arrived at a detent
volatile byte bFlag = 0; // let's us know when we're expecting a rising edge on pinB to signal that the encoder has arrived at a detent (opposite direction to when aFlag is set)
volatile byte reading = 0; //somewhere to store the direct values we read from our interrupt pins before checking to see if we have moved a whole detent
volatile long encoderPos = 0; //this variable stores our current value of encoder position. Change to int or uin16_t instead of byte if you want to record a larger range than 0-255
volatile int encoderPosOld = 0; //stores the last encoder position value so we can compare to the current reading and see if it has changed (so we know when to print to the serial monitor)
volatile bool encoderDir = CW; // stores the direction of rotation clockwise = 0 or counter-clockwise = 1
void setup() {
pinMode(pinA, INPUT_PULLUP); // set pinA as an input, pulled HIGH to the logic voltage (5V or 3.3V for most cases)
pinMode(pinB, INPUT_PULLUP); // set pinB as an input, pulled HIGH to the logic voltage (5V or 3.3V for most cases)
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
attachInterrupt(0,PinA,RISING); // set an interrupt on PinA, looking for a rising edge signal and executing the "PinA" Interrupt Service Routine (below)
attachInterrupt(1,PinB,RISING); // set an interrupt on PinB, looking for a rising edge signal and executing the "PinB" Interrupt Service Routine (below)
Serial.begin(115200); // start the serial monitor link
// pinMode(SLEEP, OUTPUT);
// digitalWrite(SLEEP, HIGH); // Wake up EasyDriver
}
void PinA() {
cli(); //stop interrupts happening before we read pin values
reading = PIND & 0xC; // read all eight pin values then strip away all but pinA and pinB's values
if(reading == B00001100 && aFlag) { //check that we have both pins at detent (HIGH) and that we are expecting detent on this pin's rising edge
encoderPos --; //decrement the encoder's position count
encoderDir = CW;
digitalWrite(dirPin, encoderDir);
digitalWrite(stepPin, HIGH);
//delayMicroseconds(5);
digitalWrite(stepPin, LOW);
if (encoderPos < -2147483648L) {
encoderPos = 0L;
}
bFlag = 0; //reset flags for the next turn
aFlag = 0; //reset flags for the next turn
}
else if (reading == B00000100) bFlag = 1; //signal that we're expecting pinB to signal the transition to detent from free rotation
sei(); //restart interrupts
}
void PinB() {
cli(); //stop interrupts happening before we read pin values
reading = PIND & 0xC; //read all eight pin values then strip away all but pinA and pinB's values
if (reading == B00001100 && bFlag) { //check that we have both pins at detent (HIGH) and that we are expecting detent on this pin's rising edge
encoderPos ++; //increment the encoder's position count
encoderDir = CCW;
digitalWrite(dirPin, encoderDir);
digitalWrite(stepPin, HIGH);
//delayMicroseconds(5);
digitalWrite(stepPin, LOW);
if (encoderPos > 2147483647L) {
encoderPos = 0L;
}
bFlag = 0; //reset flags for the next turn
aFlag = 0; //reset flags for the next turn
}
else if (reading == B00001000) aFlag = 1; //signal that we're expecting pinA to signal the transition to detent from free rotation
sei(); //restart interrupts
}
void loop() {
if(encoderPosOld != encoderPos) {
Serial.print(encoderPos);
if(encoderDir == CW) Serial.println(" CW");
if(encoderDir == CCW) Serial.println(" CCW");
encoderPosOld = encoderPos;
// digitalWrite(SLEEP, LOW); // Wake up EasyDriver
}
}```