问题描述
我正在尝试构建一个由Arduino控制的漫游器,该漫游器具有Funduino电机控制罩和Bluetooth HC-05模块。但是,每当我按在处理中创建的GUI上的按钮向其发送数据时,蓝牙就会接收数据,并在很短的时间内运行,然后停止并与笔记本电脑断开连接。>
我已经检查过接线,并且有一个分压器。蓝牙指示灯以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,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,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,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,150); //Background colour of window (r,g,b) or (0 to 255)
}
void Button(){port.write('t');}
接线原理图-除此之外,在Arduino上还添加了一个带有四个直流电动机的电动机控制板。
非常感谢您的帮助!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)