3

我正在尝试构建一个由带有 Funduino 电机控制盾和蓝牙 HC-05 模块的 Arduino 控制的漫游者。但是,每当我通过按下我在处理中创建的 GUI 上的按钮向它发送数据时,蓝牙都会接收数据并运行很短的时间,然后停止并与我的笔记本电脑断开连接。

我已经检查了我的接线,我有一个分压器。蓝牙 LED 以 2Hz 的频率闪烁,因此正在接收电源,但未连接。当我运行处理草图时,蓝牙连接,然后在按下按钮时断开连接,并停止做它正在做的任何事情。但是,当我通过 USB 将 Arduino 连接到我的笔记本电脑时,它可以完美运行。

我的代码如下:

#include <AFMotor.h> 

boolean state = false; 

AF_DCMotor motorA(4); 
AF_DCMotor motorB(3); 
AF_DCMotor motorC(2); 
AF_DCMotor motorD(1); 
 

void setup() { 
  Serial.begin(9600);           // set up Serial library at 9600 bps 
  Serial.println("Motor test!");  // it is just satisfying to see that 'Motor test' message - it means your code is running on the Arduino
  // turn on motor - don't lower the speed otherwise it will make a really annoying noise
  motorA.setSpeed(220); 
  motorB.setSpeed(255); 
  motorC.setSpeed(255); 
  motorD.setSpeed(220); 
  motorA.run(RELEASE); 
  motorB.run(RELEASE); 
  motorC.run(RELEASE); 
  motorD.run(RELEASE); 
}

void loop() { 
  if (Serial.available()) {
    char val = Serial.read();
    Serial.println(val);
    if (val == 'w') {
      Serial.println(state);
      // Forward
      if (state == false) {
        state = true;
        motorA.run(BACKWARD);  // Motor A needs to be opposite - it is actually turning forward yere
        motorB.run(FORWARD); 
        motorC.run(FORWARD); 
        motorD.run(FORWARD);
      } else {
        state = false;
        motorA.run(RELEASE); 
        motorB.run(RELEASE); 
        motorC.run(RELEASE); 
        motorD.run(RELEASE); 
      }
    } else if (val == 'a') {  
      Serial.println(state);// Left
      if (state == false) {
        state = true;
        motorA.run(BACKWARD); 
        motorB.run(BACKWARD); 
        motorC.run(BACKWARD); 
        motorD.run(FORWARD); 
      } else {
        state = false;
        motorA.run(RELEASE); 
        motorB.run(RELEASE); 
        motorC.run(RELEASE); 
        motorD.run(RELEASE); 
      }
    } else if (val == 'd') {     // Right
      Serial.println(state);
      if (state == false) {
        state = true;
        motorA.run(FORWARD); 
        motorB.run(FORWARD); 
        motorC.run(FORWARD); 
        motorD.run(BACKWARD); 
      } else {
        state = false;
        motorA.run(RELEASE); 
        motorB.run(RELEASE); 
        motorC.run(RELEASE); 
        motorD.run(RELEASE); 
      }
    } else if (val == 's') {     // Reverse
      Serial.println(state);
      if (state == false) {
        state = true;
        motorA.run(FORWARD); 
        motorB.run(BACKWARD); 
        motorC.run(BACKWARD); 
        motorD.run(BACKWARD); 
      } else {
        state = false;
        motorA.run(RELEASE); 
        motorB.run(RELEASE); 
        motorC.run(RELEASE); 
        motorD.run(RELEASE); 
      }
    } else if (val == 'm') {     // Stand still
      motorA.run(RELEASE); 
      motorB.run(RELEASE); 
      motorC.run(RELEASE); 
      motorD.run(RELEASE); 
    }
  } else {
    Serial.println(0);
  }
} 

我的处理草图如下:

import controlP5.*; //import ControlP5 library
import processing.serial.*;
Serial port;

ControlP5 cp5; //create ControlP5 object

void setup(){ //Same as setup in arduino
  frameRate(50);
  size(1200, 750);                          //Window size, (width, height)
  port = new Serial(this, "COM6", 9600);   //Change this to your port
  cp5 = new ControlP5(this);
  
  Button b1 = cp5.addButton("Forward")  //The button
    .setPosition(90, 100)  //x and y coordinates of upper left corner of button
    .setSize(120, 70)      //(width, height)
  ;
  

b1.addCallback(new CallbackListener() {
    public void controlEvent(CallbackEvent theEvent) {
      switch(theEvent.getAction()) {
        case(ControlP5.ACTION_RELEASED): port.write('w'); println("p"); break;
        
      }
    }
  }
  ); 


  Button b2 = cp5.addButton("Left")  //The button
    .setPosition(90, 200)  //x and y coordinates of upper left corner of button
    .setSize(120, 70)      //(width, height)
  ;
  

b2.addCallback(new CallbackListener() {
    public void controlEvent(CallbackEvent theEvent) {
      switch(theEvent.getAction()) {
        case(ControlP5.ACTION_RELEASED): port.write('a'); println("p"); break;
      }
    }
}  
  ); 


  Button b3 = cp5.addButton("Right")  //The button
    .setPosition(290, 100)  //x and y coordinates of upper left corner of button
    .setSize(120, 70)      //(width, height)
  ;
  

b3.addCallback(new CallbackListener() {
    public void controlEvent(CallbackEvent theEvent) {
      switch(theEvent.getAction()) {
        case(ControlP5.ACTION_RELEASED): port.write('d'); println("p"); break;
        
      }
    }
  }
  ); 


  Button b4 = cp5.addButton("Reverse")  //The button
    .setPosition(290, 200)  //x and y coordinates of upper left corner of button
    .setSize(120, 70)      //(width, height)
  ;
  

b4.addCallback(new CallbackListener() {
    public void controlEvent(CallbackEvent theEvent) {
      switch(theEvent.getAction()) {
        case(ControlP5.ACTION_RELEASED): port.write('s'); println("p"); break;
        
      }
    }
  }
  ); 
}
void draw(){  //Same as loop in arduino

  background(150, 0 , 150); //Background colour of window (r, g, b) or (0 to 255)

}

void Button(){port.write('t');}

接线示意图——除此之外,Arduino 中还添加了一个电机控制屏蔽,并附有四个直流电机。 接线示意图——除此之外,Arduino 中还添加了一个电机控制屏蔽,并附有四个直流电机。

非常感谢你的帮助!

4

0 回答 0