Hello,
I have come across a problem. I am trying to control some motors using bluetooth, but seem to be unable to receive data while the led on my bluetooth module is blinking. When it is off, all seems well.
What I am using:
- Arduino uno
- Adafruit motorshield v1
- HC-05 BT module
- 'Arduino Joystick' app on android.
I have connected the RX and TX of the HC-05 to pins 14 and 15 (instead of 0 and 1) since those are available on the shield and it makes programming over usb more convenient. I tested the same setup on pins 0 and 1 with hardware serial, but got the same results.
The app seems to connect. I get 2 longish blinks on the HC-05 led in one second then one second of no blinks. When I send commands in the app I see them coming in instantly if the HC-05 led is off. If the led is blinking, no command comes in, until the 2 blinks are over, after which I receive some delayed data. I am new to using bluetooth in a project, so I am not sure where the problem is. The barebones code I used to test my setup is below.
Thank you
#include <AFMotor.h>
#include <SoftwareSerial.h>
String command = "0";
const int RXpin = 14;
const int TXpin = 15;
SoftwareSerial BTserial(RXpin, TXpin); // RX | TX
//define motors on adafruit motor board
AF_DCMotor motorLB(1, MOTOR12_1KHZ);
AF_DCMotor motorRB(2, MOTOR12_1KHZ);
AF_DCMotor motorLF(4, MOTOR12_1KHZ);
AF_DCMotor motorRF(3, MOTOR12_1KHZ);
void setup() {
motorLB.setSpeed(200);
motorLF.setSpeed(200);
motorRB.setSpeed(200);
motorRF.setSpeed(200);
Serial.begin(9600);
BTserial.begin(9600);
}
void loop() {
if(BTserial.available()>0){
command = BTserial.readStringUntil('#'); //App sends: AAASSSN# where A is angle, S is strength and N is button
Serial.println(command);
if(command.length()==7){
int angle = command.substring(0,3).toInt();
int strength = command.substring(3,6).toInt()*2.5;
int button = command.substring(6,8).toInt();
Serial.print(angle);
Serial.print("\t");
Serial.println(strength);
if((angle < 180) && (strength > 100)){
StartForward(strength);
}
if(strength < 100){
Stop();
}
BTserial.flush();
command="";
}
}//end BTserial
}// end void loop
void StartForward(int spd){
motorLB.setSpeed(spd);
motorLF.setSpeed(spd);
motorRB.setSpeed(spd);
motorRF.setSpeed(spd);
motorLB.run(FORWARD);
motorLF.run(FORWARD);
motorRB.run(FORWARD);
motorRF.run(FORWARD);
}