I am trying to build a rover controlled by an Arduino with a Funduino Motor Control Shield, and a Bluetooth HC-05 module. However, whenever I send data to it by pressing a button on the GUI that I have created in Processing, the Bluetooth receives the data, and runs for a very short period of time, and then stops, and disconnects from my laptop.
I have already checked my wiring, and I have a voltage divider. The bluetooth LED blinks at 2Hz, so is receiving power, but not connecting. When I run the Processing sketch, the bluetooth connects, and then disconnects when a button is pressed, and stops doing whatever it was doing. However, when I connect the Arduino via USB to my laptop, it works perfectly.
My code is as follows:
#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);
}
}
My processing sketch is as follows:
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');}
Schematic of wiring - in addition to this, a Motor Control Shield is added to the Arduino with four DC motors attached.
Thanks so much for your help!