I'm using an Arduino Uno to control a stepper motor for material deposition in a robot-assisted 3D printing process.
It's a Geared Stepper motor ratio of 5.18:1 and I control it through an external signal (sending from robot controller) to turn the motor on/off.
But It does not work as I expect. Here are the problems:
1- Sometimes the signal is high but the motor is off and vise versa. I'm wondering if it might be because of the "CHANGE" state interrupt or because the input signal changes too fast, so the microcontroller cannot adjust? (Honestly, it's not that fast, 1-second delay between on/off, but I was suspicious)
2-Motor speed is controlled by a potentiometer. but the change of the speed changes suddenly when I spin the potentiometer in its 1/4 to the end and the motor spins fast suddenly; it's not smooth at the last quarter!
3-I was using a shield and a grove LCD. It dramatically lowers the max speed of the stepper motor! So I need to remove both LCD and shield! I mean, the same code works better without a shield! is that normal?
4-I have tried AccelStepper, but for some reason, AccelStepper doesn't work well for this situation.
I really appreciate it if anyone can help me with these problems.
Here is the code:
#define startSwitch 3 //push button to start or stop motor
#define reverseSwitch 2 //push button for reverse pin
#define driverPUL 9 //PUL pin
#define driverDIR 8 //DIR pin
#define speedPin A3 //potentiometer
// Variables
int pd = 500 ; // pulse delay period
boolean onoroff = digitalRead(startSwitch) ; //On Off Motor
boolean setdir = LOW ; //set Direction
void setup (){
pinMode (driverPUL, OUTPUT);
pinMode (driverDIR, OUTPUT);
pinMode (startSwitch, INPUT);
attachInterrupt(digitalPinToInterrupt(startSwitch), startMotor, CHANGE);
attachInterrupt(digitalPinToInterrupt(reverseSwitch), revMotor, FALLING);
}
void loop() {
if (onoroff == LOW) { //turn the motor OFF
digitalWrite(driverPUL, LOW);
}
else if (onoroff == HIGH) { //turn th e motor ON
pd = analogRead(speedPin);
digitalWrite(driverDIR,setdir);
digitalWrite(driverPUL,HIGH);
delayMicroseconds(pd);
digitalWrite(driverPUL,LOW);
delayMicroseconds(pd);
}
}
//Interupt Handler
void revMotor () {
setdir = !setdir;
}
void startMotor () {
onoroff = !onoroff ;
}