Bluetooth only works when arduino is plugged into the laptop

Im making a car which is controlled by bluetooth and everything is fine as long as the arduino is plugged into the laptop , I can move the car forward,backwards … etc, However when I remove the cable the bluetooth doesnt seem to work and Im not sure why
I think it’s a coding problem as the car works already when the cable is plugged into the arduino or am I wrong ?
Here’s the code(I cut some of it out like the functions and #define s) :

char btoothIn ;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

}

void loop() {
  // put your main code here, to run repeatedly:
  if (Serial.available()){
    btoothIn = Serial.read();
  

  switch(btoothIn){
  
  case 'F' :{
    moveForward();
    break;
  }
  case 'R':{
    moveRight();
    break;
  }
  case 'L':{
    moveLeft();
    break;
  }
  case 'B':{
    moveBack();
    break;
  }
  default :{
    moveStop();
  }

    
  }
  }
Serial.println(btoothIn);
}

Where does it get its power from when the USB is not plugged in ? Can you supply a wiring diagram

I think it's a coding problem as the car works already when the cable is plugged into the arduino or am I wrong

I would think so yes.

I can’t imagine what sort of code could differentiate between being run off batteries, a mains adaptor or batteries even if this is what you want to do.

Hardware however I can imagine making a difference. We often get these sorts of questions about the capacitive sensor but the same principals apply.

The laptop is offering a ground area which will improve the RF antenna of the Bluetooth system. Try connecting some foil to ground of your controller and see if it improves.

What Arduino are you using? Don't things get confused when you have Serial connected to both the PC and the Bluetooth adapter?!?

Are you by some chance, attempting to "power" an Arduino via the "Vin" or "Raw" pin or "barrel jack"?

That would be a bad idea. :astonished: