You are right. That is the code for the remote control. The code with the motor driver is below.
#include <SPI.h>
#include <RF24.h>
#include <RF24_config.h>
// Radio pin setup
#define cePin 9
#define csnPin 10
// US sensor pin setup
#define trigPin 14
#define echoPin 2
#define accuracy 4
// Motor pin setup
#define enableLeft 6
#define leftForward 4
#define leftBackward 3
#define enableRight 5
#define rightForward 8
#define rightBackward 7
// Init RF24 radio
RF24 radio(cePin, csnPin);
const uint64_t pipe0 = 0xF0F0F0AA;
uint8_t p0 = 0;
// Variables
String receivedMessage = "";
int chr[1];
int joyX = 4;
int joyY = 4;
int speedCoeff = 0;
int left = 0;
int right = 0;
void setup(){
// Setup RF24 radio
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(p0, pipe0);
radio.startListening();
// Setup US sensor
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
// Setup LEFT whel
pinMode(enableLeft, OUTPUT);
analogWrite(enableLeft, 255);
pinMode(leftForward, OUTPUT);
digitalWrite(leftForward, LOW);
pinMode(leftBackward, OUTPUT);
digitalWrite(leftBackward, LOW);
// Setup RIGHT wheel
pinMode(enableRight, OUTPUT);
analogWrite(enableRight, 255);
pinMode(rightForward, OUTPUT);
digitalWrite(rightForward, LOW);
pinMode(rightBackward, OUTPUT);
digitalWrite(rightBackward, LOW);
}
void loop(){
// Get Distance sensor
float duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10); // Added this line
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) / 29.1;
// Watch control messages
while(radio.available()){
radio.read(chr, 1);
char receivedChar = chr[0];
if (chr[0] != 2){
receivedMessage.concat(receivedChar);
} else {
joyX = receivedMessage.substring(0,1).toInt();
joyY = receivedMessage.substring(1,2).toInt();
if(distance < 15){
joyX = 4;
joyY = 4;
}
if(joyY < 4){
// Backward
digitalWrite(rightBackward, HIGH);
digitalWrite(rightForward, LOW);
digitalWrite(leftBackward, HIGH);
digitalWrite(leftForward, LOW);
speedCoeff = map(joyY, 0, 4, 255, 0);
speedCoeff = speedCoeff < 0 ? 0 : speedCoeff;
speedCoeff = speedCoeff > 255 ? 255 : speedCoeff;
} else if(joyY > 4){
// Forward
digitalWrite(rightBackward, LOW);
digitalWrite(rightForward, HIGH);
digitalWrite(leftBackward, LOW);
digitalWrite(leftForward, HIGH);
speedCoeff = map(joyY, 4, 9, 0, 255);
speedCoeff = speedCoeff < 0 ? 0 : speedCoeff;
speedCoeff = speedCoeff > 255 ? 255 : speedCoeff;
}
right = map(joyX, 4, 9, 255, 0);
right = right < 0 ? 0 : right;
right = right > 255 ? 255 : right;
right = (right + speedCoeff) / 2;
analogWrite(enableRight, right);
left = map(joyX, 0, 4, 0, 255);
left = left < 0 ? 0 : left;
left = left > 255 ? 255 : left;
left = (left + speedCoeff) / 2;
analogWrite(enableLeft, left);
if(joyY == 4){
// Stopped
if(joyX < 4){
// Left
digitalWrite(rightBackward, LOW);
digitalWrite(rightForward, HIGH);
digitalWrite(leftBackward, HIGH);
digitalWrite(leftForward, LOW);
right = map(joyX, 0, 4, 255, 0);
right = right < 0 ? 0 : right;
right = right > 255 ? 255 : right;
analogWrite(enableRight, right);
left = map(joyX, 0, 4, 255, 0);
left = left < 0 ? 0 : left;
left = left > 255 ? 255 : left;
analogWrite(enableLeft, left);
} else if(joyX > 4){
// Right
digitalWrite(rightBackward, HIGH);
digitalWrite(rightForward, LOW);
digitalWrite(leftBackward, LOW);
digitalWrite(leftForward, HIGH);
right = map(joyX, 4, 9, 0, 255);
right = right < 0 ? 0 : right;
right = right > 255 ? 255 : right;
analogWrite(enableRight, right);
left = map(joyX, 4, 9, 0, 255);
left = left < 0 ? 0 : left;
left = left > 255 ? 255 : left;
analogWrite(enableLeft, left);
} else if (joyX == 4){
// Stopped
digitalWrite(rightBackward, LOW);
digitalWrite(rightForward, LOW);
digitalWrite(leftBackward, LOW);
digitalWrite(leftForward, LOW);
analogWrite(enableRight, 0);
analogWrite(enableLeft, 0);
}
}
receivedMessage = "";
}
}
}
Thank you!