Servos don’t respond to command on app via bluetooth

I created a robot arm and placed them on wheels and want to control the motor and servos. I took code from 2 different projects.

Code to control motors with bluetooth:

 // Starting of Program
 #include <SoftwareSerial.h>
int Input_Pin_4 = 4;
int Input_Pin_3 = 5;
int Input_Pin_2 = 6;
int Input_Pin_1 = 7;
int enA = 3;
int enB = 2;
char Value;
int bluetoothTx = 10;
int bluetoothRx = 11;
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);


void setup() {
  pinMode(Input_Pin_4, OUTPUT);  // Digital pin 9
  pinMode(Input_Pin_3, OUTPUT);  // Digital pin 10
  pinMode(Input_Pin_2, OUTPUT);  // Digital pin 11
  pinMode(Input_Pin_1, OUTPUT);  // Digital pin 12
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  Serial.begin(9600);
  bluetooth.begin(9600);
}

void loop() {
  while (bluetooth.available() > 0) {
    Value = bluetooth.read();
    Serial.println(Value);
  }

  if ( Value == 'F') {
    // Robo Pe_t Run Forward
    digitalWrite(enA, 255);
    digitalWrite(enB, 255);
    digitalWrite(Input_Pin_1, HIGH);
    digitalWrite(Input_Pin_2, LOW);
    digitalWrite(Input_Pin_3, HIGH);
    digitalWrite(Input_Pin_4, LOW);
  } else if (Value == 'B') {
    //Robo Pet Run Backward
    digitalWrite(enA, 255);
    digitalWrite(enB, 255);
    digitalWrite(Input_Pin_1, LOW);
    digitalWrite(Input_Pin_2, HIGH);
    digitalWrite(Input_Pin_3, LOW);
    digitalWrite(Input_Pin_4, HIGH);
  } else if (Value == 'L') {
    //Robo Pet Turn Left
    digitalWrite(enA, 255);
 
    digitalWrite(Input_Pin_1, LOW);
    digitalWrite(Input_Pin_2, LOW);
    digitalWrite(Input_Pin_3, HIGH);
    digitalWrite(Input_Pin_4, LOW);
  } else if (Value == 'R') {
    //Robo Pet Turn Right

    digitalWrite(enB, 255);
    digitalWrite(Input_Pin_1, HIGH);
    digitalWrite(Input_Pin_2, LOW);
    digitalWrite(Input_Pin_3, LOW);
    digitalWrite(Input_Pin_4, LOW);
  } else if (Value == 'S') {
    //Robo Pet Stop
    digitalWrite(Input_Pin_1, LOW);
    digitalWrite(Input_Pin_2, LOW);
    digitalWrite(Input_Pin_3, LOW);
    digitalWrite(Input_Pin_4, LOW);
  }
}

Code to control servo with bluetooth:

#include <Servo.h> // servo library 
Servo myservo1, myservo2, myservo3; // servo name
int bluetoothTx = 10; // bluetooth tx to 10 pin
int bluetoothRx = 11; // bluetooth rx to 11 pin
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
void setup()
{
  myservo1.attach(9); // attach servo signal wire to pin 9
  myservo2.attach(12);
  myservo3.attach(13);

  //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(50);
    }
    if (realservo >= 2000 && realservo <2180) {
      int servo2 = realservo;
      servo2 = map(servo2, 2000, 2180, 0, 180);
      myservo2.write(servo2);
      Serial.println("Servo 2 ON");
      delay(50);
    }
    if (realservo >= 3000 && realservo <3180) {
      int servo3 = realservo;
      servo3 = map(servo3, 3000, 3180, 0, 180);
      myservo3.write(servo3);
      Serial.println("Servo 3 ON");
      delay(50);
    }
  }
}

I compiled them into one file:

#include <Servo.h> // servo library 
Servo myservo1, myservo2, myservo3; // servo name
int motorOne = 3;
int motorOne2 = 4;
int motorTwo = 5;
int motorTwo2 = 6;
char Value;
int en1 = 2;
int en2 = 7;
int bluetoothTx = 10;
int bluetoothRx = 11;
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

