Code only works properly when connected to usb

So I'm making a robotic dog and I'm using the Bluetooth HC-05 module to try to control the robot. It works perfectly fine when plugged into USB, but when I unplug it, the commands from the Bluetooth module no longer seem to do anything. I'm using a 4S li-po battery stepped down with a voltage and amperage regulator to which everything that needs power is connected, so I don't think it's a power or grounding issue. I'm thinking it has something to do with serial commands and not being plugged into the computer? The BT.available()>0 command only works when connected to the pc maybe? I'm basically trying to get the robot to only walk forward when it receives the command "F" from the Bluetooth module. From the programming in the app it will receive the "F" command once when the button is pressed, and when I let go it will send "." once.

Code below...

#include <Servo.h>
#include <SoftwareSerial.h>
SoftwareSerial BT(3,4);

bool FL = false;
bool BL = false;
bool FR = false;
bool BR = false;

float v;
float w;
float x;
float y;
float z;
float A;
float B;
float L;
float D;
float Adeg;
float Bdeg;
float Ddeg;
char data;

Servo TFL;
Servo BFL;
Servo TWFL;
Servo TFR;
Servo BFR;
Servo TWFR;
Servo TBL;
Servo BBL;
Servo TWBL;
Servo TBR;
Servo BBR;
Servo TWBRR;

void setup() {
  // put your setup code here, to run once:
TFL.attach(12);
BFL.attach(13);
TWFL.attach(8);
TFR.attach(A1);
BFR.attach(A2);
TWFR.attach(A0);
TBL.attach(10);
BBL.attach(9);
TWBL.attach(7);
TBR.attach(2);
BBR.attach(A5);
TWBRR.attach(A3); 
BT.begin(9600);

BFR.write(178);
TFR.write(100);
TFL.write(90);
BFL.write(7);
TWFL.write(70);
TWFR.write(65);
TBL.write(98);
BBL.write(2);
TWBL.write(110);
TBR.write(90);
BBR.write(180);
TWBRR.write(105);
delay(5000);
v = -40;
w = 40;
x = 40;
y = -40;
}

void loop() {
if(BT.available()>0){
data = BT.read();
}
if(data == 'F'){
//Front Left Leg Foward
if(v >= -40 && FL == false){
  v = v + 0.25;
  z = -pow((-(v*v)+1600),0.25)+115;
L = sqrt(z*z+v*v);
A = acos(L/180);
D = atan(v/z);
Adeg = A * (180/3.14159);   //Angle for A
Bdeg = 180-2*Adeg;   //Angle for B
Ddeg = D * (180/3.14159);   //Angle for D

TFL.write(90+Adeg-Ddeg);
BFL.write(Bdeg+7);

delay(0.5);

if (v == 40 && FL == false){
FL = !FL;
} 
}

//Front Left Leg Backward
if(v <= 40 && FL == true){
  v = v - 0.25;
  z = 125;
L = sqrt(z*z+v*v);
A = acos(L/180);
D = atan(v/z);
Adeg = A * (180/3.14159);   //Angle for A
Bdeg = 180-2*Adeg;   //Angle for B
Ddeg = D * (180/3.14159);   //Angle for D

TFL.write(90+Adeg-Ddeg);
BFL.write(Bdeg+7);

delay(0.5);

if (v == -40 && FL == true){
FL = !FL;
} 
}

//Back Left Leg Foward
if(w >= -40 && BL == true){
  w = w + 0.25;
  z = -pow(-(w*w)+1600,0.25)+115;
L = sqrt(z*z+w*w);
A = acos(L/180);
D = atan(w/z);
Adeg = A * (180/3.14159);   //Angle for A
Bdeg = 180-2*Adeg;   //Angle for B
Ddeg = D * (180/3.14159);   //Angle for D

TBL.write(98+Adeg-Ddeg);
BBL.write(Bdeg+2);

delay(0.5);

if (w == 40 && BL == true){
BL = !BL;
} 
}

//Back Left Leg Backward
if(w <= 40 && BL == false){
  w = w - 0.25;
  z = 125;
L = sqrt(z*z+w*w);
A = acos(L/180);
D = atan(w/z);
Adeg = A * (180/3.14159);   //Angle for A
Bdeg = 180-2*Adeg;   //Angle for B
Ddeg = D * (180/3.14159);   //Angle for D

TBL.write(98+Adeg-Ddeg);
BBL.write(Bdeg+2);

delay(0.5);

if (w == -40 && BL == false){
BL = !BL;
} 
}

//Front Right Leg Foward
if(x >= -40 && FR == true){
  x = x + 0.25;
  z = -pow(-(x*x)+1600,0.25)+115;
L = sqrt(z*z+x*x);
A = acos(L/180);
D = atan(x/z);
Adeg = A * (180/3.14159);   //Angle for A
Bdeg = 180-2*Adeg;   //Angle for B
Ddeg = D * (180/3.14159);   //Angle for D

TFR.write(100-Adeg+Ddeg);
BFR.write(178-Bdeg);

delay(0.5);

if (x == 40 && FR == true){
FR = !FR;
}
}


//Front Right Leg Backward
if(x <= 40 && FR == false){
  x = x - 0.25;
  z = 125;
L = sqrt(z*z+x*x);
A = acos(L/180);
D = atan(x/z);
Adeg = A * (180/3.14159);   //Angle for A
Bdeg = 180-2*Adeg;   //Angle for B
Ddeg = D * (180/3.14159);   //Angle for D

TFR.write(100-Adeg+Ddeg);
BFR.write(178-Bdeg);

delay(0.5);

if (x == -40 && FR == false){
FR = !FR;
}
}


//Back Right Leg Foward
if(y >= -40 && BR == false){
  y = y + 0.25;
  z = -pow(-(y*y)+1600,0.25)+115;
L = sqrt(z*z+y*y);
A = acos(L/180);
D = atan(y/z);
Adeg = A * (180/3.14159);   //Angle for A
Bdeg = 180-2*Adeg;   //Angle for B
Ddeg = D * (180/3.14159);   //Angle for D

TBR.write(98-Adeg+Ddeg);
BBR.write(180-Bdeg);

delay(0.5);

if (y == 40 && BR == false){
BR = !BR;
}
}


//Back Right Leg Backward
if(y <= 40 && BR == true){
  y = y - 0.25;
  z = 125;
L = sqrt(z*z+y*y);
A = acos(L/180);
D = atan(y/z);
Adeg = A * (180/3.14159);   //Angle for A
Bdeg = 180-2*Adeg;   //Angle for B
Ddeg = D * (180/3.14159);   //Angle for D

TBR.write(90-Adeg+Ddeg);
BBR.write(180-Bdeg);

delay(0.5);

if (y == -40 && BR == true){
BR = !BR;
}
}
}
}

It 99% sounds like a hardware problem rather than code.

Draw out your schematic, and post here… maybe grounds etc"

Not a great schematic but hopefully it's readable. Keep in mind there's 4 servos that aren't shown on this diagram and pretend the LED is the Bluetooth module (there's also a resistor connecting ground and the RX pin on the Bluetooth module).

Thanks for that, but sadly a frizzy drawing is only half as good as a schematic.

What’s the power supply spec? link to the product if you can.
All these servos can draw a lot of current..
I wouldn’t reply on proto strips to handle the peak or operating current demands.

Note, if the supply is 5V, it shouldn’t go to the Vin pin of the Arduino, that’s for 7V5 or higher.

Link to the buck converter below...

I'm not sure what I have the voltage and current set to. I'll have to get my hands on a multimeter and check. It's being powered by a 16v Li-Po battery.

Also I'm using a shield like this to connect everything.

I've also just checked the voltage and at the moment I have it around 7.2 ish volts

So not a breadboard like your schematic? Have you checked the current limit on the power converter? Just which pin on the Uno have you connected to power to?

No it's not a breadboard I just used that to show the connections. I believe the current limit is set to as high as it will go. I just turned it until the pot started clicking and I measured the amperage when none of the servos are moving at a little over 12 amps. The power is connected to the vin pin on the Uno.

Show us good images of your actual wiring.

  • If you ever attach external 5V to the Arduino 5V pin, do not plug the USB/PC into the Arduino USB jack.

I'd be surprised if you can tell much from this it's pretty messy...
I've also noticed that if I open up the app and repeatedly press the forward button the legs will twitch like it does the loop once but then stops.