I should begin by saying I am not a programmer, do not really know much about programming, and am programming an Arduino Uno for the first time. I am using a Nema 23 motor (23HS22-2804S) with a microstep driver (ST-M5045). Within the following code is the description of what I need the program to do:
#include <AccelStepper.h>
/* Using a stepper motor,
rotate a vibrational model using gears interconnected by springs
in such a way as to induce torsional vibration, which should be visible by comparing
movement of juxtaposed rotating gears with markers painted on. */
AccelStepper stepper(AccelStepper::FULL4WIRE, 2, 6, 4, 8);
int Microstepping = 8; /* Microstepping factor, as set on the ST-M5045 control board.
Multiplies the number of steps per revolution on the stepper motor.
Reference controller manual for adjustments to this number. */
int Steps = 200; /* Number of steps on your stepper motor.*/
int SPS;
int Amp;
int Freq;
// int SC;
int n = 2;
int i;
int totalSteps = Microstepping * Steps;
/* It should be noted that the Microstepping and Steps factors are not necessary, and adjusting the microstepping factor
* on the fly will not break the code. However, be aware that changing the microstepping factor by more than one interval
* at a time can cause the motor to not keep up with the sudden changes, i.e. losing torque or vibrating/stuttering. */
void setup() { // THIS RUNS ONCE //
// read potentiometers; raw inputs from 0 to 1023 //
SPS = analogRead(0)*0.733; // MAX RPM ~28, max speed ~750 steps per sec (speeds above 1000 steps per second are unreliable,
// according to AccelStepper documentation)//
Amp = analogRead(1)/20; // MAX AMP ~51//
Freq = analogRead(2)/170; // MAX FREQ ~6 Hz//
stepper.setMaxSpeed(SPS);
stepper.setAcceleration(SPS/1.5);
int nextStop = totalSteps/Freq;
stepper.moveTo(nextStop);
stepper.run();
}
void loop() {
int nextStop = totalSteps/Freq;
// reaches here, doesn't execute if loop.
if(stepper.distanceToGo()== 0){
SPS = analogRead(0)*0.733;
Amp = analogRead(1)/20;
Freq = analogRead(2)/170;
i = SPS - Amp;
if(i <=0)
i = 1;
stepper.setSpeed(i);
stepper.moveTo(nextStop*n);
stepper.run();
}
n++;
}
When I use Serial.println() to check where the program is working and where it isn't, I see that it completes the setup loop without ever moving, and in slightly different versions of the same code, it has entered the if() statement and executed it (also without moving.)
An earlier version of the program runs and executes all statements, but only moves a maximum of about 20 steps per second, without the fluctuations in speed I call for, and doesn't respond in real time to any input from the speed potentiometer that puts the speed above 20.
Ideally, the program would run the demo at full speed, and freq times per second, it would slow down just enough to be noticeable as a torsional vibration.
I have tried running the program with and without the run() lines, and I have tried using runToNewPosition() in attempts to get the program to stop and properly execute the movement.
I made the setup loop as close as possible to the setup loop in bounce.pde, the example program in the AccelStepper documentation which moves the stepper motor between two points repeatedly, accelerating and decelerating before changing directions. The reason I did this is because I know that the setup loop in bounce.pde works, and so I would feel more confident that my setup loop is not the root problem.
Any help you can offer me is appreciated! Thanks for your time.