Fixing Shaking from motors when Controlling Steppers with a Joystick

Hello. This is my first post here so please let me know if it should be moved elsewhere.

Essentially, I have a project where I am controlling four Nema steppers with a Arduino CNC shield and a joystick. I am able to get all the steppers running with a code that just cycles through each axis and runs it through a certain amount of steps. I have been able to also get the same result with using the joystick. However, the motors run much slower and create a lot of vibration and noise. I have checked the pots on each driver and made sure those are at the correct voltage.

Any help is greatly appreciated.

Here is the code I have been using for the joystick control:

# include <Servo.h>
//Steppers
const int stepPinx = 2; //X.STEP
const int dirPinx = 5; // X.DIR
const int stepPiny = 3; //Y.STEP
const int dirPiny = 6; //Y.DIR
const int stepPinz = 4; //Z.STEP
const int dirPinz = 7; //Z.STEP
int SMSpeed = 500; // Stepper Motor Speed 
// joystick
const int joystickSwitchPin = 13;  // Switch pin of the joystick
int vrx = A0; 
int vry = A1; 
int vrx_data = 0; 
int vry_data = 0; 
int x = 0; 
int y = 0;
//Servo
const int servoMotorPin = 11;
Servo servoMotor;
int servoPosition = 0;          // Current position of the servo motor
int originalServoPosition = 0;  // Original position of the servo motor
bool isServoAtOriginalPosition = true;

 void setup() {
 // Sets the dir and step pins as Outputs
 Serial.begin(9600); 
 pinMode(stepPinx,OUTPUT); 
 pinMode(dirPinx,OUTPUT);
 pinMode(stepPiny,OUTPUT); 
 pinMode(dirPiny,OUTPUT);
 pinMode(stepPinz,OUTPUT); 
 pinMode(dirPinz,OUTPUT);
 pinMode(vrx , INPUT); 
 pinMode(vry, INPUT); 
 servoMotor.attach(servoMotorPin);
 pinMode(joystickSwitchPin, INPUT_PULLUP);
 }

 
void loop() {
joystick();
}

void joystick(){
vrx_data = analogRead(vrx);
Serial.print("Vrx:"); 
Serial.println(vrx_data); 

vry_data = analogRead(vry); 
Serial.print("Vry:"); 
Serial.println(vry_data); 


// to stop the stepper motor
if ( (vrx_data > 490)  &&   (vrx_data < 510)   )
{

;
  
}

if ( (vry_data > 490)  &&   (vry_data < 510)   )
{

;
  
}

if ( vrx_data > 700  )
{

digitalWrite(dirPinx,HIGH);
digitalWrite(dirPinz,LOW);
x = x + 1; 
 digitalWrite(stepPinx,HIGH);
 digitalWrite(stepPinz,HIGH); 
 delayMicroseconds(SMSpeed); 
 digitalWrite(stepPinx,LOW); 
 digitalWrite(stepPinz,LOW);
 delayMicroseconds(SMSpeed); 
  
}

if ( vry_data > 700  )
{

digitalWrite(dirPiny,HIGH);
y = y + 1; 
 digitalWrite(stepPiny,HIGH); 
 delayMicroseconds(SMSpeed); 
 digitalWrite(stepPiny,LOW); 
 delayMicroseconds(SMSpeed); 
  
}


if ( vrx_data < 300   )
{
digitalWrite(dirPinx,LOW);
digitalWrite(dirPinz,HIGH);
x = x - 1; 

 digitalWrite(stepPinx,HIGH); 
 digitalWrite(stepPinz,HIGH);
 delayMicroseconds(SMSpeed); 
 digitalWrite(stepPinx,LOW); 
 digitalWrite(stepPinz,LOW);
 delayMicroseconds(SMSpeed);  
}

if ( vry_data < 300   )
{
digitalWrite(dirPiny,LOW);
y = y - 1; 

 digitalWrite(stepPiny,HIGH); 
 delayMicroseconds(SMSpeed); 
 digitalWrite(stepPiny,LOW); 
 delayMicroseconds(SMSpeed);  
}
}

I haven't gone over your code in-depth, but at the very least you could boost your Serial speed to 115200 or more. 9600 is very slow and you have Serial.print() in each loop of your code that will be slowing everything down while it writes to the serial monitor. Better yet, if everything is mostly working to your satisfaction, remove them altogether. For bonus performance, you could try and rewrite without the delayMicroseconds() using "without delay" techniques.

Getting rid of all the serial monitor stuff really helped. I think with the input from the joystick was slowing it down a lot. Thank you!

Hi,
You might also like to try reading each analog input twice to ensure the ADC has time to read each input.

There is only one ADC and it switches from input to input, it takes time for the input to change to the new voltage due to a capacitor on the input to the ADC.

Tom.. :smiley: :+1: :coffee: :australia:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.