I'm trying to make an RC car with an arduino uno and hc-05 bluetooth module. When i try to read a string from the module and send it to the serial monitor it sometimes shows a reversed question mark instead of the second character. I tried to read it both as a string or by individual character but it still does this and the app that i'm using for communication is ArduinoRC. And also i can't send anything from the arduino to the phone. Why?
P.S. I've put the module in AT mode and the baud rate was 9600 so that's not the problem.
Here is my code:
#include <SoftwareSerial.h>
// connect motor controller pins to Arduino digital pins
// motor one
int enA = 10; // in1, in2, enA - motor 1 - stanga
int in1 = 9;
int in2 = 8;
int in3 = 13;
int in4 = 12;
int enB = 11; // in3, in4, enB - motor 2 - dreapta
int lastV=0;
SoftwareSerial BTSerial(2,3);
void setup()
{
// set all the motor control pins to outputs
pinMode(enA, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
Serial.begin(9600);
Serial.println("START");
BTSerial.begin(9600);
BTSerial.println("START");
}
void loop()
{
if (BTSerial.available() > 0)
{
Serial.println("!");
BTSerial.println("!");
/*
char x;
String str = "";
while (BTSerial.available() > 0)
{
delay(100);
x = BTSerial.read();
str += x;
}
*/
String str = BTSerial.readString();
Serial.println(str);
BTSerial.println(str);
if (str=="STOP") //stop
{
analogWrite(enA, 0);
analogWrite(enB, 0);
}
else
{
bool fata=false;
int i=0, v=0;
while (i<str.length() && int(str.charAt(i)) >= 48 && int(str.charAt(i)) <= 48+9)
{
v=v*10 + str.charAt(i) - 48;
i++;
}
if (str.charAt(i)=='F') // merge in fata
{
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
analogWrite(enA, v);
analogWrite(enB, v);
fata=true;
}
else if (str.charAt(i)=='S') //merge in spate
{
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
analogWrite(enA, v);
analogWrite(enB, v);
}
if (str.length()>=i+2)
{
i++;
if (str.charAt(i)=='D') // merge in dreapta
{
int w=0;
for (int j=i+1;j<str.length();j++)
{
w=w*10+str.charAt(j)-48;
}
int x=v-w;
if (x>=0)
{
analogWrite(enB, x);
}
else
{
if (fata)
{
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
analogWrite(enB, -x);
}
else
{
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
analogWrite(enB, -x);
}
}
}
else if (str.charAt(i)=='S') // merge in stanga
{
int w=0;
for (int j=i+1;j<str.length();j++)
{
w=w*10+str.charAt(j)-48;
}
int x=v-w;
if (x>=0)
{
analogWrite(enA, x);
}
else
{
if (fata)
{
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
analogWrite(enA, -x);
}
else
{
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
analogWrite(enA, -x);
}
}
}
}
}
}
}
motor_test.ino (3.11 KB)