My HC-05not working i dont know where wrong

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

//輸出到L298N的腳位
#define MT_L 6  //ENA
#define IN1 5
#define IN2 4

#define IN3 5
#define IN4 4
#define MT_R 3  //ENB
void Stop();
void Forward();
void Backward();


char Car_status;
int Speed_value=150;
#include <Servo.h> // servo library 
Servo myservo1, myservo2, myservo3, myservo4, myservo5; // servo name

int bluetoothTx = 12; // bluetooth tx to 12 pin
int bluetoothRx = 13; // bluetooth rx to 13 pin

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

void setup()
{
  pinMode(MT_L,OUTPUT);
  pinMode(IN1,OUTPUT);
  pinMode(IN2,OUTPUT);
  pinMode(IN3,OUTPUT);
  pinMode(IN4,OUTPUT);
  pinMode(MT_R,OUTPUT);

  myservo1.attach(11); // attach servo signal wire to pin 9
  myservo2.attach(10);
  myservo3.attach(9);
  myservo4.attach(8);
  //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(10);
    }
    if (realservo >= 2000 && realservo <2180) {
      int servo2 = realservo;
      servo2 = map(servo2, 2000, 2180, 0, 180);
      myservo2.write(servo2);
      Serial.println("Servo 2 ON");
      delay(10);
    }
    if (realservo >= 3000 && realservo <3180) {
      int servo3 = realservo;
      servo3 = map(servo3, 3000, 3180, 0, 180);
      myservo3.write(servo3);
      Serial.println("Servo 3 ON");
      delay(10);
    }
    if (realservo >= 4000 && realservo <4180) {
      int servo4 = realservo;
      servo4 = map(servo4, 4000, 4180, 0, 180);
      myservo4.write(servo4);
      Serial.println("Servo 4 ON");
      delay(10);
    }
    if (realservo >= 5000 && realservo <5180) {
      int servo5 = realservo;
      servo5 = map(servo5, 5000, 5180, 0, 180);
      myservo5.write(servo5);
      Serial.println("Servo 5 ON");
      delay(10);
  }
  if(Serial.available() == true){   
    Car_status = Serial.read();  
  }
  
//當藍芽有資料傳輸時,會將讀取到的字元存到Car_status。
  if (bluetooth.available()){      
    Car_status =bluetooth.read();  
    Serial.println(Car_status);  
  }
  
//依照Car_status的字元來選擇車子方向控制

 switch(Car_status){   
  case 'F':
    Forward();
    Serial.println("Forward");
    Car_status='N';
    break;
  case 'B':
    Backward();
    Serial.println("Backward");
    Car_status='N';
    break;

  case 'S':
    Stop();
    Serial.println("Stop");
    Car_status='N';
    break;
  case 'v':
    Speed_value=0; //先將速度歸0
    do{
      if (bluetooth.available()){
        Car_status =bluetooth.read();  //讀取藍芽傳送字元
        Serial.println(Car_status); 
        //當藍芽讀取到字元'e'時,就會跳出迴圈。
        if( Car_status=='e'){        
          Serial.println(Speed_value);
          break;
        }         
        else
        //將傳送來的速度數值字元轉成整數。
        Speed_value=10*Speed_value+(Car_status-48); 
      }
    }while(true);      
  }
}
}
//下面是停止、前進、後退、左轉、右轉的函式。
void Stop()
{
  analogWrite(MT_L,0);
  analogWrite(MT_R,0);
}

void Forward()
{
   digitalWrite(IN1,HIGH);
   digitalWrite(IN2,LOW);
   digitalWrite(IN3,LOW);
   digitalWrite(IN4,HIGH);  
   analogWrite(MT_R,Speed_value);
   analogWrite(MT_L,Speed_value); 
}
void Backward()

  {
   digitalWrite(IN1,LOW);
   digitalWrite(IN2,HIGH);
   digitalWrite(IN3,HIGH);
   digitalWrite(IN4,LOW);  
   analogWrite(MT_R,Speed_value);
   analogWrite(MT_L,Speed_value); 
}

I have a problem to my code
I can't use it
My phone can connect
but i can't control
The ardiuno wiring is normal

Does your code really have smileys in it ?

Please follow the advice on posting code given in Read this before posting a programming question in order to make your sketch easy to read, copy and test

In particular note the advice to Auto format code in the IDE and to use code tags when posting code here as it prevents some combinations of characters in code being interpreted as HTML commands such as italics, bold or a smiley character, all of which render the code useless

If the code exceeds the 9000 character inline limit then attach it to a post

UKHeliBob:
Does your code really have smileys in it ?

Please follow the advice on posting code given in Read this before posting a programming question in order to make your sketch easy to read, copy and test

In particular note the advice to Auto format code in the IDE and to use code tags when posting code here as it prevents some combinations of characters in code being interpreted as HTML commands such as italics, bold or a smiley character, all of which render the code useless

If the code exceeds the 9000 character inline limit then attach it to a post

UKHeliBob:
Does your code really have smileys in it ?

Please follow the advice on posting code given in Read this before posting a programming question in order to make your sketch easy to read, copy and test

In particular note the advice to Auto format code in the IDE and to use code tags when posting code here as it prevents some combinations of characters in code being interpreted as HTML commands such as italics, bold or a smiley character, all of which render the code useless

