Hello ,I'm new to Arduino and I'm attempting to build a RC vehicle using two UNO boards and two Bluetooth HC-05 modules configured as master and slave.
Two joysticks on the master side control the car's throttling and steering.
On the slave side, I'm using a servo motor to steer. I also use two motors for forward and reverse motion, which are controlled by a l911 h bridge motor driver.
Can someone look over the code? It compiles correctly but does not provide the desired output.
Even though the code is uploaded, the motor never turns on when moving the joystick from the master side, and the servo buzzes but never moves.
CODE FOR MASTER
int xAxis, yAxis,xAxis1, yAxis1;
void setup() {
Serial.begin(38400);
}
void loop() {
xAxis = analogRead(A0);
yAxis = analogRead(A1);
xAxis1 = analogRead(A2);
yAxis1 = analogRead(A3);
Serial.write(xAxis/4);
Serial.write(yAxis/4);
Serial.write(xAxis1/4);
Serial.write(yAxis1/4);
Serial.print(xAxis/4);
Serial.print(yAxis/4);
Serial.print(xAxis1/4);
Serial.print(yAxis1/4);
delay(1000);
}
CODE FOR SLAVE
#include <Servo.h>
Servo myservo;
int motor_left[] = {3, 5};
int motor_right[] = {9, 6};
int xAxis, yAxis, xAxis1, yAxis1;
unsigned int x = 0;
unsigned int y = 0;
int motorSpeed = 0;
void setup()
{
int i;
for (i = 0; i < 2; i++) {
pinMode(motor_left[i], OUTPUT);
pinMode(motor_right[i], OUTPUT);
Serial.begin(38400);
myservo.attach(10);
}
}
void loop()
{
x = 510 / 4;
y = 510 / 4;
while (Serial.available() >= 2)
{
x = Serial.read();
delay(1000);
y = Serial.read();
Serial.print(x);
Serial.print(y);
}
delay(1000);
xAxis = x * 4;
yAxis = y * 4;
xAxis1 = x * 4;
yAxis1 = y * 4;
if (yAxis < 470) {
motorSpeed = map(yAxis, 470, 0, 0, 255);
digitalWrite(motor_left[0], motorSpeed);
digitalWrite(motor_left[1], LOW);
digitalWrite(motor_right[0], motorSpeed);
digitalWrite(motor_right[1], LOW);
Serial.print(motorSpeed);
}
else if (yAxis > 550)
{
motorSpeed = map(yAxis, 550, 1023, 0, 255);
digitalWrite(motor_left[0], LOW);
digitalWrite(motor_left[1], motorSpeed);
digitalWrite(motor_right[0], LOW);
digitalWrite(motor_right[1], motorSpeed);
Serial.print(motorSpeed);
}
else if (xAxis1 != 0)
{
int val = map(xAxis, 0, 1023, 0, 179);
int servoAng = val;
myservo.write(servoAng);
Serial.print(servoAng);
}
}