Hey there!
I wrote a program on Arduino to control stepper motors using the EasyDriver library and serial communication. It is all working, but I have an issue when I try to send more than 32767 steps to the motors. When I do that, the motors will turn in the opposite direction of the one it should, and if the number is too big, it doesn't move all the steps either. I understand that this is a problem of the type of variable that I'm using. I was using the type float at the beginning, and then I tried to change it to int, and then to long int. But the result was the same.
My set up:
- Win11
- Arduino IDE 2.0.5
- Arduino UNO
Here is the code with the last modification (try to use long int):
//libraries for drivers and serial communication (sc)
#include <AH_EasyDriver.h>
#include <SoftwareSerial.h>
#include <stdio.h>
#include <stdlib.h>
//sending the characters via sc
const byte numChars = 32; //number of characters per piece of info
char receivedChars[numChars];
char tempChars[numChars]; //temporary array for use when parsing
long m1steps = 0.0;
long m2steps = 0.0;
long m3steps = 0.0;
long m4steps = 0.0;
float speedrpm = 170;
boolean newdata = false;
//AH_EasyDriver(int RES, int DIR, int STEP, int MS1, int MS2, int SLP, int ENABLE, int RST);
AH_EasyDriver m1stepper(200,6,7,3,4,2); //initialize with the basics functions
//MS1, MS2 and SLP control microstepping
//RES are steps per revolution*
AH_EasyDriver m2stepper(200,8,9,3,4,2);
AH_EasyDriver m3stepper(200,10,11,3,4,2);
AH_EasyDriver m4stepper(200,12,13,3,4,2);
//add the rest of the drivers (when i have them)
void setup() {
Serial.begin(9600); //initialize sc
Serial.println("Arduino is ready");
m1stepper.resetDriver();
m1stepper.enableDriver();
m1stepper.setMicrostepping(0); // 0 -> Full Step
// 1 -> 1/2 microstepping
// 2 -> 1/4 microstepping
// 3 -> 1/8 microstepping
m1stepper.setSpeedRPM(speedrpm); // set speed in RPM, rotations per minute
// m1stepper.setSpeedHz(100);
m2stepper.resetDriver();
m2stepper.enableDriver();
m2stepper.setMicrostepping(0);
m2stepper.setSpeedRPM(speedrpm);
m3stepper.resetDriver();
m3stepper.enableDriver();
m3stepper.setMicrostepping(0);
m3stepper.setSpeedRPM(speedrpm);
m4stepper.resetDriver();
m4stepper.enableDriver();
m4stepper.setMicrostepping(0);
m4stepper.setSpeedRPM(speedrpm);
}
void loop() {
receive();
if (newdata == true) {
strcpy(tempChars, receivedChars); //this copies the values in recerivedchars into tempchars (string) for a temporary copy
parsedata();
showpd(); //show parsed data
newdata = false;
m1stepper.sleepOFF();
m1stepper.move(-m1steps); //movement of the motor1
m1stepper.sleepON();
m2stepper.sleepOFF();
m2stepper.move(-m2steps);
m2stepper.sleepON();
m3stepper.sleepOFF();
m3stepper.move(-m3steps);
m3stepper.sleepON();
m4stepper.sleepOFF();
m4stepper.move(-m4steps);
m4stepper.sleepON();
}
}
//=======functions======
//receiving (and stop receiving) the data
//the data needs to be sent as <m1steps,m2steps,m3steps,m4steps>
void receive() {
static boolean rip = false; //receive in process
static byte ndx = 0;
char startmarker = '<'; //start of the reading
char endmarker = '>'; //end of the reading
char rc;
while (Serial.available() > 0 && newdata == false) { //check to see if anything is available in the serial receive buffer
rc = Serial.read(); // we add to the array the characters until the endmarker
if (rip == true) {
if (rc != endmarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
} else {
receivedChars[ndx] = '\0'; //terminate the string
rip = false;
ndx = 0;
newdata = true;
}
} else if (rc == startmarker) {
rip = true;
}
}
}
//split the data into the different parts
//four sets of data in float
void parsedata() {
char* strtokindx; //use as an index for the strtok funtion
strtokindx = strtok(tempChars, ","); //get the first part of the string
m1steps = atol(strtokindx); //converts this to a float
strtokindx = strtok(NULL, ","); //continues where the previous call left
m2steps = atol(strtokindx);
strtokindx = strtok(NULL, ","); //continues where the previous call left
m3steps = atol(strtokindx);
strtokindx = strtok(NULL, ","); //continues where the previous call left
m4steps = atol(strtokindx);
}
//show what we have received the characters (confirmation of arrival and movement of the motors)
void showpd() { //show parsed data
Serial.print("m1steps: ");
Serial.print( m1steps);
Serial.print("; m2steps: ");
Serial.print( m2steps);
Serial.print("; m3steps: ");
Serial.print( m3steps);
Serial.print("; m4steps: ");
Serial.print( m4steps);
Serial.println();
}
Thanks a lot!