nrf24l01 + old aritronics + L298N need a little help.

I have an answer for you on both those cases.

If LMotor and RMotor should be after, then why does RMotor work? I'll move them to see what changes.

I do have output to my serial monitor. I get a nice flow of numbers when I manipulate either joystick. I used that to tweak my mappings.

I did build the separate circuit as you suggested with out the wireless. I even added in joystick control to see how that works. It worked. Here's my code.

 #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
      }

Something about this code has me puzzled. Can you shed some light on this bit of code and what it does in my program.

void loop() {

LMotor = joystick[0];
RMotor = joystick[2];

If I leave it out entirely, Right motor works but acceleration is in wrong direction. I pushup and motor smoothly accelerates. If I push down it starts at full speed then slows down as joystick travels further from center. In this case reversing my map from low to high does not make a difference. Left Motor does nothing.

With the bit of code as it is written above in the sketch. The right motor works exactly as expected. Left does nothing.

if I change at piece to what I think it should be:

void loop() {

LMotor = analogRead (joystick[0]);
RMotor = analogRead (joystick[2]);

Then I'm back to right motor behaving exactly as with first example where I leave out that piece entirely and my serial monitor not displaying joystick inputs.

Please wait for a response before writing a second Post - it is difficult to keep up.

The lines

LMotor = joystick[0];
RMotor = joystick[2];

copy the values from the joystick[] array into the two variables.

However your code seems very confused because later on it copies the same joystick[] values into LeftMSpeed and RightMSpeed

I suggest that the lines like

LeftMSpeed = map(joystick[0], 490, 417, 0, 255);

should be changed to

LeftMSpeed = map(LMotor, 490, 417, 0, 255);

That way there will be no danger of different parts of the program using different values.

You have not said if the values in joystick[0] and joystick[2] are the values you expect them to be. Maybe you should be using joystick[0] and joystick[1] ?

...R

Ok Thanks for helping. I changed code as you suggested, but it made no difference. However, I discovered one quirk.

BTW, the serial output shows the numbers exactly as I expect them. They are the same as they are when I run s potentiometer range test on the joysticks.

The quirk is that if I get the left joystick to output exactly 417 I get the left motor going at full speed. Nowhere else does this happen. It is significant because 417 is the lowest number in my mapping LeftMSpeed.

jonawadl:
Ok Thanks for helping. I changed code as you suggested, but it made no difference. However, I discovered one quirk.

I can't help as I can't see the latest version of your program.

If you are getting the correct values by wireless (i.e. the values that are being sent) then there is probably just some silly error in the program.

...R

receiver code

/*
  ---- 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;  //
//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() {

LMotor = joystick[0];
RMotor = joystick[2];

  if (radio.available() ){
    radio.read( joystick, sizeof(joystick) ); // Fetch the data payload
  }
  if (LMotor < 490) { //this is backwards
    LeftMSpeed = map(LMotor, 490, 417, 0, 255); // map Joystick0_X
    digitalWrite(IN1, LOW);
    digitalWrite(IN2, HIGH);
    analogWrite(enA, LeftMSpeed); // PWM signal for left motor speed
    Serial.print("LMotor = ");
    Serial.println(LMotor);
  }
  else if (LMotor > 491 && LMotor < 511) { //Motors will not move with the joystick at center
    digitalWrite(IN1, LOW);
    digitalWrite(IN2, LOW);
  }
  
  else if (LMotor > 510) { //this is forwards
    LeftMSpeed = map(LMotor, 510, 606, 0, 255); 
    //Set Left motor forwards
    digitalWrite(IN1, HIGH);
    digitalWrite(IN2, LOW);
    analogWrite(enA, LeftMSpeed);
    Serial.print("LMotor = ");
    Serial.println(LMotor);
  }
  

  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);
    Serial.print("    RMotor = ");
    Serial.println(RMotor);
  }
  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);
    Serial.print("    RMotor = ");
    Serial.println(RMotor);

  
  }





// for the servo motor    
 servo_pos = map(joystick[7], 0, 1024, 0,255);  
      myServo.write(servo_pos);   
}


========================

Transmitter code

[code]


#include <SPI.h>                      //the communication interface with the modem
#include "RF24.h"                     //the library which helps us to control the radio modem

//define the input pins
int JOYSTICK0_X = A0;  // Throttle input
int TJOYSTICK0_X = A5; // Throttle trim future use
int JOYSTICK0_Y = A3; // Rudder future use
int TJOYSTICK0_Y = A7; // Rudder trim
int JOYSTICK1_X = A1; // Elevator input 
int TJOYSTICK1_X = A4; // elevator trim future use
int JOYSTICK1_Y = A2; // ailerons
int TJOYSTICK1_Y = A6; // aileron trim
int aux_switch0 = 2; // switches as labeled on controler
int aux_switch1 = 3;
int trainer_switch = 4;

//define variable values
int joystick[8];     // array containing 8 joystick readings.


RF24 radio(7,8);                     //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 Arduino.

unsigned long currentMillis;
unsigned long prevMillis;
unsigned long txIntervalMillis = 200; // send once per second

void setup(void){
  pinMode(2, INPUT);
  pinMode(3, INPUT);
  pinMode(4, INPUT);
  radio.begin();                      //it activates the modem.
  radio.openWritingPipe(pipe);        //sets the address of the receiver to which the program will send data.
}

void loop(){
  currentMillis = millis();
  if (currentMillis - prevMillis <= txIntervalMillis){
    radio.write( joystick, sizeof(joystick) );
    prevMillis = millis();
  }
  
  joystick[0] = analogRead(JOYSTICK0_X); // read Throttle input put into array slot 0
  joystick[1] = analogRead(JOYSTICK0_Y); // Rudder to array
  joystick[2] = analogRead(JOYSTICK1_X); // Elevator to array
  joystick[3] = analogRead(JOYSTICK1_Y); //...
  joystick[4] = analogRead(TJOYSTICK0_X);
  joystick[5] = analogRead(TJOYSTICK0_Y);
  joystick[6] = analogRead(TJOYSTICK1_X);
  joystick[7] = analogRead(TJOYSTICK1_Y);

}

[/code]

I wonder should this

LeftMSpeed = map(LMotor, 490, 417, 0, 255); // map Joystick0_X

be

LeftMSpeed = map(LMotor, 417, 490, 0, 255); // map Joystick0_X

And if this was my project I think I would do it like this (not tested)

LeftAdcVal = joystick[0];           // range 0 to 1023
leftMotorVal = leftAdcVal - 513;    // range -511 to +510
        // subtracting 513 (rather than 512) ensures that no later value will exceed 255
if (leftMotorVal < 0) {         // Reverse
    digitalWrite(IN1, LOW);
    digitalWrite(IN2, HIGH);
}
else {                          // Forward
    digitalWrite(IN1, HIGH);
    digitalWrite(IN2, LOW);
}

leftMotorVal = (abs(leftMotorVal)) / 2; // get rid of the negative values and fit to range 0 - 255

if (leftMotorVal <= 10) {       // allow for dead band
    analogWrite(enA, 0);
}
else {
    analogWrite(enA, leftMotorVal);
}

...R

Why are we making a range of 0 to1023?

I know that is the normal range for a joystick pot. My joysticks have a range of 319 - 567 exactly for joystick[0] The rest point as I determined by centering the joystick and checking the pot value is 503. Should I still use 0 - 1023 or should I modify that? Wouldn't I be limiting max motor speed if my joysticks never reach 1023 or max value as described in code?

How far above and below my joystick actual range should I go in the case of me modifying values to suit my joysticks?

I'm working at modifying code. Will report back when I have progress.

jonawadl:
Why are we making a range of 0 to1023?

I know that is the normal range for a joystick pot. My joysticks have a range of 319 - 567

I was not aware of that - if you have determined the max and min values then those are what you should use.

However I wonder how you have the joysticks connected to your Arduino - the range seems very small. Can you make a simple pencil drawing showing the connections and post a photo of the drawing? See this Simple Image Upload Guide

...R

I still need an answer to the above question, but I have something to add to it.

If I do this;

  leftAdcVal = joystick[0];           // range 0 to 1032
  leftMotorVal = leftAdcVal - 513;    // range -511 to +510
  // subtracting 513 (rather than 512) ensures that no later value will exceed 255
  Serial.print("LMotor = ");
  Serial.println(leftMotorVal);

My serial monitor spits out values I would expect given my joystick max and min limits.

LMotor min is -98 LMotor max is 91.

If I change that snippet to this;

  leftAdcVal = joystick[0];           // range 319 to 567
  leftMotorVal = leftAdcVal - 64;    // range -64 to +64
  // subtracting 513 (rather than 512) ensures that no later value will exceed 255
  Serial.print("LMotor = ");
  Serial.println(leftMotorVal);

The serial output falls back to giving a max reading of 540 and min of 352.

I may just be a very slow school teacher trying to learn how to code, but to me math is math and should give me predictable results. What's up with that?

I may just be a very slow school teacher trying to learn how to code, but to me math is math and should give me predictable results. What's up with that?

If you print both the value read from the pot and the value you calculate, I'm sure you'll reach for your dunce hat.

Sorry for posting another question before replying to your previous. I didn't realize we had rolled onto a new page.

Here is a simple drawing of my pots. Connections are as simple as it gets Left post to - right to + middle to signal.

I suspect the limited range is because of the way the pots are mounted in the old radio. It limits travel somewhat so we are not getting full range.

I also added a picture of the back of the radio. There are some extra wires. I want to leave options open for future expansions, but don't want anything else connected while I'm still getting the first stage going.

PaulS:
If you print both the value read from the pot and the value you calculate, I'm sure you'll reach for your dunce hat.

Ya whatever. I'll just go sit in a corner for a bit here.

If your Pot is connected as in your diagram I would expect it to produce ADC values from 0 to 1023 - but maybe the joystick levers cannot move the Pot through its full range.

If the range is from 319 to 567 it would probably simplify the maths if you subtract 319 so that the range is from 0 to 248.

And if you subtract a further 124 (making 443) you will get a symmetrical range from -124 to + 124.

And 124 * 2 is near enough to 255 to make little difference.

...R

Ok, I'm going nuts here. With the code as it is written bellow. I can use the right joystick to control right motor forward works ok. Reverse starts fast then slows down. That's an easy fix. left motor still does nothing.

Serial output is exactly as expected.

/*
  ---- 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);
}

left motor still does nothing.

You have an awful lot of code for (still) having a fundamental problem.

Get rid of most of the code. Just make one motor spin in one direction for 5 seconds. Then spin the other motor the same way, for the same length of time. Then, make each motor spin the other way, for the same length of time. Lock the joysticks and radios away until you can positively control the motors.

it appears that shifting the numbers down so that we have -124 -- + 124 doesn't translate directly as I, and I assume you expected.

I was expecting the enA pin to disregard the - and take just the numbers to feed to the speed controllers. So we are back to mapping for both a forward and a reverse value.

PaulS:
You have an awful lot of code for (still) having a fundamental problem.

Get rid of most of the code. Just make one motor spin in one direction for 5 seconds. Then spin the other motor the same way, for the same length of time. Then, make each motor spin the other way, for the same length of time. Lock the joysticks and radios away until you can positively control the motors.

Paul, That's where I started if you read up on this thread. I broke away from the wireless system and used simple joysticks and a wired connection between the joysticks ==> arduino ==> motor controller ==> motors. Everything worked perfectly. I even added in a servo and a pot and controlled the motion of the servo using the pot. Wired everything worked perfectly.

jonawadl:
Everything worked perfectly. I even added in a servo and a pot and controlled the motion of the servo using the pot. Wired everything worked perfectly.

Then the obvious question is how does the motor control code in the non-working program differ from the working program?

Can you remove the "broken" motor control code and replace it with the working code?

...R

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
      }