DC Motors and HC-06 not working properly

Hi everyone,
Despite I am quite new to the Arduino world, I'm trying to build a first "drone".
Anyways, I'm having a lot of problems:
I control the arduino with bluetooth, with an HC-06 sensor. It connects properly and works fine with some commands, but when I press some command like "up", "down", "X", the command is not working and the BT immediatly disconnects. (I can immediatly reconnect).
So, I'm thinking the issues are in some part of my code related to those commands, but I can't find out any:

const float  note_C0 = 16.35;  //C0
// here i have a lot of others const float notes that I will not bother you with
int cont=0;
   
  
#define speakerPin = 3; // speaker connected to digital pin 

#define motor1Pin = 7;    // H-bridge leg 1 (pin 2 of H bridge)
#define motor2Pin = 8;    // H-bridge leg 2 (pin 7of H bridge)
#define enablePin = 5;    // H-bridge enable pin 1 
#define enablePin2 = 6;    // H-bridge enable pin 9
#define motor21Pin = 9;    // H-bridge leg 1 (pin 15 of H bridge
#define motor22Pin = 10;    // H-bridge leg 2 (pin 10 of bridge)
  
#define led=12;//red terminal of rgb led
#define led2=13;//blue terminal of rgb led

int incomingByte;      // a variable to read incoming serial data into
  
#include <Servo.h> 
Servo myservo;  
               
int pos = 1500; 


void setup() {
  Serial.begin(9600); // open serial port to receive data
  myservo.attach(11);
  
  pinMode(speakerPin, OUTPUT); // sets the speakerPin to be an output 
  
  pinMode(motor1Pin, OUTPUT);
  pinMode(motor2Pin, OUTPUT);
  pinMode(enablePin, OUTPUT);
  pinMode(motor21Pin, OUTPUT);
  pinMode(motor22Pin, OUTPUT);
  pinMode(enablePin2, OUTPUT);
   
  pinMode(led, OUTPUT);
  pinMode(led2, OUTPUT);

  digitalWrite(enablePin, LOW);
  digitalWrite(enablePin2, LOW);
  
   r2D2(); //starting sound
      delay(500);
}
  
void loop() {
  
  if (Serial.available() > 0) {
    // read the oldest byte in the serial buffer:
    incomingByte = Serial.read();
    
    //up
    if (incomingByte == '1') {
      digitalWrite(led, LOW);//change color of led
      digitalWrite(led2, HIGH);
      analogWrite(enablePin, 200);  // turn on the motor 1
      digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low
      digitalWrite(motor2Pin, HIGH);  // set leg 2 of the H-bridge high
     
      analogWrite(enablePin2, 200);  // turn  on  second motor
      digitalWrite(motor21Pin, LOW);   // set leg 1 of the H-bridge low
      digitalWrite(motor22Pin, HIGH);  // set leg 2 of the H-bridge high
      delay(10);
      digitalWrite(enablePin, LOW);  // turn off motor 1
      digitalWrite(enablePin2, LOW);  // turn off motor 2
   
      cont=0;
    }  
      
    //down
    else if (incomingByte == '2') {
      digitalWrite(led, LOW);  //change color of led
      digitalWrite(led2, HIGH);
      analogWrite(enablePin, 200);  // turn on the motor 1
      digitalWrite(motor1Pin, HIGH);   // set leg 1 of the H-bridge high
      digitalWrite(motor2Pin, LOW);  // set leg 2 of the H-bridge low
      
      analogWrite(enablePin2, 200);  // turn  on  second motor
      digitalWrite(motor21Pin, HIGH);   // set leg 1 of the H-bridge high
      digitalWrite(motor22Pin, LOW);  // set leg 2 of the H-bridge low
      
      delay(10);
      digitalWrite(enablePin, LOW);  // turn off motor 1
      digitalWrite(enablePin2, LOW);  // turn off motor 2
      
      cont=0;
     }     
    
    //left
    else if (incomingByte == '3') {
      digitalWrite(led2, LOW);
      digitalWrite(led, HIGH);
   
      digitalWrite(enablePin2, HIGH);   // turn  on  second motor
      digitalWrite(motor21Pin, LOW);   // set leg 1 of the H-bridge low
      digitalWrite(motor22Pin, HIGH);  // set leg 2 of the H-bridge high
      delay(10);
      
      digitalWrite(enablePin2, LOW);   // turn off motor 2
   
    cont=0;

    }
    
    //right 
    else if (incomingByte == '4') {
      digitalWrite(led2, LOW);
      digitalWrite(led, HIGH);
   
      digitalWrite(enablePin, HIGH);  // turn  on motor 1
      digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low
      digitalWrite(motor2Pin, HIGH);  // set leg 2 of the H-bridge high
      
      delay(10);
      digitalWrite(enablePin, LOW);  // turn off motor 1
  
      cont=0;
      
      }     
  
  //triangle
    else if ((incomingByte == '9')&&(cont==0)) {
      cont++;
      ohhh();
      delay(500);
  
    }  
  
  
  //x
   else if ((incomingByte == '7')&&(cont==0)) {
      cont++;
      catcall();
      delay(500);
      
      }  
    
    else if (incomingByte == '8') {
      pos=pos+35;
      if(pos<2000){myservo.writeMicroseconds(pos);}
      else{pos=2000;}
      delay(10);
      cont=0;
      
      }  
    
    else if (incomingByte == 'A') {
      cont=0;
      pos=pos-35;
      if(pos>1000){myservo.writeMicroseconds(pos);}
      else{pos=1000;}
      delay(10);
      
      }  
  
  }
  
}


    
void beep (int speakerPin, float noteFrequency, long noteDuration)
{    
  int x;
  // Convert the frequency to microseconds
  float microsecondsPerWave = 1000000/noteFrequency;
  // Calculate how many HIGH/LOW cycles there are per millisecond
  float millisecondsPerCycle = 1000/(microsecondsPerWave * 2);
  // Multiply noteDuration * number or cycles per millisecond
  float loopTime = noteDuration * millisecondsPerCycle;
  // Play the note for the calculated loopTime.
  for (x=0;x<loopTime;x++)   
          {   
              digitalWrite(speakerPin,HIGH); 
              delayMicroseconds(microsecondsPerWave); 
              digitalWrite(speakerPin,LOW); 
              delayMicroseconds(microsecondsPerWave); 
          } 
}     
     


