DC and servo control with bluetooth


I have dug through just about every corner of the internet trying to get a good code for controlling both a (small) DC motor and a servo motor through BlueTooth with my smartphone, using the HC-05 module. However, since I’ve modified and compiled the code myself, I would really appreciate it if anyone wants to just read through it and find any of the tiniest mistakes (or bigger ones). Please let me know! I realize I could probably get the same results with code that’s a bit more efficient and/or elegant, but as long as it works, I’m happy. I will use two separate apps for the DC (RoboRemo) and the servo (my own via MIT).

Both original, individual codes have been attached (the yellow part is for the servo and the white part below for the DC).

Here’s the code:

//for the DC motor (with adjustable speed):
#define Bluetooth Serial
int motorSpeedPin = 9;
char cmd[100];
int cmdIndex

//for the servo motor:
#include <SoftwareSerial.h>
#include <Servo.h>
Servo myservo;
int bluetoothTx = 0;
int bluetoothRx = 1;
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

//'void exeCmd(){...}' is only for the DC motor (this presumably isn't an issue(?)):
void exeCmd() {
  if(cmd[0]=='s' &&
  cmd[1]=='p' &&
  cmd[2]=='e' &&
  cmd[3]=='e' &&
  cmd[4]=='d' &&
  cmd[5]==' ') {
   int val = 0;
   for(int i=6; cmd[i]!=0; i++)
   val = val*10 + (cmd[i]-'0');

void setup()
  // for the servo:
  // for the DC:
  pinMode(motorSpeedPin, OUTPUT);
  analogWrite(motorSpeedPin, 0);
  cmdIndex = 0;
  //for the servo:

void loop()
  //for the DC:
    char c = (char)Bluetooth.read();
    cmd[cmdIndex] = 0;
    cmdIndex = 0;
    } else {
      cmd[cmdIndex] = c;
      if(cmdIndex<99) cmdIndex++;
  // for the servo:
  if(bluetooth.available()> 0 )
    int servopos = bluetooth.read();


This may be slightly too much code - although I hope not -, so I’ll list my biggest concerns below:

  1. The code that I found for the DC used ‘Bluetooth’ (with a capital B) and the code for the servo used ‘bluetooth’ (with a lower case b). Also, the baud rate for the DC is set at 115200 and for the servo it is set at 9600. I didn’t mess around with either of the two differences, though. Is it still possible to send signals through the HC-05 without interference between the signals that are meant for either the DC or the servo? Even if the code for the DC and the servo uses the phrase ‘if(Bluetooth.available()){…}’ and ‘if(bluetooth.available()>0){…}’, respectively? And should I take down the DC baud rate to the same value as the servo (9600)?

  2. The overall combining of the two codes is also something that worries me. Have I made any mistake whilst doing this? Please let me know!

Thank you very much in advance. Even if it’s flawless, let me know: better safe than sorry!

code arduino.doc (95.8 KB)

int motorSpeedPin = 9;

Does this help?

It does, thanks. I'll be sure to choose another PWM-pin for that wire.

Other than that, are there any problems, abnormalities or strange things with the code?