Hey everyone,
I'm currently working on a project that's giving me some trouble wrapping my head around. Here's the setup: I've got a Stepper Motor connected to my Arduino Uno via a Motor Shield. Alongside that, I have an incremental rotary encoder, mechanically linked to the stepper rotation using a gear from Baumer electric. You can find the datasheet for the encoder here.
My goal is to use the encoder to keep track of the stepper motor's position, effectively using it as a count. Once the stepper reaches its maximum count, I want it to stop. Then, after a defined period of time, I want the stepper to start turning in the opposite direction until it reaches the minimum count of the encoder.
Ive tried different kind of codes but everyone of them makes the stepper stutter once the encoder is turned, i think that it was because of the program is alternating between two or more void in which its getting the position and making the stepper turn.
#include <Stepper.h>
#include <Encoder.h>
#include <digitalWriteFast.h>
#include <EnableInterrupt.h>
#define encoderA 2
#define encoderB 3
#define encoderZ 4
// Define number of steps per revolution:
const int stepsPerRevolution = 200;
#define pwmA 3
#define pwmB 11
#define brakeA 9
#define brakeB 8
#define dirA 12
#define dirB 13
// Initialize the stepper library on the motor shield:
Stepper myStepper = Stepper(stepsPerRevolution, dirA, dirB);
volatile int countA = 0;
volatile int countB = 0;
volatile int cumulativeCountA = 0;
volatile int cumulativeCountB = 0;
int pulsesPerRev = 900;
int Dir = 0; // 1 = CW
// 0 = Stationary
// -1 = CCW
void setup() {
Serial.begin(9600);
// Set the PWM and brake pins so that the direction pins can be used to control the motor:
pinMode(pwmA, OUTPUT);
pinMode(pwmB, OUTPUT);
pinMode(brakeA, OUTPUT);
pinMode(brakeB, OUTPUT);
digitalWrite(pwmA, HIGH);
digitalWrite(pwmB, HIGH);
digitalWrite(brakeA, LOW);
digitalWrite(brakeB, LOW);
// Set the motor speed (RPMs):
myStepper.setSpeed(30);
pinMode(encoderA, INPUT);
pinModeFast(encoderB, INPUT);
pinMode(encoderZ, INPUT);
enableInterrupt(encoderA, pulseA, RISING);
enableInterrupt(encoderB, pulseB, RISING);
enableInterrupt(encoderZ, pulseZ, RISING);
}
void loop() {
Serial.print(countA);
Serial.print('\t');
Serial.print(countB);
Serial.print('\t');
//Serial.print(getAngle());
//Serial.print('\t');
Serial.print(cumulativeCountA);
Serial.print('\t');
Serial.print(cumulativeCountB);
Serial.print('\t');
Serial.println(Dir);
checkDirection();
if (countA >= (pulsesPerRev - 50)) {
if (Dir == 1) {
myStepper.step(countA - (pulsesPerRev - 50));
} else if (Dir == 0) {
myStepper.step(countA - (-pulsesPerRev + 50));
}
}
}
void checkDirection() {
if ((bool)digitalReadFast(encoderB) == HIGH) { //digitalReadFast() is faster than digitalRead()
Dir = 1;
} else {
Dir = -1;
}
}
void pulseA() {
checkDirection();
countA += Dir;
cumulativeCountA += Dir;
}
void pulseB() {
countB += Dir;
cumulativeCountB += Dir;
}
void pulseZ() {
countA = 0; //reset counters at "home" reference point
countB = 0;
}
Any suggestions or insights into how I can achieve this would be greatly appreciated!