I am trying to control a 48V DC motor with an encoder handwheel, similar to what you would find on a CNC, and a joystick. The goal being the handwheel makes small "steps" and the joystick moves the motor rapidly. My entire code will be to control two motors X and Y axis but for simplicity I am only showing X axis control. My problem is when I use the handwheel the motor is "stepping" but it is jerky at times depending on how quickly the wheel is spun. I have looked at various code and examples using encoders but have not found anything similar to this application. I think an option may be to put an encoder on the motor but, I don't want to go down that rabbit hole unless I have no other option.
`#include <Encoder.h>
Encoder myEncX(2,3);
//Inputs & Outputs
int XEnable=6; //X Motor PWM
int XDirA=4; //X Motor direction A
int XDirB=5; //X Motor direction B
int JoyBut=12; //Joy Stick Button to enable Joy Stick
const int X_pin = A0; // analog pin connected to X input
const int Y_pin = A1; // analog pin connected to Y input
//Handwheel variables
long HWXoldPos = -999;
long HWXnewPos;
int JOG=125; //Speed of PWM when using handwheel
int X_data;
int Y_data;
//Bool
bool JoyMove = false;
void setup()
{
pinMode(JoyBut, INPUT_PULLUP);
pinMode(XEnable, OUTPUT);
pinMode(XDirA, OUTPUT);
pinMode(XDirB, OUTPUT);
pinMode(X_pin, INPUT);
pinMode(Y_pin, INPUT);
Serial.begin(9600);
}
void loop()
{
//X axis Handwheel Operation
long HWXnewPos = myEncX.read();
if (HWXnewPos != HWXoldPos)
{
Serial.println(HWXnewPos);
if (HWXnewPos < HWXoldPos)
{
analogWrite(XEnable,JOG);
digitalWrite(XDirA,LOW);
digitalWrite(XDirB,HIGH);
Serial.println("On XDirB");
delay(1);
digitalWrite(XDirB,LOW);
}
if (HWXnewPos > HWXoldPos)
{
analogWrite(XEnable,JOG);
digitalWrite(XDirA,HIGH);
digitalWrite(XDirB,LOW);
Serial.println("On XDirA");
delay(1);
digitalWrite(XDirA,LOW);
}
HWXoldPos = HWXnewPos;
}
//JoyStick Operation
if (digitalRead(JoyBut) == LOW)
{
JoyMove = true;
}
if (digitalRead(JoyBut) == HIGH)
{
JoyMove = false;
analogWrite(XEnable,0);
digitalWrite(XDirA,LOW);
digitalWrite(XDirB,LOW);
}
if (JoyMove == true) //Joy stick will not opperate unless button on top is pressed/held
{
Serial.println("JoyBut");
X_data = analogRead(X_pin);
Y_data = analogRead(Y_pin);
//Backward Movement
if(X_data <= 500)
{
int X_speedB = map(X_data, 500, 0, 0, 255);
Serial.print("X B ");
Serial.println(X_data);
analogWrite(XEnable,X_speedB);
digitalWrite(XDirA,LOW);
digitalWrite(XDirB,HIGH);
}
//Forward Movement
else if(X_data >= 590)
{
int X_speedF = map(X_data, 550, 1024, 0, 255);
Serial.print("X F ");
Serial.println(X_data);
analogWrite(XEnable,X_speedF);
digitalWrite(XDirA,HIGH);
digitalWrite(XDirB,LOW);
}
//Don't Move
else
{
analogWrite(XEnable,0);
digitalWrite(XDirA,LOW);
digitalWrite(XDirB,LOW);
}
}
}
`