So I have tried to create a remote control car controlled by a joystick (two potentiometers) but am failing to get it to work correctly. I am using an L293 H-Bridge motor driver to control one motor and also have a basic 180 degree servo connected straight to the arduino. The transmitter and receiver are 433Mhz modules and that was the first thing I checked when I started troubleshooting my code.
The modules send and receive fine a string of numbers on their own. I do not know where to start troubleshooting the motor control and servo control. Basically when I am moving the potentiometers, I can notice that the motor control changes but instead of a nice speed change or reversing, the motor kinda just turns on and off randomly every few seconds. I can't find a pattern other than that it changes when the potentiometer is completely one way or another. The servo control doesn't do anything. Thank you for any ideas y'all may have!
Transmitter Code
#include <RCSwitch.h>
RCSwitch mySwitch = RCSwitch();
int drive;
int steer;
void setup() {
Serial.begin(9600);
// Transmitter is connected to Arduino Pin #10
mySwitch.enableTransmit(10);
}
void loop() {
int steer = analogRead(0) + 10000; //"encodes" the joystick x
// steering value into a distinquishable number on the reciever end.
int drive = analogRead(1) + 5000; //"encodes" the joystick y
// steering value into a distinquishable number on the reciever end.
/* Same switch as above, but using decimal code */
mySwitch.send(steer, 24); // sends "encoded" steering value
mySwitch.send(drive, 24); // sends "encoded" motor control value
delay(100);
}
Receiver Code
#include <Servo.h>
#include <RCSwitch.h>
Servo myservo;
RCSwitch mySwitch = RCSwitch();
int drive;
int steer;
void setup() {
Serial.begin(9600);
mySwitch.enableReceive(0); // Receiver on interrupt 0 => that is pin #2
myservo.attach(8);
}
void loop() {
if (mySwitch.available()) {
int value = mySwitch.getReceivedValue();
if (value == 0) {
Serial.print("Unknown encoding");
}
if (value < 10000){ // takes motor speed value encoded
int drive = value - 5000; // "decodes" motor speed value
int drivevalue1 = map(drive, 1, 1023, 0, 255); // maps value to PWM value
if (drive < 512){ // for forward drive
analogWrite(9, drivevalue1); // 9 will be high
digitalWrite(10, LOW); // 10 will be low
}
if (drive >= 512){ // for backward drive
analogWrite(10, drivevalue1); // 10 will be high
digitalWrite(9, LOW); // 9 will be low
}
if (value > 10000){ // takes servo position value
int steer = value - 10000; // "decodes" steering value
int steervalue = map(steer, 1, 1023, 30, 150); // maps steering value to servo position
myservo.write(steervalue); // writes servo position value to servo
delay(15);
}
else {
Serial.print("Received ");
Serial.print( mySwitch.getReceivedValue() );
Serial.print(" / ");
Serial.print( mySwitch.getReceivedBitlength() );
Serial.print("bit ");
Serial.print("Protocol: ");
Serial.println( mySwitch.getReceivedProtocol() );
}
delay(50);
mySwitch.resetAvailable();
}
}
}