Help required please - Arduino Leonardo - servo movement via bluetooth

Hi guys,

I have researched best I can but am not experienced in Arduino coding enough to be able to find the answer to my problem.

I am building a robotic arm and need to drive with 4 servos.

I am using an Arduino Leonardo with HS-06 bluetooth and will be using an Android app via phone / tablet to make the axis move.

I am following this instructable:

I have been reading that for the Leonardo, you need to make modifications to code to reflect “Serial1” but I don’t know what to change in the code I have downloaded to get things working.

The TX and RX pins from the bluetooth adapter connect to arduino pins as per the code, not the designated TX and RX pins.

The code I am using is:

#include <SoftwareSerial.h>

#include <Servo.h>
Servo myservo1, myservo2, myservo3, myservo4;

int bluetoothTx = 6;
int bluetoothRx = 7;

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

void setup()
{
myservo1.attach(8);
myservo2.attach(9);
myservo3.attach(10);
myservo4.attach(11);
//Setup usb serial connection to computer
Serial.begin(9600);

//Setup Bluetooth serial connection to android
bluetooth.begin(9600);
}

void loop()
{
//Read from bluetooth and write to usb serial
if(bluetooth.available()>= 2 )
{
unsigned int servopos = bluetooth.read();
unsigned int servopos1 = bluetooth.read();
unsigned int realservo = (servopos1 *256) + servopos;
Serial.println(realservo);

if (realservo >= 1000 && realservo <1180){
int servo1 = realservo;
servo1 = map(servo1, 1000,1180,0,180);
myservo1.write(servo1);
Serial.println(“servo 1 ON”);
delay(10);

}

if (realservo >=2000 && realservo <2180){
int servo2 = realservo;
servo2 = map(servo2,2000,2180,0,180);
myservo2.write(servo2);
Serial.println(“servo 2 On”);
delay(10);

}

if (realservo >=3000 && realservo < 3180){
int servo3 = realservo;
servo3 = map(servo3, 3000, 3180,0,180);
myservo3.write(servo3);
Serial.println(“servo 3 On”);
delay(10);

}

if (realservo >=4000 && realservo < 4180){
int servo4 = realservo;
servo4 = map(servo4, 4000, 4180,0,180);
myservo4.write(servo4);
Serial.println(“servo 4 On”);
delay(10);

}

}

}

The bluetooth adapter will connect to the app on my phone so I don’t believe this is the issue. I do not get any led activity for TX and RX on the Leonardo board when I move the sliders for the servos in the Android app that I am using.

I would really appreciate any assistance to get this working.

Cheers,

Steve

stevo1957:
I have been reading that for the Leonardo, you need to make modifications to code to reflect "Serial1" but I don't know what to change in the code I have downloaded to get things working.

On a Leonardo, Serial refers to the USB comms, e.g. the serial monitor, and you use Serial1 to talk to a device connected to hardware serial pins 0,1. I won't ask why, but you are using Bluetooth with software serial on pins 6,7. so the matter of Serial1 is irrelevant.

Since phone sees bluetooth, check bluetooth signal wiring Tx>Rx and Rx>Tx.

The first modification would be to get rid of the smiley :smiley: Please read How to use this forum - please read., specifically point 7 how to post code.

Led activity for RX and TX only happens when communicating with the PC, not with the bluetooth adapter.

Native USB devices (like the Leonardo), need an additional statement in e.g. setup if you want to use Serial for printing to PC.

Serial.begin(9600);
while(!Serial);

Without that, data will not go out.

If the leds are the only problem that you have, this should be sufficient. There is a catch though; disconnecting the USB port can cause your Leonardo to become unresponsive as the Serial becomes blocking.

You can use availableForWrite() to prevent that from happening; this method gives you how many bytes you can still transmit before the communication will block. E.g.

if(Serial.availableForWrite() > 16)
{
  Serial.println("servo 4 On");
}

The text is 10 characters and you need to add 2 characters for the carriage return and linefeed that println adds. That makes 12 characters total so 16 is a safe value.

You need to do this for all Serial statements in your code that result in sending data (print, println, write).

Oh, forgot that if you use while(!Serial), you need to be connected to a PC. You can work around that by only checking for e.g. 5 seconds

Serial.begin(9600);
while(!Serial)
{
  if(millis() >5000)
  {
    break;
  }
}

I have been reading that for the Leonardo, you need to make modifications to code to reflect "Serial1" but I don't know what to change in the code I have downloaded to get things working.

