I tried to do that. I have uploaded my wired code set here, but I will upload both again together. I have been comparing the two and con't find the difference that would make it go.
/*
---- Receiver Code ----
Mert Arduino Tutorial & Projects (YouTube)
Please Subscribe for Support
*/
#include <Servo.h> //the library which helps us to control the servo motor
#include <SPI.h> //the communication interface with the modem
#include "RF24.h" //the library which helps us to control the radio modem (nRF24L)
//define our L298N control pins
//Motor Left
const int enA = 10; //enA for speed control
const int IN1 = 2; // IN1
const int IN2 = 3; // IN2
//Motor RIght
const int enB = 5; //enB for speed control
const int IN3 = 4; // IN3
const int IN4 = 6; // IN4
int LeftMSpeed; // interger to capture speed
int RightMSpeed; // to capture R speed
int LMotor;
int RMotor;
int joystick[8]; //The element array holding the data from joysticks
int fail = 0; // for when we lose radio range
int servo_pos;
bool newData = false; //
int leftAdcVal;
int leftMotorVal;
int rightAdcVal;
int rightMotorVal;
//define the servo name
Servo myServo;
RF24 radio(7, 8); /*This object represents a modem connected to the Arduino.
Arguments 5 and 10 are a digital pin numbers to which signals
CE and CSN are connected.*/
const uint64_t pipe = 0xE8E8F0F0E1LL; //the address of the modem,that will receive data from the Arduino.
void setup() {
Serial.begin(115200); // for outputting debuggin information to serial monitor
delay(3000);
Serial.println("Nrf24l01 Receiver Starting");
pinMode(enB, OUTPUT); // RIgnt Motor speed PWM
pinMode(enA, OUTPUT); //Left Motor Speed PWM
pinMode(IN3, OUTPUT); //Right Forward
pinMode(IN1, OUTPUT); //Left Forward
pinMode(IN2, OUTPUT); //Left Backward
pinMode(IN4, OUTPUT);//Right Backward
//define the servo input pins
myServo.attach(14); //A0
radio.begin(); //it activates the modem.
radio.openReadingPipe(1, pipe); //determines the address of our modem which receive data.
radio.startListening(); //enable receiving data via modem
}
void loop() {
if (radio.available() ) {
radio.read( joystick, sizeof(joystick) ); // Fetch the data payload
leftAdcVal = joystick[0]; // range 0 to 1023
leftMotorVal = leftAdcVal - 443; // range -124 to +124
rightAdcVal = joystick[2]; // range 0 to 1023
rightMotorVal = rightAdcVal - 443; // range -124 to +124
analogWrite(enA, leftMotorVal);
analogWrite(enB, rightMotorVal);
Serial.print("LMotor =");
Serial.print(leftMotorVal);
Serial.print(" RMotor =");
Serial.println(rightMotorVal);
}
LMotor = joystick[0]; // range 319 to 567
// leftMotorVal = leftAdcVal - 443; // range -64 to +64
// subtracting 513 (rather than 512) ensures that no later value will exceed 255
if (LMotor < 504) {//This is backwards
// LeftMSpeed = map(LMotor, 504, 414, 0, 255); //366
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
// analogWrite(enA, LeftMSpeed);
}
else if (LMotor > 504 && RMotor < 511) {
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
}
else if (LMotor > 511) {//This is forwards
// LeftMSpeed = map(LMotor, 511, 604, 0, 255);
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
// analogWrite(enA, LeftMSpeed);
}
RMotor = joystick[2];
if (RMotor < 505) { //this is backwards
// RightMSpeed = map(RMotor, 505, 366, 0, 255);
//Set Right motor backwards
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
// analogWrite(enB, RightMSpeed);
}
else if (RMotor > 506 && RMotor < 512) {
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
}
else if (RMotor > 512) { //this is forwards
// RightMSpeed = map(RMotor, 512, 660, 0, 255);
//Set Left motor forwards
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
// analogWrite(enB, RightMSpeed);
}
// for the servo motor
servo_pos = map(joystick[7], 0, 1024, 0, 255);
myServo.write(servo_pos);
}
Wired code that worked:
#include <Servo.h>; // added by jonawadl
//Joystick Pins
int x_key = A0;
int y_key = A1;
int x_pos;
int y_pos;
//Motor Pins
int EN_A = 11; //Enable pin for first motor
int IN1 = 9; //control pin for first motor
int IN2 = 8; //control pin for first motor
int IN3 = 7; //control pin for second motor
int IN4 = 6; //control pin for second motor
int EN_B = 10; //Enable pin for second motor
//Servo pins added by jonawadl
int pot_pin = A2;
//Define Servo Name
Servo myServo; // added by jonawadl
int pot_value;
int servo_pos;
//Initializing variables to store data
int motor_speed;
int motor_speed1;
void setup ( ) {
Serial.begin (9600); //Starting the serial communication at 9600 baud rate
//Initializing the motor pins as output
pinMode(EN_A, OUTPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
pinMode(EN_B, OUTPUT);
//Initializng the joystick pins as input
pinMode (x_key, INPUT) ;
pinMode (y_key, INPUT) ;
myServo.attach(17); // added by jonawadl
}
void loop () {
x_pos = analogRead (x_key) ; //Reading the horizontal movement value
y_pos = analogRead (y_key) ; //Reading the vertical movement value
pot_value = analogRead (pot_pin);
if (x_pos < 400){ //Rotating the left motor in clockwise direction
motor_speed = map(x_pos, 400, 0, 0, 255); //Mapping the values to 0-255 to move the motor
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
analogWrite(EN_A, motor_speed);
}
else if (x_pos>400 && x_pos <600){ //Motors will not move when the joystick will be at center
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
}
else if (x_pos > 600){ //Rotating the left motor in anticlockwise direction
motor_speed = map(x_pos, 600, 1023, 0, 255);
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
analogWrite(EN_A, motor_speed);
}
if (y_pos < 400){ //Rotating the right motor in clockwise direction
motor_speed1 = map(y_pos, 400, 0, 0, 255);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
analogWrite(EN_B, motor_speed1);
}
else if (y_pos>400 && y_pos <600){
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
}
else if (y_pos > 600){ //Rotating the right motor in anticlockwise direction
motor_speed1 = map(y_pos, 600, 1023, 0, 255);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
analogWrite(EN_B, motor_speed1);
}
servo_pos = map(pot_value, 0, 1024, 0,255); // added by jonawadl
myServo.write(servo_pos); // added by jonawadl
}