Bluetooth connection using cnc shield and arduino uno

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);
  
}

what does that mean? what do you see?


side note

that's fast for software serial. stick to 9600

What happened (twice) was that it froze and then it went black and i couldn't close it as usual, I had to force close using the task manager :frowning:

What does that mean? Does the Arduino restart? Does it lock up? Do you see strange characters in serial monitor?

What is shown in serial monitor before the "crash"?

All of my HC05 modules default to 9600 baud for communication mode. Did you change that?

What is the Bluetooth device that is sending the data and to which the HC05 is paired? What app is running on the sender that sends the data.

The serial monitor doesn't show anything, it's just blank
I didn't change the baud, but I'm going to try now
I'm sending the data with the pc (windows 7 enterprise), but I'm not using any app, I'm trying to send it via using the serial monitor (I don't know if that is correct)

What got black ? Iā€™m still unclear what you are referring to

Sorry for my poor explanation. I mean that the interface where I write the code was just black, like instead of being white with the code and all, it was just black

The HC05 needs to be paired with another Bluetooth device. Is the PC that device?

There is an LED on the HC05 module. What is the flash pattern when you are sending and receiving data?

Yes, the pc is that device.

The problem with the led of the hc05 device is that it doesn't flash when I disconnect the board from the pc (leaving the power supply connected of course)

is that with IDE 2.0?

I'm using the 1.8.19 IDE because for the 2.0 I need Win10 and my computer has Win7

Are you particularly keen to write your own code for this? I ask because there is an open source program, GRBL, which runs on a Uno and can drive a CNC shield. It needs serial data, which can be from the standard serial monitor, to provide commands as g-code. However it is also possible to drive a a second serial port from a Bluetooth module. I use this in a 2-axis CNC box to drive my milling machine X axis and also a rotary axis; or a CNC coil winder. Commands come from a program called GRBL Controller that runs on my phone (or aa tablet). I am using an HC-06 module, at 115600 bps. If this is of interest I can try to see how I did it.

1 Like

besides the baud rate which seems a bit HIGH for software serial, nothing is fundamentally wrong in your arduino code and I've never seen (but I don't use Windows - I'm a Mac person) the IDE freezing because of what was happening on the Arduino

I'd be tempted to say that you have some sort of issue with the terminal window where you talk pair with the HC05 is faulty. Did you add a BT dongle to your PC or are you using the built-in BT capabilities of the PC?

I'm not keen on writing my own code to be honest. Which language uses this GRBL program? I'd really appreciate if you gave some info about this
Thank you very much!

I lowered the baud rate to 9600 and the problem persists.
For the BT I added a BT dongle to my PC since it didn't have any BT capabilities itself

are you using a third party terminal connected to the BT dongle then to send the commands or did you point the IDE at that Serial interface and opened a Serial monitor ?

The second thing that you said, I opened the serial monitor from my interface

so you are running 2 instances of the IDE. One is configured on a COM port to talk via USB with the Arduino and one is configured (with a fake arduino) to use the COM port where the dongle is attached.

is that correct?

I would try using a dedicated terminal for the BT communication, something like PuTTY, KiTTY, coolTerm, ... and not go through the IDE.

The thing is that I should be able to communicate via Bluetooth without having the Arduino attached with USB, right? So tried to unplug the Arduino from the computer and using only the COM port of the bluetooth device. And that is when it freezes

yes, you don't need to attach the arduino at all the PC.

I would really suggest you try with a real terminal application (they are free downloads) so that you don't depend on weird stuff the Arduino IDE might try to do

1 Like