void r2D2(){
          beep(speakerPin, note_A7,100); //A 
          beep(speakerPin, note_G7,100); //G 
          beep(speakerPin, note_E7,100); //E 
          beep(speakerPin, note_C7,100); //C
          beep(speakerPin, note_D7,100); //D 
          beep(speakerPin, note_B7,100); //B 
          beep(speakerPin, note_F7,100); //F 
          beep(speakerPin, note_C8,100); //C 
          beep(speakerPin, note_A7,100); //A 
          beep(speakerPin, note_G7,100); //G 
          beep(speakerPin, note_E7,100); //E 
          beep(speakerPin, note_C7,100); //C
          beep(speakerPin, note_D7,100); //D 
          beep(speakerPin, note_B7,100); //B 
          beep(speakerPin, note_F7,100); //F 
          beep(speakerPin, note_C8,100); //C 
}


void catcall() {
  for (int i=1000; i<5000; i=i*1.05) {
    beep(speakerPin,i,10);
  }
 delay(300);
 
  for (int i=1000; i<3000; i=i*1.03) {
    beep(speakerPin,i,10);
  }
  for (int i=3000; i>1000; i=i*.97) {
    beep(speakerPin,i,10);
  }
}

void ohhh() {
  for (int i=1000; i<2000; i=i*1.02) {
    beep(speakerPin,i,10);
  }
  for (int i=2000; i>1000; i=i*.98) {
    beep(speakerPin,i,10);
  }
}

void uhoh() {
  for (int i=1000; i<1244; i=i*1.01) {
    beep(speakerPin,i,30);
  }
  delay(200);
  for (int i=1244; i>1108; i=i*.99) {
    beep(speakerPin,i,30);
  }
}


long microsecondsToCentimeters(long microseconds)
{
  return microseconds / 29 / 2;
}

Furthermore, about the DC motors commands, only the "left" command looks like it's working, but, if I hold down the "movement arrow" on the bluetooth controller (a random mobile app), the motors just move a little bit when i release it, while, obviously, I need it to keep going the whole time I'm pressing it.

Hope someone can help me out, thank you.
circuit.PNG

circuit.PNG

Congratulations on posting your code correctly on your first post.

But I have no idea what commands your "a random mobile app" is sending. Without knowing that it's impossible to help. E.g. maybe you know when a key is pressed but how do you tell when the key is held down or released?

When you say "only the "left" command looks like it's working" what is it actually doing? Does the motor run? The code looks like one motor will just blip for a very short time which is not what I think of a working.

Steve

Thank you so much Steve for your answer!!
Yes exactly, it blips just a little while when i press it (but at least that doesn't cause a disconnection).

Could you give me an example of how I should write the code to make the motors run for the whole time I'm holding the button?
The application I'm trying to use asks me to link the value I want as an input, so, for example, I got the "incomingByte=1" in the code, so I link "1" to the up-direction.

By the way it's really probable I'm doing everything wrong, I'm not following a tutorial or anything, just trying to arrange something.

And, about the Bluetooth module (HC-06), what could be the reason why it disconnects if I press some of the command buttons? More likely a hardware or software/code one?

Bluetooth disconnects are usually power or reset problems. What is the voltage of your power bank and what is its current capability? Trying to power motors through a breadboard also often causes power problems. The code doesn't look like a problem.

Does your mobile app give you the ability to associate a code with a button being RELEASED as well as being PRESSED? Because that's what you need in order to tell if you have just quickly tapped a button or are holding it on. Without that you're stuck.

Steve

Thank you once again for your time!

By the way, I'm using a 5V power bank (for mobiles), but you just let me think that this isn't enough for the whole circuit. I got there like two DC motors 3V each, a 4.8V Servo, a 3.3V HC-06, a led and a little speaker (3 watt).
How many volts do you (approximately) think I need to power the circuit? Honestly I got no ideas.
I could also try to double connect that power bank (it has got two outputs) to the breadboard, so that I should gain a 10V power (?).

Cheers.

5V sounds sufficient for those components.
It's the total current draw that could be a problem.
Might need a 5V to 3.3V stepdown regulator for the 3V and 3.3V stuff.

The 3.3V regulator on the board can only 150mA, I'm sure the motors are dragging that 3.3V down and causing the HC-06 to reset.

Thank you CrossRoads!
So, if I understood something, basically the problem is that motors are 3V but I input 5V?
Because these dc motors should be "3-6 volt", not sure what that means, but I thought they could use between 3 and 6 volt based on what you input.

No, the problem is they need more current than the regulator can supply.
The regulator is likely going into shutdown briefly and causing the HC-06 to act like it is getting reset.