Vấn đề với mô-đun Bluetooth (HC 05)

Oct 19 2020

Tôi đang cố gắng tạo ra một robot 4WD được điều khiển bằng Bluetooth. tôi đã làm theo một hướng dẫn mà tôi tìm thấy trên mạng (https://howtomechatronics.com/tutorials/arduino/arduino-robot-car-wireless-control-using-hc-05-bluetooth-nrf24l01-and-hc-12-transceiver-modules/). Sau khi kết nối thành công mô-đun Bluetooth 2 HC 05 của tôi, tôi đã sử dụng tập lệnh từ hướng dẫn và sửa đổi nó một chút để nó hoạt động cho thiết lập của tôi. Mã cho rô bốt:

//Code for Slave
#define enA 9
#define in1 8
#define in2 7
#define enB 10
#define in3 6
#define in4 5
int xAxis, yAxis;
unsigned int  x = 0;
unsigned int  y = 0;
int motorSpeedA = 0;
int motorSpeedB = 0;
void setup() {
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  Serial.begin(9600); // Default communication rate of the Bluetooth module
}
void loop() {
  // Default value - no movement when the Joystick stays in the center
  x = 510 / 4;
  y = 510 / 4;
  // Read the incoming data from the Joystick, or the master Bluetooth device
  while (Serial.available() >= 2) {
    x = Serial.read();
    Serial.print("X: ");
    Serial.println(x);
    delay(10);
    y = Serial.read();
    Serial.print("Y: ");
    Serial.println(y);
  }
  delay(10);
  // Convert back the 0 - 255 range to 0 - 1023, suitable for motor control code below
  xAxis = x*4;
  yAxis = y*4;
  // Y-axis used for forward and backward control
  if (yAxis < 470) {
    backMov;
    // Convert the declining Y-axis readings for going backward from 470 to 0 into 0 to 255 value for the PWM signal for increasing the motor speed
    motorSpeedA = map(yAxis, 470, 0, 0, 255);
    motorSpeedB = map(yAxis, 470, 0, 0, 255);
  }
  else if (yAxis > 550) {
    fowardMov;
    // Convert the increasing Y-axis readings for going forward from 550 to 1023 into 0 to 255 value for the PWM signal for increasing the motor speed
    motorSpeedA = map(yAxis, 550, 1023, 0, 255);
    motorSpeedB = map(yAxis, 550, 1023, 0, 255);
  }
  // If joystick stays in middle the motors are not moving
  else {
    motorSpeedA = 0;
    motorSpeedB = 0;
  }
  // X-axis used for left and right control
  if (xAxis < 470) {
    // Convert the declining X-axis readings from 470 to 0 into increasing 0 to 255 value
    int xMapped = map(xAxis, 470, 0, 0, 255);
    // Move to left - decrease left motor speed, increase right motor speed
    motorSpeedA = motorSpeedA + xMapped;
    motorSpeedB = motorSpeedB - xMapped;
    // Confine the range from 0 to 255
    if (motorSpeedA > 255) {
      motorSpeedA = 255;
    }
    if (motorSpeedB < 0) {
      motorSpeedB = 0;
    }
  }
  if (xAxis > 550) {
    // Convert the increasing X-axis readings from 550 to 1023 into 0 to 255 value
    int xMapped = map(xAxis, 550, 1023, 0, 255);
    // Move right - decrease right motor speed, increase left motor speed
    motorSpeedA = motorSpeedA - xMapped;
    motorSpeedB = motorSpeedB + xMapped;
    // Confine the range from 0 to 255
    if (motorSpeedA < 0) {
      motorSpeedA = 0;
    }
    if (motorSpeedB > 255) {
      motorSpeedB = 255;
    }
  }
  // Prevent buzzing at low speeds (Adjust according to your motors. My motors couldn't start moving if PWM value was below value of 70)
  if (motorSpeedA < 70) {
    motorSpeedA = 0;
  }
  if (motorSpeedB < 70) {
    motorSpeedB = 0;
  }
  analogWrite(enA, motorSpeedA); // Send PWM signal to motor A
  analogWrite(enB, motorSpeedB); // Send PWM signal to motor B
}
void fowardMov(){
  digitalWrite(in2,HIGH);
  digitalWrite(in4,HIGH);
  digitalWrite(in1, LOW);
  digitalWrite(in3, LOW);
}

void backMov(){
  digitalWrite(in1,HIGH);
  digitalWrite(in3,HIGH);
  digitalWrite(in2, LOW);
  digitalWrite(in4, LOW);
}

Mã cho Bộ điều khiển:

#define Xaxis A0
#define Yaxis A1

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

void loop() {
  // put your main code here, to run repeatedly:
 Serial.write(analogRead(Xaxis)/4);
 Serial.write(analogRead(Yaxis)/4);
 delay(20);
}

Sau khi tải lên mã, tôi đã cố gắng di chuyển nó nhưng nó không hoạt động vì vậy tôi đặt các câu lệnh in để xem giá trị của X và Y. ban đầu nó không hoạt động vì vậy tôi đã hoán đổi các mô-đun Bluetooth mà tôi có và kết nối lại chúng. tôi cũng đã thay đổi dòng này.

 while (Serial.available() >= 2)

Tới đây

 if (Serial.available() > 0)

tôi bắt đầu nhận các giá trị từ master đến slave nhưng tôi nhận thấy các giá trị giống nhau đối với x và y vì vậy tôi đã tìm kiếm một hướng dẫn khác trên internet và tìm thấy điều này (https://create.arduino.cc/projecthub/andriy-baranov/arduino-4wd-rc-car-639953?ref=similar&ref_id=18369&offset=3) đọc mã của anh ấy, tôi tự hỏi nô lệ hoạt động như thế nào. Theo tài liệu chính thức về Arduino, Serial.read () không có bất kỳ đối số nào nhưng tôi vẫn thử nó và nó không hoạt động. Tôi quyết định thử lại mã từ hướng dẫn đầu tiên mà tôi đã kiểm tra lần này, nó có vẻ như nó đang hoạt động vì vậy tôi đã thử di chuyển cần điều khiển và màn hình nối tiếp đã in các giá trị chính xác cho cả hai lúc đầu, sau đó nó hoán đổi các giá trị của chúng rồi đóng băng. Làm cách nào để ngăn nó đóng băng và hoán đổi các giá trị? Ngoài ra, tại sao vòng lặp while không chạy mãi mãi không phải là lượng dữ liệu trong bộ đệm nối tiếp luôn cao hơn 2? Cảm ơn bạn.

Trả lời

user3765883 Oct 20 2020 at 22:02

Thiết lập của bạn CÁCH quá phức tạp! Đơn giản hóa thiết lập của bạn đến mức nó chỉ giải quyết phần đang gây ra vấn đề cho bạn; trong trường hợp của bạn cổng nối tiếp kết nối giữa chủ và nô lệ. Giải quyết vấn đề CHỈ ĐÓ và sau đó tiếp tục