Hello and thank you in advance for the advice!
I have been out of the game for a while and have spent a few days beating my head against the wall with this project. I have tried a few different methods from other threads found in the search with no luck.
I am trying to control two steppers with one analog joystick. The joystick will control direction and speed. The farther the stick is pressed the faster the stepper will go. The Homing Function at the end of my code works really great and moves at the speed specified. When I try to move the motors with the joystick they step extremely slow, 1 step at a time.
Here is my 1st attempt at using Fritzing with my Arduino Uno R3, DRV8825 Drivers powered with external supply and Vref has been properly set for both.
Here is my Code: I am currently only trying to get 1 axis to work a expected.
#include <AccelStepper.h>
#include <MultiStepper.h>
// Define motor interface type
#define motorInterfaceType 1
// PAN STEPPER PINS & HOMEING INPUT
const int PanDir = 5;
const int PanStep = 2;
const int PanHome = 9;
// SLIDE STEPPER PINS & HOMING INPUT
const int SliDir = 6;
const int SliStep = 3;
const int SlideHome = 10;
// USED FOR HOMING
int initial_homing = 1;
//ANALOG JOYSTICK PINS
const int Xpin = A0;
const int Ypin = A1;
int xval;
int yval;
int xspeed;
int yspeed;
//DEFINE STEPPERS
AccelStepper Pan(motorInterfaceType, PanStep, PanDir);
AccelStepper Slide(motorInterfaceType, SliStep, SliDir);
MultiStepper steppers;
void setup()
{
Serial.begin(9600);
// SET PULL UP INPUTS
pinMode(PanHome, INPUT_PULLUP);
pinMode(SlideHome, INPUT_PULLUP);
//ENABLE STEPPER DRIVER VIA PIN 8
Pan.setEnablePin(8); //Sets Enable/Disable Pin
Pan.disableOutputs(); //Drives Pin LOW to enable Steppers
//CREATING PAN AND SLIDE IN STEPPERS
steppers.addStepper(Slide);
steppers.addStepper(Pan);
//SET MAX AND SPEED
Pan.setMaxSpeed(300);
Pan.setSpeed(200);
Slide.setMaxSpeed(200);
Slide.setSpeed(300);
//RUN HOMING FUNCTION TO START
Home();
}
void loop() {
//Update Analog Values
xval = analogRead(Xpin);
delay(10);
yval = analogRead(Ypin);
delay(10);
//Set Speed based on how far joystick is pushed
xspeed = abs(map(xval, 523, 1023, 0, 300));
yspeed = abs(map(yval, 523, 1023, 0, 300));
while (analogRead(Xpin) > 550) {
xval = analogRead(Xpin);
xspeed = abs(map(xval, 523, 1023, 0, 300));
Pan.moveTo(1);
Pan.runSpeed();
}
while (analogRead(Xpin) < 450) {
xval = analogRead(Xpin);
xspeed = abs(map(xval, 523, 1023, 0, 300));
Pan.moveTo(-1);
Pan.runSpeed();
}
}
//HOME FUNCTION FOR PAN AXIS
int Home() {
Pan.setMaxSpeed(600);
Pan.setSpeed(600);
Serial.println("Homing Pan");
//Finding Home Fast
while (digitalRead(PanHome)) {
Pan.move(initial_homing);
initial_homing--;
Pan.run();
}
Pan.setCurrentPosition(0);
delay(1000);
//Moving Off Home Slow
initial_homing = 0;
Pan.setMaxSpeed(100);
Pan.setSpeed(100);
while (!digitalRead(PanHome)) {
Pan.move(initial_homing);
initial_homing++;
Pan.run();
}
//Setting Home Position
Pan.setCurrentPosition(0);
delay(100);
initial_homing = 1;
//Finding Home Fast
Slide.setMaxSpeed(600);
Slide.setSpeed(600);
Serial.println("Homing Slide");
while (digitalRead(SlideHome)) {
Slide.move(initial_homing);
initial_homing--;
Slide.run();
}
Slide.setCurrentPosition(0);
delay(1000);
//Moving Off Home Slow
Slide.setMaxSpeed(100);
Slide.setSpeed(100);
initial_homing = 0;
while (!digitalRead(SlideHome)) {
Slide.move(initial_homing);
initial_homing++;
Slide.run();
}
Slide.setCurrentPosition(0);
delay(1000);
}