//initial motors pin

char command; 


void setup()
{
  myservo1.attach(9); // attach servo signal wire to pin 9
  myservo2.attach(12);
  myservo3.attach(13);
   
  //Setup usb serial connection to computer
  Serial.begin(9600);
  bluetooth.begin(9600);
  //Setup Bluetooth serial connection to android

}

void loop()
{
  while (bluetooth.available() > 0) {
    Value = bluetooth.read();
    Serial.println(Value);
  }

  if ( Value == 'F') {
    // Robo Pet Run Forward
    digitalWrite(en1, HIGH);
    digitalWrite(en2, HIGH);
    digitalWrite(motorOne, HIGH);
    digitalWrite(motorOne2, LOW);
    digitalWrite(motorTwo, HIGH);
    digitalWrite(motorTwo2, LOW);
  } else if (Value == 'B') {
    //Robo Pet Run Backward
    digitalWrite(en1, HIGH);
    digitalWrite(en2, HIGH);
    digitalWrite(motorOne, LOW);
    digitalWrite(motorOne2, HIGH);
    digitalWrite(motorTwo, LOW);
    digitalWrite(motorTwo2, HIGH);
  } else if (Value == 'L') {
    //Robo Pet Turn Left
    digitalWrite(en1, HIGH);
    digitalWrite(motorOne, HIGH);
    digitalWrite(motorOne2, LOW);
    digitalWrite(motorTwo, LOW);
    digitalWrite(motorTwo2, LOW);
  } else if (Value == 'R') {
    //Robo Pet Turn Right
    
    digitalWrite(en1, HIGH);
    digitalWrite(motorTwo, HIGH);
    digitalWrite(motorTwo2, LOW);
    digitalWrite(motorOne, LOW);
    digitalWrite(motorOne2, LOW);
  } else if (Value == 'S') {
    //Robo Pet Stop
    digitalWrite(motorOne, LOW);
    digitalWrite(motorOne2, LOW);
    digitalWrite(motorTwo, LOW);
    digitalWrite(motorTwo2, LOW);
  } 
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(50);
    }
    if (realservo >= 2000 && realservo <2180) {
      int servo2 = realservo;
      servo2 = map(servo2, 2000, 2180, 0, 180);
      myservo2.write(servo2);
      Serial.println("Servo 2 ON");
      delay(50);
    }
    if (realservo >= 3000 && realservo <3180) {
      int servo3 = realservo;
      servo3 = map(servo3, 3000, 3180, 0, 180);
      myservo3.write(servo3);
      Serial.println("Servo 3 ON");
      delay(50);
    }
  }  

}

The motors respond and the servos don’t in the combined file. I tested the code for motors and servos in their separate files and they function without a problem. I found out the functions that read input for the motors and servos are mixing up their signals causing the problem.

Input reading function for car:

while (bluetooth.available() > 0) {
    Value = bluetooth.read();
    Serial.println(Value);
  }

Input reading function for servos:

  {
    unsigned int servopos = bluetooth.read();
    unsigned int servopos1 = bluetooth.read();
    unsigned int realservo = (servopos1 *256) + servopos;
    Serial.println(realservo);
}

I’ve experimented by erasing one function and keeping the other, using code from another project. All other projects about bluetooth control read inputs the same way the servos and motors do. I’m a beginner coder and don’t know how to troubleshoot this properly.

Any tips & help would be much appreciated.

Thank you!

That loop is probably consuming most of the bluetooth data and so when you get to the servo code there is <2 bytes of data available. It is kind of a moot problem due to the problems below:

  1. The way the data from bluetooth is interpreted for the motors vs the servos is completely different! What is the bluetooth device that the data is being sent from? You will have to change how the data is sent to distinguish motor vs. servo. For example, you could prepend the data sent with an ASCII 'M' or 'S' to distinguish between motor and servo and that would dictate how you interpret the data. That is just one way to do it. There are multiple ways.
  2. It would be a good idea to distinguish the start/end of a message so if the receiver got out of sync there would be a way to sync up.
  3. Ideally you would check the data for errors but you could probably get away with not doing that.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.