Hi,
Lets me start with saying I'm very new to Arduino and Arduino coding.
I'm trying to create a remote control car as the first step before moving the project to a remote control lawnmower. Long story short I have everything working fine if I connect TX from Arduino #1 to RX of Arduino #2 via jumper cable.
The joystick sends the analog reading to the other Arduino fine and powers the motor and displays correctly on the serial monitor.
The problem I am faced with is as soon as I bring my 2xHC-05 modules into play it doesn't read the correct data. Now it does receive something as it displays constant " | " symbols on the serial monitor.
My first guess is I'm not converting strings/ints/bytes correctly but cannot see where I'm going wrong.
Here is my code, which I pieced together from many hours of googling.
Any help would be greatly appreciated.
Transmitter code:
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(2, 3); // RX, TX
int joyPin = 0; //A0
void setup() {
BTSerial.begin(9600);
Serial.begin(9600);
}
void loop() {
int joystick = analogRead(joyPin) / 4;
BTSerial.println(joystick);
Serial.println(joystick);
//mySerial.println(1111);
//Serial.println(1111);
delay(10);
}
Receiver code:
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(2, 3); // RX, TX
String incomingData;
int speedPin = 11;
int motor1inPin1 = 10;
int motor1inPin2 = 9;
void setup() {
BTSerial.begin(9600);
Serial.begin(9600);
}
void loop() {
while(BTSerial.available()) {
incomingData += char(BTSerial.read());
}
if(!BTSerial.available()) {
if(incomingData != "") {
Serial.println(incomingData);
if (incomingData.toInt() > 130) {
analogWrite(speedPin, incomingData.toInt());
digitalWrite(motor1inPin1, HIGH);
digitalWrite(motor1inPin2, LOW);
}
else if (incomingData.toInt() < 120) {
analogWrite(speedPin, 255 - incomingData.toInt());
digitalWrite(motor1inPin1, LOW);
digitalWrite(motor1inPin2, HIGH);
}
else {
digitalWrite(motor1inPin1, LOW);
digitalWrite(motor1inPin2, LOW);
}
incomingData = "";
}
}
delay(10);
}
Attached is a picture of both serials and what i see (Transmitter on the right & Receiver on the Left):