To change your code to make use of RX1 and TX1 for bluetooth
1)
Hook the bluetooth module up to pins 0 and 1.
2)
Get rid of anything before setup that refers to SoftwareSerial / bluetooth.
3)
Replace all remaining bluetooth by Serial1. E.g. bluetooth.begin -> Serial1.begin

And now it's time that you start studying the stuff and figure out how it works. Too many people are too eager to start with something that provides some functionality but don't understand anything about thehardware and/or software. You need to start simple and learn how it all works; it's part of what Arduino stands for.

On request (via PM) the modificationsm to use HardwareSerial instead of SoftwareSerial.

#include <Servo.h>
Servo myservo1, myservo2, myservo3, myservo4;

void setup()
{
  // flag to indicate if a PC is connected; assume connected till proven differently
  bool isConnected = true;

  myservo1.attach(8);
  myservo2.attach(9);
  myservo3.attach(10);
  myservo4.attach(11);


  // use builtin led for visual indication
  pinMode(LED_BUILTIN, OUTPUT);

  //Setup usb serial connection to computer
  Serial.begin(9600);
  // check connection; needed for boards with native USB (e.g. Leonardo)
  while (!Serial)
  {
    // toggle the built in led
    digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));

    // wait a bit
    delay(500);

    // check if 5 seconds have passed
    if (millis() > 5000)
    {
      // after time-out, indicate not connected
      isConnected = false;
      break;
    }
  }

  // visual indication of connected / not connected
  if (isConnected == true)
  {
    digitalWrite(LED_BUILTIN, HIGH);
  }
  else
  {
    digitalWrite(LED_BUILTIN, LOW);
  }

  //Setup Serial1 serial connection to android
  Serial1.begin(9600);
}

void loop()
{
  //Read from Serial1 and write to usb serial
  if (Serial1.available() >= 2 )
  {
    unsigned int servopos = Serial1.read();
    unsigned int servopos1 = Serial1.read();
    unsigned int realservo = (servopos1 * 256) + servopos;
    if (Serial.availableForWrite() > 16)
    {
      Serial.println(realservo);
    }

    if (realservo >= 1000 && realservo < 1180)
    {
      int servo1 = realservo;
      servo1 = map(servo1, 1000, 1180, 0, 180);
      myservo1.write(servo1);
      if (Serial.availableForWrite() > 16)
      {
        Serial.println("servo 1 ON");
      }
      delay(10);
    }

    if (realservo >= 2000 && realservo < 2180)
    {
      int servo2 = realservo;
      servo2 = map(servo2, 2000, 2180, 0, 180);
      myservo2.write(servo2);
      if (Serial.availableForWrite() > 16)
      {
        Serial.println("servo 2 On");
      }
      delay(10);
    }

    if (realservo >= 3000 && realservo < 3180)
    {
      int servo3 = realservo;
      servo3 = map(servo3, 3000, 3180, 0, 180);
      myservo3.write(servo3);
      Serial.println("servo 3 On");
      if (Serial.availableForWrite() > 16)
      {
        delay(10);
      }
    }

    if (realservo >= 4000 && realservo < 4180)
    {
      int servo4 = realservo;
      servo4 = map(servo4, 4000, 4180, 0, 180);
      myservo4.write(servo4);
      if (Serial.availableForWrite() > 16)
      {
        Serial.println("servo 4 On");
      }
      delay(10);
    }
  }
}

In setup, I’ve added a visual indication (flashing) during the 5 seconds that the code checks for connection with the PC. After that it will indicate the status (led on means connected to PC, led off means not connected). Can’t test at this moment but from memory it requires serial monitor to be opened after connecting.

The other change is the availableForWrite() implementation for all serial prints.

Really appreciate the responses guys and big thanks to sterretje for the code above and your assistance via PM.

I've got to mod the HC-06 as suggested in relation to adding the resistors so will source the required components and attend to this modification.

I'll report back with an update as soon as possible.

Thanks again!

Thanks for the code sterretje.

Everything works a treat.

When I get time, I will make up permanent connections now that I know what goes where.

Thanks again for your help.

Nick_Pyner:
Since phone sees bluetooth, check bluetooth signal wiring Tx>Rx and Rx>Tx.

I think that was the problem.

I've also mentioned in my reply to OP's PM the 5V vs 3.3V (Arduino TX to HC-06 RX) 'issue' and advised on the voltage divider.

Indeed!