Code below instructs the steppermotor to run for a given distance (input over serial monitor).
The input from the serial monitor is well received and parsed. For example f 1000 100 means forward, distance 1000, speed 100.
However the motor does not move. The if-loop where the motor is instructed to run loops forever and rvcdDistCopy is never reached.
What is wrong?
#include <AccelStepper.h>
#define MotorInterfaceType 4 // type 4 = unipolar motor controlled by 4 transistors ie ULN9003
uint32_t rcvdDistance, rcvdSpeed, rcvdAccel, rcvdDistCopy; //number of steps, speed, acceleration value from compute, character for commands:
// each character needs two variables: distance and speed. Example: s 2000 500 means 2000 steps at 500 steps/second
/* f = forward (CW) // needs steps and speed values
r = reverse (CCW) // needs steps and speed values
a = set acceleration // needs acceleration value
s = stop right now! // just the 's' is needed
*/
bool newData, runOk = true; // booleans for new data from serial, and run allowed flag
const byte numChars = 32;
char myData[numChars] = {0};
char rcvddChars[numChars], tempChars[numChars], rcvdCmd[numChars];
// Pins entered in sequence IN1-IN3-IN2-IN4 for proper step sequence
AccelStepper myStepper(MotorInterfaceType, 8, 9, 10, 11);
void setup() {
Serial.begin(9600);
delay(500);
Serial.println("DCCpp_turntable_controller_v1");
// set the maximum speed, acceleration factor,
// initial speed and the target position
myStepper.setMaxSpeed(500.0);
myStepper.setAcceleration(10.0);
myStepper.setSpeed(200);
myStepper.moveTo(2000);
// myStepper.disableOutputs();
}
void loop() {
// Change direction once the motor reaches target position
if (Serial.available() > 0) //if something comes
{
int i = i++;
Serial.print(" i = "); Serial.println(i);
char * strtokIndx; // this is used by strtok() as an index. It creates the variable
byte m = Serial.readBytesUntil('\n', myData, numChars);
myData[m] = '\0'; //null-byte
strtokIndx = strtok(myData, " "); // this continues where the previous call left off
// rcvdCmd = Serial.read(); // this will read the command character
strcpy (rcvdCmd, strtokIndx);
Serial.print("char = "); Serial.println(rcvdCmd);
strtokIndx = strtok(NULL, " "); // this continues where the previous call left off
uint32_t rcvdDistance = atol(strtokIndx);
Serial.print("long uint y1 = "); Serial.println(rcvdDistance);
strtokIndx = strtok(NULL, " "); // this continues where the previous call left off
uint32_t rcvdSpeed = atol(strtokIndx);
Serial.print("long uint y2 = "); Serial.println(rcvdSpeed);
// float y2 = atof(strtokIndx);
// Serial.print("float y2 = "); Serial.println(y2, 2);
rcvdDistCopy = rcvdDistance;
char cmd = rcvdCmd[0];
switch (cmd)
{
case 'f':
runOk = true;
Serial.print(" char case = "); Serial.println(rcvdCmd);
Serial.print(" fwd distance = "); Serial.println(rcvdDistance);
Serial.print(" fwd speed = "); Serial.println(rcvdSpeed);
// myStepper.move(rcvdDistance);
// myStepper.setMaxSpeed(rcvdSpeed);
// runOk = false;
break;
case'r':
runOk = true;
Serial.print(" char case = "); Serial.println(rcvdCmd);
// myStepper.move(-1 * rcvdDistance);
// myStepper.setMaxSpeed(rcvdSpeed);
// runOk = false;
break;
case 'a':
runOk = false; //disable running
Serial.print(" char case = "); Serial.println(rcvdCmd);
rcvdAccel = Serial.parseFloat(); //receive the acceleration from serial
myStepper.setAcceleration(rcvdAccel); //update the value of the variable
Serial.println(" Accel. updated "); //confirm update by message
break;
case 's':
runOk = false; //we still keep running disabled, since we just update a variable
Serial.print(" char case = "); Serial.println(rcvdCmd);
Serial.println(" STOP "); //print action
myStepper.setCurrentPosition(0); // reset position
myStepper.stop(); //stop motor
//myStepper.disableOutputs(); //disable power
break;
}
}
if (runOk == true)
{
Serial.print(" currentPosition = "); Serial.println(abs(myStepper.currentPosition()));
Serial.print(" received distance = "); Serial.println(rcvdDistCopy);
if (abs(myStepper.currentPosition()) < rcvdDistCopy) //abs() is needed because of the '<'
{
Serial.println(" stepper active");
// myStepper.enableOutputs(); //enable pins
// myStepper.setMaxSpeed(rcvdSpeed);
// myStepper.moveTo(rcvdDistCopy); //Set the target motor position (i.e. turn motor for 3 full revolutions)
// myStepper.runToPosition(); // Run the motor to the target position
// myStepper.move(rcvdDistance);
myStepper.run(); //step the motor (this will step the motor by 1 step at each loop)
}
else //program enters this part if the required distance is completed
{
Serial.println(" stepper stopped");
runOk = false; //disable running -> the program will not try to enter this if-else anymore
// myStepper.disableOutputs(); // disable power
Serial.print("POS: ");
Serial.println(myStepper.currentPosition()); // print pos -> this will show you the latest relative number of steps
myStepper.setCurrentPosition(0); //reset the position to zero
Serial.print("POS: ");
Serial.println(myStepper.currentPosition()); // print pos -> this will show you the latest relative number of steps; we check here if it is zero for real
}
}
}