Hey there,
I'm working on a project with steppers motors. I am using an Arduino UNO board and a CNC shield to connect the motors to the drivers. Right now I have the first code (code 1) that I have attached that allows me to write in the serial monitor the steps that I want my motors to move as follows: <xsteps,ysteps>. This code works perfectly for that purpose.
However, I have attached a HC05 bluetooth module because I want to be able to send this information via bluetooth and I can't seem to be able to modify my code for that purpose. The second code that I have attached is what I have tried. I based that code in info that I have read in other posts in this forum.
After loading this second code, when I try to send the info (using bluetooth) writing it in the serial monitor the arduino interface just crashes.
Thank you in advance for your help!
Code 1: this works perfectly when I enter manually the data in the serial monitor.
#include <AccelStepper.h>
const byte numChars = 32; //number of characters per piece of info
char receivedChars[numChars];
char tempChars[numChars]; //temporary array for use when parsing
float xsteps = 0.0;
float ysteps = 0.0;
boolean newdata = false;
//
//two stepper motors
#define xenable 8
#define xstep 2 //pin for step of x motor
#define xdir 5 //pin for dir of x motor
AccelStepper xmotor(1, xstep, xdir);
#define yenable 8
#define ystep 3 //pin for step of y motor
#define ydir 6 //pin for dir of y motor
AccelStepper ymotor(1, ystep, ydir);
//
void setup()
{
Serial.begin(9600);
Serial.println("input data");
pinMode(xenable, OUTPUT);
pinMode(yenable, OUTPUT);
xmotor.setEnablePin(xenable);
xmotor.setPinsInverted(false, false, true);
xmotor.setAcceleration(1);
xmotor.setMaxSpeed(50);
//motorX.setSpeed(100);
xmotor.enableOutputs();
ymotor.setEnablePin(yenable);
ymotor.setPinsInverted(false, false, true);
ymotor.setAcceleration(1);
ymotor.setMaxSpeed(50);
//motorY.setSpeed(100);
ymotor.enableOutputs();
}
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;
}
xmotor.moveTo(xsteps);
ymotor.moveTo(ysteps);
xmotor.run();
ymotor.run();
}
//======= define the used functions======
//receiving (and stop receiving) the data
void receive(){
static boolean rip = false; //receive in process
static byte ndx = 0;
char startmarker = '<'; //character that will mark the start of the reading by the serial monitor (i need to send the characters like that from labview)
char endmarker = '>'; //the same but with the 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 -> depending in how many characters and what kind of characters we are sending
//this function will change
//we are getting in principle 4 sets of steps data (although we will try first with two) that are supposed to be float type
//
void parsedata(){
char*strtokindx; //use as an index for the strtok funtion
strtokindx = strtok(tempChars,","); //get the first part
xsteps = atof(strtokindx); //converts this to a float
// Serial.print(xsteps);
strtokindx = strtok(NULL, ","); //continues where the previous call left
ysteps = atof(strtokindx);
// Serial.print(ysteps);
}
//show what we have received (that serves as a confirmation that we did receive it or at least i think and hope so)
void showpd(){ //show parsed data
Serial.print("steps X motor ");
Serial.print(xsteps);
Serial.print("; steps Y motor ");
Serial.print(ysteps);
}
Code 2: it crashes my arduino interface when I try to write in the serial monitor
#include <SoftwareSerial.h>
#include <AccelStepper.h>
//Bluetooth serial communication
SoftwareSerial bt(A0,A1);
const byte numChars = 32; //number of characters per piece of info
char receivedChars[numChars];
char tempChars[numChars]; //temporary array for use when parsing
float xsteps = 0.0;
float ysteps = 0.0;
boolean newdata = false;
//
//two stepper motors
#define xenable 8
#define xstep 2 //pin for step of x motor
#define xdir 5 //pin for dir of x motor
AccelStepper xmotor(1, xstep, xdir);
#define yenable 8
#define ystep 3 //pin for step of y motor
#define ydir 6 //pin for dir of y motor
AccelStepper ymotor(1, ystep, ydir);
//
void setup()
{
//bluetooth
Serial.begin(9600);
bt.begin(38400);
Serial.println("input data");
pinMode(xenable, OUTPUT);
pinMode(yenable, OUTPUT);
xmotor.setEnablePin(xenable);
xmotor.setPinsInverted(false, false, true);
xmotor.setAcceleration(1);
xmotor.setMaxSpeed(50);
//motorX.setSpeed(100);
xmotor.enableOutputs();
ymotor.setEnablePin(yenable);
ymotor.setPinsInverted(false, false, true);
ymotor.setAcceleration(1);
ymotor.setMaxSpeed(50);
//motorY.setSpeed(100);
ymotor.enableOutputs();
}
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;
}
xmotor.moveTo(xsteps);
ymotor.moveTo(ysteps);
xmotor.run();
ymotor.run();
}
//======= define the used functions======
//receiving (and stop receiving) the data
void receive(){
static boolean rip = false; //receive in process
static byte ndx = 0;
char startmarker = '<'; //character that will mark the start of the reading by the serial monitor (i need to send the characters like that from labview)
char endmarker = '>'; //the same but with the end of the reading
char rc;
while (bt.available()>0 && newdata == false){ //check to see if anything is available in the serial receive buffer
rc = bt.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 -> depending in how many characters and what kind of characters we are sending
//this function will change
//we are getting in principle 4 sets of steps data (although we will try first with two) that are supposed to be float type
//
void parsedata(){
char*strtokindx; //use as an index for the strtok funtion
strtokindx = strtok(tempChars,","); //get the first part
xsteps = atof(strtokindx); //converts this to a float
// Serial.print(xsteps);
strtokindx = strtok(NULL, ","); //continues where the previous call left
ysteps = atof(strtokindx);
// Serial.print(ysteps);
}
//show what we have received (that serves as a confirmation that we did receive it or at least i think and hope so)
void showpd(){ //show parsed data
Serial.print("steps X motor ");
Serial.print(xsteps);
Serial.print("; steps Y motor ");
Serial.print(ysteps);
}