I have got Arduino Mega 2560, Ladyada Motor Shield v1.0, HC-07 Bluetooth Module, and Android Bluetooth Terminal App. I combined Motor Shield on Arduino Mega. I get M+(5V)/GND point to run my HC-07 Bluetooth module to give energy. Bluetooth's RxD=Arduino's Tx1(18. pin), and Bluetooth's TxD=Arduino's Rx1(19. pin). I connected Arduino Mega to PC via USB cable to give energy. There is a DC motor attached on Motor Shield's M1 side.
My Bluetooth's connection, my Arduino's connection and Android application's situations are ready to use. But, I only control my DC motor from my PC's "Serial Monitor". Although, I want to control via Bluetooth, I could only control via PC. Where am I doing an failure onto?
My Arduino Code is:
#include <AFMotor.h>
#include <SoftwareSerial.h>
SoftwareSerial bluetooth(19, 18);
char val; // Variable to receive data from the serial port
// DC motor on M1
AF_DCMotor motor(1);
// DC motor on M2
AF_DCMotor motor2(2);
int i;
void setup() {
Serial.begin(115200);
bluetooth.begin(115200);
// turn on motor #1
motor.setSpeed(200);
motor.run(RELEASE);
// turn on motor #2
motor2.setSpeed(200);
motor2.run(RELEASE);
}
// Move FORWARD
void go_forward(){
motor.run(FORWARD);
motor2.run(FORWARD);
for (i=0; i<255; i++) {
motor.setSpeed(i);
motor2.setSpeed(i);
}
}
// Move REVERSE
void go_reverse(){
motor.run(BACKWARD);
motor2.run(BACKWARD);
for (i=0; i<255; i++) {
motor.setSpeed(i);
motor2.setSpeed(i);
}
}
// Move LEFT
void stop_go_forward(){
motor.run(RELEASE);
motor2.run(RELEASE);
}
void go_left(){
motor.run(FORWARD);
for (i=0; i<255; i++) {
motor.setSpeed(i);
}
}
// Move RIGHT
void go_right(){
motor2.run(FORWARD);
for (i=0; i<255; i++) {
motor2.setSpeed(i);
}
}
// Read serial port and perform command
void performCommand() {
if (Serial.available()) {
val = Serial.read();
}
//Bluetooth kontrol kısmı
if(bluetooth.available()) {
val = (bluetooth.read());
Serial.print(val);
}
if (val == '1') { // Forward
go_forward();
} else if (val == '2') { // Reverse
go_reverse();
} else if (val == '3') { // Stop Forward
stop_go_forward();
} else if (val == '4') { // Right
go_right();
} else if (val == '5') { // Left
go_left();
}
}
void loop() {
performCommand();
}
/* KOMUTLAR
1 - ileri
2 - geri
4 - sağ
5 - sol
3 - dur
*/
Best Answer
The usual issue I have with these bluetooth dongles is configuring them to the same baud-rate and framing that my Arduino uses. Can you load a simple Hello program and then, with the dongle connected the to hardware serial pins, be able to read what it transmits? Once it works that way, then try to get it working on the software serial.
One quirk of these devices is that there is no way to reset it to a known state; it wakes up configured however you last left it. And of course, I never remember how I last used it! :( Virtuabotix, a vendor of these devices, has a program to configure it by trying all baud rates until it gets a recognizable response, and then configuring it to your choice of baud rate and PIN (for pairing). You may have to extend his baud table; it only goes to 57600 baud.