Hey all,
I wrote the code below that reads an encoder and outputs the corresponding angle. However, when I add a piece of code to run a stepper motor, the stepper motor will run, but the position of the encoder won't be printed during the run of the stepper motor. It only prints the starting position and the end position. I want to it to print during the entire cycle. How could I achieve this?
#include <AccelStepper.h>
#define motorInterfaceType 1
#define CHA 2
#define CHB 3
#define dirPin 8 //dir motorcontroller
#define stepPin 9 // step motorcontroller
volatile int master_count = 0; // universal count
volatile byte INTFLAG1 = 0; // interrupt status flag
volatile int var_degrees = 0; // encoder output in degrees
int stepsPerRevolution = 800; //microstepping incl
boolean doprint = true;
AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);
void setup() {
pinMode(CHA, INPUT);
pinMode(CHB, INPUT);
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
Serial.begin(9600);
Serial.println(master_count);
stepper.setMaxSpeed(1000);
stepper.setAcceleration(700);
attachInterrupt(digitalPinToInterrupt(CHA), flag, CHANGE);
// interrupt 0 digital pin 2 positive edge trigger
}
void loop() {
if (doprint = true){ //added this in hopes that it would keep printing but doesnt work
}
if (INTFLAG1 == 1) {
var_degrees = ((360 / 4096.00) * master_count);
//Serial.println(var_degrees);
//delay(500);
INTFLAG1 = 0; // clear flag
} // end if
stepper.moveTo(8000);
Serial.println(var_degrees);
// Run to target position with set speed and acceleration/deceleration:
stepper.runToPosition();
delay(1000);
// Move back to zero:
stepper.moveTo(0);
// stepper.runToPosition();
delay(1000);
} // end loop
void flag() {
INTFLAG1 = 1;
if (digitalRead(CHA) == HIGH) {
if (digitalRead(CHB) == LOW) {
master_count = master_count - 1; //COUNTER CLOCK WISE
}
else {
master_count = master_count + 1; //CW
}
}
else {
if (digitalRead(CHB) == LOW) {
master_count = master_count + 1; //CW
}
else {
master_count = master_count - 1 ; //CCW
}
}
}