Hi, I've been trying to create a remote controlled RC Car using Arduino Uno, Arduino Nano, 2 HC-05 Bluetooth Modules, a L298n motor controller, and a ps2 joystick.
When I try to control the car using the Master HC-05, the wheels stutter due to the data it receives: it alternates between receiving the value 0 (turn wheel) and 508 (stop wheel). (See pictures below).
I don't know what seems to be causing the issue. Let me know if more information is needed. Any help is greatly appreciated!
//MASTER
int xAxis, yAxis;
#define yPin 2
#define xPin 3
#define swPin 8
void setup() {
Serial.begin(38400);
pinMode(7, OUTPUT);
pinMode(swPin, OUTPUT);
pinMode(xPin, INPUT);
pinMode(yPin, INPUT);
digitalWrite(swPin, HIGH);
}
void loop() {
if (Serial.available() > 1){
digitalWrite(7, HIGH);
}
//xAxis = analogRead(xPin);
yAxis = analogRead(yPin);
//Serial.write(xAxis/4); Serial.print("x: "); Serial.println(xAxis/4);
Serial.write(yAxis/4); Serial.print("y: "); Serial.println(yAxis/4);
delay(20);
}
//SLAVE
#include <SoftwareSerial.h>
#define lm1 4
#define lm2 5
#define rm1 6
#define rm2 7
#define enA 2
#define enB 3
#define lm3 10
#define lm4 11
#define rm3 8
#define rm4 9
int x, y, motorSpeedA, motorSpeedB, tempMap, xAxis, yAxis;
void setup() {
pinMode(lm1, OUTPUT);
pinMode(lm2, OUTPUT);
pinMode(rm1, OUTPUT);
pinMode(rm2, OUTPUT);
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(lm3, OUTPUT);
pinMode(lm4, OUTPUT);
pinMode(rm3, OUTPUT);
pinMode(rm4, OUTPUT);
Serial.begin(38400);
}
void loop() {
x = 510/4;
y = 510/4;
while (Serial.available() > 1){ // Checks whether data is coming from the serial port
x = Serial.read();
delay(10);
y = Serial.read();
}
delay(10);
xAxis = x*4;
yAxis = y*4;
if (yAxis < 470){ //backward
backward();
motorSpeedA = map(yAxis, 470, 0, 0, 255);
motorSpeedB = map(yAxis, 470, 0, 0, 255);
}
else if (yAxis > 550){ //forward
forward();
motorSpeedA = map(yAxis, 550, 1023, 0, 255);
motorSpeedB = map(yAxis, 550, 1023, 0, 255);
}
else{
idle();
motorSpeedA = 0;
motorSpeedB = 0;
}
/*if (xAxis < 470){ //right
tempMap = map(xAxis, 470, 0, 0, 255);
motorSpeedA += tempMap;
motorSpeedB -= tempMap;
bound();
}
else if (xAxis > 550){ //left
tempMap = map(xAxis, 550, 1023, 0, 255);
motorSpeedA -= tempMap;
motorSpeedB += tempMap;
bound();
}*/
//Serial.print("X: "); Serial.println(xAxis);
Serial.print("Y: "); Serial.println(yAxis);
analogWrite(enA, motorSpeedA); //Serial.print("A: "); Serial.println(motorSpeedA);
analogWrite(enB, motorSpeedB); //Serial.print("B: "); Serial.println(motorSpeedB);
}
void bound(){
if (motorSpeedA > 255) {motorSpeedA = 255;}
else if (motorSpeedA < 0) {motorSpeedA = 0;}
if (motorSpeedB > 255) {motorSpeedB = 255;}
else if (motorSpeedB < 0) {motorSpeedB = 0;}
}
void forward(){
digitalWrite(lm1, HIGH);
digitalWrite(lm2, LOW);
digitalWrite(rm1, HIGH);
digitalWrite(rm2, LOW);
}
void backward(){
digitalWrite(lm1, LOW);
digitalWrite(lm2, HIGH);
digitalWrite(rm1, LOW);
digitalWrite(rm2, HIGH);
}
void idle(){
digitalWrite(lm1, LOW);
digitalWrite(lm2, LOW);
digitalWrite(rm1, LOW);
digitalWrite(rm2, LOW);
}