If the code exceeds the 9000 character inline limit then attach it to a post

No
It dont have smile

@blackwolf,
Please start using code tags, the </> button on the menu. This looks like the same code that I fixed elsewhere already this morning.

Thanks
Moderator

It dont have smile

In your original post it does

Please read the advice linked to in post #1 and post your code using code tags to prevent the forum software interpreting some code as smileys and to make it easy to copy for examination

What is sending to the HC05?

Is the sender paired with the HC05?

Is an app on the sender connected to the HC05?

Did you get the HC05 to communicate from the sender to your Arduino with a simple test code before adding anything else?

test code for HC05 paired and connected to a Bluetooth device.

//Bluetooth HC05 equipped Arduino and device with Bluetooth terminal app
//Sends from serial monitor through a HC05 equipped Arduino to a connected device app
//Receives from a connected device app
//HC05 is paired with another Bluetooth device
//HC05 is connected with an app on the other Bluetooth device
//For the connected app, I recommend the Bluetooth Serial Terminal:
//https://play.google.com/store/apps/details?id=de.kai_morich.serial_bluetooth_terminal&hl=en_US&gl=US

#include <SoftwareSerial.h>

SoftwareSerial BTserial(4, 5); // RX | TX

void setup()
{
   Serial.begin(9600);
   BTserial.begin(9600);
}

void loop()
{
   // send from serial monitor to the Bluetooth device connected the HC05.
   if (Serial.available())
   {
      BTserial.write(Serial.read());
   }

   // print incoming data from the Bluetooth device connected the HC05
   // to the serial monitor
   if (BTserial.available())
   {
      Serial.print(char(BTserial.read()));
   }
}

Read the how get the most out of this forum sticky to see how to properly post code. Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code in code tags.

And remove the / before each line. They do not belong there.

1 Like

groundFungus:
HC05發送了什麼?

發送者是否與HC05配對?

發送器上的應用程序是否已連接到HC05?

在添加其他內容之前,您是否使HC05用簡單的測試代碼從發送方與Arduino進行通信?

閱讀如何充分利用本論壇的內容,以了解如何正確地發布代碼。在代碼標籤中發布代碼之前,請刪除無用的空白並使用IDE自動格式化工具(crtl-t或“工具”,“自動格式化”)對代碼進行格式化。

並在每行之前刪除/。他們不屬於那裡。

發送者是否與HC05配對? YES

發送器上的應用程序是否已連接到HC05?YES

在添加其他內容之前,您是否使HC05用簡單的測試代碼從發送方與Arduino進行通信?YES

當我使用手機時,我發送了一個code to HC-05 ardiuno light dont bright

?

UKHeliBob:

i did it

You have not got the hang of quoting and adding a reply, have you ?
Do not put your reply inside the quote tags

What exactly have you done ?

code to HC-05 ardiuno light dont bright

What LED? I see nothing about a LED in the posted code.

i did it

Nope, not even close.

</>#define MT_L 6 //ENA
</>#define IN1 5
</>#define IN2 4
</>#define IN3 5
</>#define IN4 4
Really? Does this look anything like the test code that I posted that is in proper code tags?

If the HC05 is connected the LED on the HC05 will flash 2 times quickly every 2 seconds.

Let's try again. Read the how get the most out of this forum sticky to see how to properly post code. Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code in code tags.

groundFungus:
What LED? I see nothing about a LED in the posted code.
Nope, not even close.

</>#define MT_L 6 //ENA
</>#define IN1 5
</>#define IN2 4
</>#define IN3 5
</>#define IN4 4
Really? Does this look anything like the test code that I posted that is in proper code tags?

Let's try again. Read the how get the most out of this forum sticky to see how to properly post code. Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code in code tags.

OK i Know how to use it

I have a problem to my code
I can't use it
My phone can connect
but i can't control
The ardiuno wiring is normal

See reply #9 and please reply properly, not inside the quote tags

but i can't control

That tells us nothing useful. What does the code actually do? You have serial prints in the code. Are the prints returning expected results?

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

What do you get in that serial print? Is it what you expect given what was sent?

groundFungus:
That tells us nothing useful. What does the code actually do? You have serial prints in the code. Are the prints returning expected results?

    unsigned int servopos = bluetooth.read();

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




What do you get in that serial print? Is it what you expect given what was sent?

Yep that can work
But i think
connect have wrong
i can't sent RT&RX
Ardiuno light can't bright

You have 4 different places where you read available data from serial. That is not the best way to go. It is better to have one serial read that inputs the data and parses the data. Structure the data so that it is easy to receive and parse. The serial input basics tutorial shows robust ways to receive serial data.

塞克斯

塞克斯

What is that?

connect have wrong

Post a schematic showing the connections.

i can't sent RT&RX

I do not know what that means. The HC05 should connect to the Arduino RX to HC05 TX, Arduino TX to HC05 RX.

Ardiuno light can't bright

What LED? What is the LED on the HC05 module doing?

Please load and run the test code that I posted. It is easier to help you if we are working with simple code that is proven to work. If we can get the test code to work then it will be easier to incorporate into your program.

I am trying to help, but you are not making it easy.

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