Bluetooth Vehicle Project

I’m making a robot vehicle that has 2 DC motors for wheels and 2 servos controlling an arm. I am controlling it via Bluetooth by an App Inventor app.

This setup (and code/app) works fine for the 2 servos on their own and the DC motors on their own, but combining the code makes the servos stop functioning.

(Circuit Diagram attached and code below)
If I am unclear please ask,
Thanks in advance!

#include <SoftwareSerial.h> // TX RX software library for bluetooth

#include <Servo.h> // servo library 
Servo bigservo, smallservo; // servo name - big servo for moving left and right - small servo for moving up and down

int bluetoothTx = 1; // bluetooth tx to 1 pin
int bluetoothRx = 0; // bluetooth rx to 0 pin

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

int motorPin1 = 3; // pin 2 on L293D IC
int motorPin2 = 4; // pin 7 on L293D IC
int motorPin3 = 7; // pin 10 on L293D IC
int motorPin4 = 8; // pin 15 on L293D IC
int enablePin1 = 5; // pin 1 on L293D IC
int enablePin2 = 9; // pin 9 on L293D IC

int state;
int flag=0;        //makes sure that the serial only prints once the state
 
void setup() {
    // sets the pins as outputs:
    pinMode(motorPin1, OUTPUT);
    pinMode(motorPin2, OUTPUT);
    pinMode(motorPin3, OUTPUT);
    pinMode(motorPin4, OUTPUT);
    pinMode(enablePin1, OUTPUT);
    pinMode(enablePin2, OUTPUT);

    bigservo.attach(10); // attach big servo signal wire to pin 10
    smallservo.attach(11); // attach small servo signal wire to pin 11
    
    // sets enablePin 1 and 2 high so that motor can turn on:
    digitalWrite(enablePin1, HIGH);
    digitalWrite(enablePin2, HIGH);
    // initialize serial communication at 9600 bits per second:
    Serial.begin(9600);

    //Setup Bluetooth serial connection to android
    bluetooth.begin(9600);  
}
 
void loop() {
    
    //if some date is sent, reads it and saves in state
    if(Serial.available() <= 4){     
      state = Serial.read();   
      flag=0;
    }   
      // if the state is '0' the DC motor will turn off
      if (state == '0') {
        digitalWrite(motorPin1, LOW); // set pin 2 on L293D low
        digitalWrite(motorPin2, LOW); // set pin 7 on L293D low
        digitalWrite(motorPin3, LOW);
        digitalWrite(motorPin4, LOW);         
        if(flag == 0){
          Serial.println("Motors: off");
          flag=1;
        }
    }
      // if the state is '1' the motor will move forward
      else if (state == '1') {
        digitalWrite(motorPin1, LOW); // set pin 2 on L293D low
        digitalWrite(motorPin2, HIGH); // set pin 7 on L293D high
        digitalWrite(motorPin3, LOW); 
        digitalWrite(motorPin4, HIGH); 
        if(flag == 0){
          Serial.println("Motor: forward");
          flag=1;
        }
    }
      // if the state is '2' the motor will move backward
      else if (state == '2') {
        digitalWrite(motorPin1, HIGH); // set pin 2 on L293D high
        digitalWrite(motorPin2, LOW); // set pin 7 on L293D low
        digitalWrite(motorPin3, HIGH);
        digitalWrite(motorPin4, LOW);  
        if(flag == 0){
          Serial.println("Motor: left");
          flag=1;
        }
    }
      // if the state is '3' the motor will move left
      else if (state == '3') {
        digitalWrite(motorPin1, HIGH); 
        digitalWrite(motorPin2, LOW); 
        digitalWrite(motorPin3, LOW);
        digitalWrite(motorPin4, HIGH);  
        if(flag == 0){
          Serial.println("Motor: left");
          flag=1;
        }
    }
      // if the state is '4' the motors will move right
      else if (state == '4') {
        digitalWrite(motorPin1, LOW); 
        digitalWrite(motorPin2, HIGH); 
        digitalWrite(motorPin3, HIGH);
        digitalWrite(motorPin4, LOW);  
        if(flag == 0){
          Serial.println("Motor: right");
          flag=1;
        }
    }
  //Read from bluetooth and write to usb serial
  else if(bluetooth.available() >4) // receive number from bluetooth
  {
    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);
      bigservo.write(servo1);
      Serial.println("Big Servo ON");
      delay(10);
    }
    if (realservo >= 2000 && realservo <2180) {
      int servo2 = realservo;
      servo2 = map(servo2, 2000, 2180, 0, 180);
      smallservo.write(servo2);
      Serial.println("Small Servo ON");
      delay(10);
    }
  }
}

They'll want to know what "stops functioning" means.
What does and does not happen?

That malfunction is no surprice using that kind of battory. There is no nuclear powerstation inside. It is suitable for low current applications like fire alarms, remote Controls but defenatly not for motors.

ieee488:
They'll want to know what "stops functioning" means.
What does and does not happen?

Neither servo will turn when commanded via Bluetooth

Railroader:
That malfunction is no surprice using that kind of battory. There is no nuclear powerstation inside. It is suitable for low current applications like fire alarms, remote Controls but defenatly not for motors.

The 9v is only powering the dc motors which works fine. The servos are getting 5V from the arduino from a plug.

What sort of battery would you recommend?

How is the Arduino powered? How much and were?

How will the actuator work? Horizontally or vertically? What load will it be moving?

You should not drive servos from the Arduino 5v pin, it cannot supply the current needed .
They should be powered from a separate supply of 4.8 or 6v as per the servo specification .

Unless that pin is powered directly by a beafy 5 volt supply but that is seldomly the case…….

So I fixed the power issue, and I've deduced that the code is the problem. The part for controlling the DC motors interferes with the part for controlling the servos.

Does anybody know how to fix this?

Can You show the pwr solution clearly?

This Peace of code can’t possibly work:

void loop() {
    
    //if some date is sent, reads it and saves in state
    if(Serial.available() <= 4){     
      state = Serial.read();   
      flag=0;

Serial.available returns an integer. You runt the code even if nothing existst to read, when zero is returned.