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);
}
}