Ok everybody here's the update.
Here's a link for the encoders specs
First, Zhomeslice your code was broken. There's a reference to what seems to be a variable that is never established and it over all just didn't work. Thank you for the try though.
Second to ToddL1962's recommendations. I believe pulling the isr out of the loop and putting them before the loop as well as making he encoderPos Volatile is what fixed the 360 pulse issue. As such in the codes to follow you will see integer "s" has been changed to 6 (2048/360 rounded up)
I could not find changes to much anything when canceling the delays. In my research I found the Delays in the isr's are what debounce to keep from missing encoder readings. However in my code trials I could not find a difference between the delay being there or not.
Here are 3 new versions of the code. The last one is my current official code.
This first code removes the stepper motor just to check the encoder reading in the serial monitor. Here the encoder reads the appropriate 360 pulses in one rotation. Proving the encoder works. No numbers in the serial monitor are skipped. Here taking out the delay in the isr (doencoder a or b) is what stopped another issue I had where spinning too fast caused the numbers not to skip but to read at incorrect intervals.
// http://thezhut.com/?page_id=22
const int encoderPinA = 2; // right
const int encoderPinB = 3; // left
volatile int encoderPos = 0; // counter
unsigned int lastReportedPos = 1; // change
static boolean rotating = false; // debounce
boolean A_set = false;
boolean B_set = false;
const int stepsPerRevolution = 32;
int s=6;
void setup() {
pinMode(encoderPinA, INPUT_PULLUP); //enabling pullups
pinMode(encoderPinB, INPUT_PULLUP);
attachInterrupt(0, doEncoderA, CHANGE); //pin 2
attachInterrupt(1, doEncoderB, CHANGE); //pin 3
Serial.begin(9600);
}
void doEncoderA() {
// debounce
if ( rotating ); // wait a little until the bouncing is done
// Test transition
if ( digitalRead(encoderPinA) != A_set ) { // debounce once more
A_set = !A_set;
// adjust counter + if A leads B
if ( A_set && !B_set )
encoderPos ++; //change the 1 to steps to take when encoder turned
rotating = false; // no more debouncing until loop() hits again
}
}
// Interrupt on B changing state
void doEncoderB() {
if ( rotating );
if ( digitalRead(encoderPinB) != B_set ) {
B_set = !B_set;
// adjust counter – 1 if B leads A
if ( B_set && !A_set )
encoderPos --; //change the 1 to steps to take when encoder turned
rotating = false;
}
}
void loop() {
rotating = true; // reset the debouncer
if (lastReportedPos != encoderPos) {
Serial.println(encoderPos);
lastReportedPos = encoderPos;
}
}
Now here is the second code which includes the stepper motor. The 360 pulses issue is resolved here as well. It affects the encoder position integer by incrementing and decrementing the integer just like the code above. That prevents the stepper motor from working properly but I don't care about that here I'm just using this code to read from the serial monitor and see how the encoder is being read. Here there can be serious skipping of pulses especially if I spin too fast. Could this be a debouncing issue? Or perhaps just the fact that my stepper motor is not fast (the 28byj-48 stepper motor has a top speed of 15rpm) Why is the inclusion of the stepper motor in the code affecting the pulse intake on the serial monitor?
// http://thezhut.com/?page_id=22
#include <Stepper.h>
const int encoderPinA = 2; // right
const int encoderPinB = 3; // left
volatile int encoderPos = 0; // counter
unsigned int lastReportedPos = 1; // change
static boolean rotating = false; // debounce
boolean A_set = false;
boolean B_set = false;
const int stepsPerRevolution = 32;
int s=6;
Stepper myStepper(stepsPerRevolution, 8, 10, 9, 11); //h-bridge pins
void setup() {
myStepper.setSpeed(500);
pinMode(encoderPinA, INPUT_PULLUP); //enabling pullups
pinMode(encoderPinB, INPUT_PULLUP);
attachInterrupt(0, doEncoderA, CHANGE); //pin 2
attachInterrupt(1, doEncoderB, CHANGE); //pin 3
Serial.begin(9600);
}
void doEncoderA() {
// debounce
if ( rotating ); // wait a little until the bouncing is done
// Test transition
if ( digitalRead(encoderPinA) != A_set ) { // debounce once more
A_set = !A_set;
// adjust counter + if A leads B
if ( A_set && !B_set )
encoderPos ++; //change the 1 to steps to take when encoder turned
rotating = false; // no more debouncing until loop() hits again
}
}
// Interrupt on B changing state
void doEncoderB() {
if ( rotating );
if ( digitalRead(encoderPinB) != B_set ) {
B_set = !B_set;
// adjust counter – 1 if B leads A
if ( B_set && !A_set )
encoderPos --; //change the 1 to steps to take when encoder turned
rotating = false;
}
}
void loop() {
rotating = true; // reset the debouncer
if (lastReportedPos != encoderPos) {
Serial.println(encoderPos);
lastReportedPos = encoderPos;
myStepper.step(encoderPos);
}
}
And last but not least this is the actual code I am using. Here the encoder and stepper motor just cant get synched very well. ( I know the difference in my "s" integer between 5.689(2048/360) and 6 add up eventually but its off more than that) I think my ultimate question at this point is about proper debouncing should I resort to hardware debouncing rather than this attempt at software debouncing?
// http://thezhut.com/?page_id=22
#include <Stepper.h>
const int encoderPinA = 2; // right
const int encoderPinB = 3; // left
volatile int encoderPos = 0; // counter
unsigned int lastReportedPos = 1; // change
static boolean rotating = false; // debounce
boolean A_set = false;
boolean B_set = false;
const int stepsPerRevolution = 32;
int s=6;
Stepper myStepper(stepsPerRevolution, 8, 10, 9, 11); //h-bridge pins
void setup() {
myStepper.setSpeed(500);
pinMode(encoderPinA, INPUT_PULLUP); //enabling pullups
pinMode(encoderPinB, INPUT_PULLUP);
attachInterrupt(0, doEncoderA, CHANGE); //pin 2
attachInterrupt(1, doEncoderB, CHANGE); //pin 3
Serial.begin(9600);
}
void doEncoderA() {
// debounce
if ( rotating ); // wait a little until the bouncing is done
// Test transition
if ( digitalRead(encoderPinA) != A_set ) { // debounce once more
A_set = !A_set;
// adjust counter + if A leads B
if ( A_set && !B_set )
encoderPos ++; //change the 1 to steps to take when encoder turned
rotating = false; // no more debouncing until loop() hits again
}
}
// Interrupt on B changing state
void doEncoderB() {
if ( rotating );
if ( digitalRead(encoderPinB) != B_set ) {
B_set = !B_set;
// adjust counter – 1 if B leads A
if ( B_set && !A_set )
encoderPos --; //change the 1 to steps to take when encoder turned
rotating = false;
}
}
void loop() {
rotating = true; // reset the debouncer
if (lastReportedPos != encoderPos) {
Serial.println(encoderPos);
lastReportedPos = encoderPos;
myStepper.step(encoderPos);
}
}