I want to change this code according to my joystick shield but how ?  

  RSS

Gulshan
(@gulshan)
Estimable Member
Joined: 1 month ago
Posts: 108
2019-11-30 11:33 am  

Here there is a code by which we can control the 2 dc motors through l298n motor driver by using the joystick module. but unlike the joystick module where the minimum value is 0 and when the joystick is free the value of both x and y is around 512 and also the max value is upto 1023. In my joystick shield module the minimum value is 0 (x,y) when joysick is free the value is 330(x,y) and the max value is 670(for x,y). what values in this code should I change it to make it compatible to my joystick shield module

 

/*  Arduino DC Motor Control - PWM | H-Bridge | L298N
         Example 02 - Arduino Robot Car Control
    by Dejan Nedelkovski, www.HowToMechatronics.com
*/

#define enA 9
#define in1 4
#define in2 5
#define enB 10
#define in3 6
#define in4 7

int motorSpeedA = 0;
int motorSpeedB = 0;

void setup() {
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
}

void loop() {
  int xAxis = analogRead(A0); // Read Joysticks X-axis
  int yAxis = analogRead(A1); // Read Joysticks Y-axis

  // Y-axis used for forward and backward control
  if (yAxis < 470) {
    // Set Motor A backward
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
    // Set Motor B backward
    digitalWrite(in3, HIGH);
    digitalWrite(in4, LOW);
    // Convert the declining Y-axis readings for going backward from 470 to 0 into 0 to 255 value for the PWM signal for increasing the motor speed
    motorSpeedA = map(yAxis, 470, 0, 0, 255);
    motorSpeedB = map(yAxis, 470, 0, 0, 255);
  }
  else if (yAxis > 550) {
    // Set Motor A forward
    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
    // Set Motor B forward
    digitalWrite(in3, LOW);
    digitalWrite(in4, HIGH);
    // Convert the increasing Y-axis readings for going forward from 550 to 1023 into 0 to 255 value for the PWM signal for increasing the motor speed
    motorSpeedA = map(yAxis, 550, 1023, 0, 255);
    motorSpeedB = map(yAxis, 550, 1023, 0, 255);
  }
  // If joystick stays in middle the motors are not moving
  else {
    motorSpeedA = 0;
    motorSpeedB = 0;
  }

  // X-axis used for left and right control
  if (xAxis < 470) {
    // Convert the declining X-axis readings from 470 to 0 into increasing 0 to 255 value
    int xMapped = map(xAxis, 470, 0, 0, 255);
    // Move to left - decrease left motor speed, increase right motor speed
    motorSpeedA = motorSpeedA - xMapped;
    motorSpeedB = motorSpeedB + xMapped;
    // Confine the range from 0 to 255
    if (motorSpeedA < 0) {
      motorSpeedA = 0;
    }
    if (motorSpeedB > 255) {
      motorSpeedB = 255;
    }
  }
  if (xAxis > 550) {
    // Convert the increasing X-axis readings from 550 to 1023 into 0 to 255 value
    int xMapped = map(xAxis, 550, 1023, 0, 255);
    // Move right - decrease right motor speed, increase left motor speed
    motorSpeedA = motorSpeedA + xMapped;
    motorSpeedB = motorSpeedB - xMapped;
    // Confine the range from 0 to 255
    if (motorSpeedA > 255) {
      motorSpeedA = 255;
    }
    if (motorSpeedB < 0) {
      motorSpeedB = 0;
    }
  }
  // Prevent buzzing at low speeds (Adjust according to your motors. My motors couldn't start moving if PWM value was below value of 70)
  if (motorSpeedA < 70) {
    motorSpeedA = 0;
  }
  if (motorSpeedB < 70) {
    motorSpeedB = 0;
  }
  analogWrite(enA, motorSpeedA); // Send PWM signal to motor A
  analogWrite(enB, motorSpeedB); // Send PWM signal to motor B
}

This topic was modified 1 week ago 2 times by Gulshan

Quote
ZeFerby
(@zeferby)
Estimable Member
Joined: 3 months ago
Posts: 131
2019-11-30 10:59 pm  

@gulshan

In the code above, the potentiometers of the joystick give values between MIN=0 and MAX=1023, and the author has choosen to ignore small variations between 0 and 40 around the middle point, giving 512-40=472 and 512+40=552 (roughly, the actual values used being 470 and 550).

So if we #define the minimum and maximum values for the pots, and the ignored range around the middle point, we can edit it like this, and all you need is to change those first 3 #define lines with your own values :

/*  Arduino DC Motor Control - PWM | H-Bridge | L298N
         Example 02 - Arduino Robot Car Control
    by Dejan Nedelkovski, www.HowToMechatronics.com
*/

#define POT_MIN             0
#define POT_MAX             1023
#define POT_NEUTRAL_WIDTH   40

#define POT_MIDDLE  ((POT_MIN + POT_MAX) / 2)
#define POT_NEUTRAL_LOW  (POT_MIDDLE - POT_NEUTRAL_WIDTH)
#define POT_NEUTRAL_HIGH  (POT_MIDDLE + POT_NEUTRAL_WIDTH)

#define enA 9
#define in1 4
#define in2 5
#define enB 10
#define in3 6
#define in4 7

int motorSpeedA = 0;
int motorSpeedB = 0;

void setup() {
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
}

void loop() {
  int xAxis = analogRead(A0); // Read Joysticks X-axis
  int yAxis = analogRead(A1); // Read Joysticks Y-axis

  // Y-axis used for forward and backward control
  if (yAxis < POT_NEUTRAL_LOW) {
    // Set Motor A backward
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
    // Set Motor B backward
    digitalWrite(in3, HIGH);
    digitalWrite(in4, LOW);
    // Convert the declining Y-axis readings for going backward from POT_NEUTRAL_LOW to POT_MIN into 0 to 255 value for the PWM signal for increasing the motor speed
    motorSpeedA = map(yAxis, POT_NEUTRAL_LOW, POT_MIN, 0, 255);
    motorSpeedB = map(yAxis, POT_NEUTRAL_LOW, POT_MIN, 0, 255);
  }
  else if (yAxis > POT_NEUTRAL_HIGH) {
    // Set Motor A forward
    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
    // Set Motor B forward
    digitalWrite(in3, LOW);
    digitalWrite(in4, HIGH);
    // Convert the increasing Y-axis readings for going forward from POT_NEUTRAL_HIGH to POT_MAX into 0 to 255 value for the PWM signal for increasing the motor speed
    motorSpeedA = map(yAxis, POT_NEUTRAL_HIGH, POT_MAX, 0, 255);
    motorSpeedB = map(yAxis, POT_NEUTRAL_HIGH, POT_MAX, 0, 255);
  }
  // If joystick stays in middle the motors are not moving
  else {
    motorSpeedA = 0;
    motorSpeedB = 0;
  }

  // X-axis used for left and right control
  if (xAxis < POT_NEUTRAL_LOW) {
    // Convert the declining X-axis readings from POT_NEUTRAL_LOW to POT_MIN into increasing 0 to 255 value
    int xMapped = map(xAxis, POT_NEUTRAL_LOW, POT_MIN, 0, 255);
    // Move to left - decrease left motor speed, increase right motor speed
    motorSpeedA = motorSpeedA - xMapped;
    motorSpeedB = motorSpeedB + xMapped;
    // Confine the range from 0 to 255
    if (motorSpeedA < 0) {
      motorSpeedA = 0;
    }
    if (motorSpeedB > 255) {
      motorSpeedB = 255;
    }
  }
  if (xAxis > POT_NEUTRAL_HIGH) {
    // Convert the increasing X-axis readings from POT_NEUTRAL_HIGH to POT_MAX into 0 to 255 value
    int xMapped = map(xAxis, POT_NEUTRAL_HIGH, POT_MAX, 0, 255);
    // Move right - decrease right motor speed, increase left motor speed
    motorSpeedA = motorSpeedA + xMapped;
    motorSpeedB = motorSpeedB - xMapped;
    // Confine the range from 0 to 255
    if (motorSpeedA > 255) {
      motorSpeedA = 255;
    }
    if (motorSpeedB < 0) {
      motorSpeedB = 0;
    }
  }
  // Prevent buzzing at low speeds (Adjust according to your motors. My motors couldn't start moving if PWM value was below value of 70)
  if (motorSpeedA < 70) {
    motorSpeedA = 0;
  }
  if (motorSpeedB < 70) {
    motorSpeedB = 0;
  }
  analogWrite(enA, motorSpeedA); // Send PWM signal to motor A
  analogWrite(enB, motorSpeedB); // Send PWM signal to motor B
}

Eric


ReplyQuote
Gulshan
(@gulshan)
Estimable Member
Joined: 1 month ago
Posts: 108
2019-12-01 5:50 am  

@zeferby

Thank you I will try this code tomorrow


ReplyQuote
Gulshan
(@gulshan)
Estimable Member
Joined: 1 month ago
Posts: 108
2019-12-02 5:03 pm  

Ha, Thank you So much it worked..........Ah sorry for troubling you this much but do you think that if this code can be fixed to. I have a rough Idea that maybe All I need to do is to change the  512 which is the mid position and the 0, 460 and the 564, 1023 values.

 

/*  
  L298N Motor Control Demonstration with Joystick
  L298N-Motor-Control-Demo-Joystick.ino
  Demonstrates use of Joystick control with Arduino and L298N Motor Controller
  
  DroneBot Workshop 2017
  

Welcome to the Workshop!


*/   // Motor A int enA = 9; int in1 = 8; int in2 = 7; // Motor B int enB = 3; int in3 = 5; int in4 = 4; // Joystick Input int joyVert = A0; // Vertical   int joyHorz = A1; // Horizontal // Motor Speed Values - Start at zero int MotorSpeed1 = 0; int MotorSpeed2 = 0; // Joystick Values - Start at 512 (middle position) int joyposVert = 512; int joyposHorz = 512;   void setup() {  // Set all the motor control pins to outputs  pinMode(enA, OUTPUT);  pinMode(enB, OUTPUT);  pinMode(in1, OUTPUT);  pinMode(in2, OUTPUT);  pinMode(in3, OUTPUT);  pinMode(in4, OUTPUT);     // Start with motors disabled and direction forward    // Motor A    digitalWrite(enA, LOW);  digitalWrite(in1, HIGH);  digitalWrite(in2, LOW);    // Motor B    digitalWrite(enB, LOW);  digitalWrite(in3, HIGH);  digitalWrite(in4, LOW);   } void loop() {  // Read the Joystick X and Y positions  joyposVert = analogRead(joyVert);  joyposHorz = analogRead(joyHorz);  // Determine if this is a forward or backward motion  // Do this by reading the Verticle Value  // Apply results to MotorSpeed and to Direction  if (joyposVert < 460)  {    // This is Backward    // Set Motor A backward    digitalWrite(in1, LOW);    digitalWrite(in2, HIGH);    // Set Motor B backward    digitalWrite(in3, LOW);    digitalWrite(in4, HIGH);    //Determine Motor Speeds    // As we are going backwards we need to reverse readings    joyposVert = joyposVert - 460; // This produces a negative number    joyposVert = joyposVert * -1;  // Make the number positive    MotorSpeed1 = map(joyposVert, 0, 460, 0, 255);    MotorSpeed2 = map(joyposVert, 0, 460, 0, 255);  }  else if (joyposVert > 564)  {    // This is Forward    // Set Motor A forward    digitalWrite(in1, HIGH);    digitalWrite(in2, LOW);    // Set Motor B forward    digitalWrite(in3, HIGH);    digitalWrite(in4, LOW);    //Determine Motor Speeds    MotorSpeed1 = map(joyposVert, 564, 1023, 0, 255);    MotorSpeed2 = map(joyposVert, 564, 1023, 0, 255);  }  else  {    // This is Stopped    MotorSpeed1 = 0;    MotorSpeed2 = 0;  }    // Now do the steering  // The Horizontal position will "weigh" the motor speed  // Values for each motor  if (joyposHorz < 460)  {    // Move Left    // As we are going left we need to reverse readings    joyposHorz = joyposHorz - 460; // This produces a negative number    joyposHorz = joyposHorz * -1;  // Make the number positive    // Map the number to a value of 255 maximum    joyposHorz = map(joyposHorz, 0, 460, 0, 255);            MotorSpeed1 = MotorSpeed1 - joyposHorz;    MotorSpeed2 = MotorSpeed2 + joyposHorz;    // Don't exceed range of 0-255 for motor speeds    if (MotorSpeed1 < 0)MotorSpeed1 = 0;    if (MotorSpeed2 > 255)MotorSpeed2 = 255;  }  else if (joyposHorz > 564)  {    // Move Right    // Map the number to a value of 255 maximum    joyposHorz = map(joyposHorz, 564, 1023, 0, 255);            MotorSpeed1 = MotorSpeed1 + joyposHorz;    MotorSpeed2 = MotorSpeed2 - joyposHorz;    // Don't exceed range of 0-255 for motor speeds    if (MotorSpeed1 > 255)MotorSpeed1 = 255;    if (MotorSpeed2 < 0)MotorSpeed2 = 0;        }  // Adjust to prevent "buzzing" at very low speed  if (MotorSpeed1 < 8)MotorSpeed1 = 0;  if (MotorSpeed2 < 8)MotorSpeed2 = 0;  // Set the motor speeds  analogWrite(enA, MotorSpeed1);  analogWrite(enB, MotorSpeed2); }

ReplyQuote
ZeFerby
(@zeferby)
Estimable Member
Joined: 3 months ago
Posts: 131
2019-12-02 6:11 pm  

@gulshan

yes this code overall structure is nearly the same and you can use the same technique as for the previous one.

Eric


ReplyQuote
Gulshan
(@gulshan)
Estimable Member
Joined: 1 month ago
Posts: 108
2019-12-03 3:41 am  

@zeferby

I changed the code up to this point but after that how should i approach it or change it.

 ' if (joyposHorz < 460) '

 

 

/*  
  L298N Motor Control Demonstration with Joystick
  L298N-Motor-Control-Demo-Joystick.ino
  Demonstrates use of Joystick control with Arduino and L298N Motor Controller
  
  DroneBot Workshop 2017
  

Welcome to the Workshop!



*/
#define POT_MIN             0
#define POT_MAX             661
#define POT_NEUTRAL_WIDTH   20

#define POT_MIDDLE  ((POT_MIN + POT_MAX) / 2)
#define POT_NEUTRAL_LOW  (POT_MIDDLE - POT_NEUTRAL_WIDTH)
#define POT_NEUTRAL_HIGH  (POT_MIDDLE + POT_NEUTRAL_WIDTH)

  
// Motor A

#define enA = 9;
#define in1 = 8;
#define in2 = 7;

// Motor B

#define enB = 3;
#define in3 = 5;
#define in4 = 4;

// Joystick Input

int joyVert = A0; // Vertical  
int joyHorz = A1; // Horizontal

// Motor Speed Values - Start at zero

int MotorSpeed1 = 0;
int MotorSpeed2 = 0;
  


void setup()

{

  // Set all the motor control pins to outputs

  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
   
  // Start with motors disabled and direction forward
  
  // Motor A
  
  digitalWrite(enA, LOW);
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  
  // Motor B
  
  digitalWrite(enB, LOW);
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
  
}

void loop() {

  // Read the Joystick X and Y positions

  int joyposVert = analogRead(A0); 
  int joyposHorz = analogRead(A1);

  // Determine if this is a forward or backward motion
  // Do this by reading the Verticle Value
  // Apply results to MotorSpeed and to Direction

  if (joyposVert < POT_NEUTRAL_LOW)
  {
    // This is Backward

    // Set Motor A backward

    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);

    // Set Motor B backward

    digitalWrite(in3, HIGH);
    digitalWrite(in4, LOW);

    //Determine Motor Speeds

    // As we are going backwards we need to reverse readings



    MotorSpeed1 = map(joyposVert, POT_NEUTRAL_LOW, POT_MIN, 0, 255);
    MotorSpeed2 = map(joyposVert, POT_NEUTRAL_LOW, POT_MIN, 0, 255);

  }
  else if (joyposVert > POT_NEUTRAL_HIGH)
  {
    // This is Forward

    // Set Motor A forward

    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);

    // Set Motor B forward

    digitalWrite(in3, LOW);
    digitalWrite(in4, HIGH);

    //Determine Motor Speeds

    MotorSpeed1 = map(joyposVert, POT_NEUTRAL_HIGH, POT_MAX, 0, 255);
    MotorSpeed2 = map(joyposVert, POT_NEUTRAL_HIGH, POT_MAX, 0, 255); 

  }
  else
  {
    // This is Stopped

    MotorSpeed1 = 0;
    MotorSpeed2 = 0; 

  }
  
  // Now do the steering
  // The Horizontal position will "weigh" the motor speed
  // Values for each motor

  if (joyposHorz < 460)
  {
    // Move Left

    // As we are going left we need to reverse readings

    joyposHorz = joyposHorz - 460; // This produces a negative number
    joyposHorz = joyposHorz * -1;  // Make the number positive

    // Map the number to a value of 255 maximum

    joyposHorz = map(joyposHorz, 0, 300, 0, 255);
        

    MotorSpeed1 = MotorSpeed1 - joyposHorz;
    MotorSpeed2 = MotorSpeed2 + joyposHorz;

    // Don't exceed range of 0-255 for motor speeds

    if (MotorSpeed1 < 0)MotorSpeed1 = 0;
    if (MotorSpeed2 > 255)MotorSpeed2 = 255;

  }
  else if (joyposHorz > 460)
  {
    // Move Right

    // Map the number to a value of 255 maximum

    joyposHorz = map(joyposHorz, 360, 661, 0, 255);
        

    MotorSpeed1 = MotorSpeed1 + joyposHorz;
    MotorSpeed2 = MotorSpeed2 - joyposHorz;

    // Don't exceed range of 0-255 for motor speeds

    if (MotorSpeed1 > 255)MotorSpeed1 = 255;
    if (MotorSpeed2 < 0)MotorSpeed2 = 0;      

  }


  // Adjust to prevent "buzzing" at very low speed

  if (MotorSpeed1 < 8)MotorSpeed1 = 0;
  if (MotorSpeed2 < 8)MotorSpeed2 = 0;

  // Set the motor speeds

  analogWrite(enA, MotorSpeed1);
  analogWrite(enB, MotorSpeed2);

}


ReplyQuote
ZeFerby
(@zeferby)
Estimable Member
Joined: 3 months ago
Posts: 131
2019-12-03 10:51 am  

@gulshan

You now have a mess there, because of partial edits, mingling your own values with the original ones 😉 

Go back to Bill's original code AND accompanying explanation and try to edit it in 2 stages :

  1. first replace hard-coded values with #define'd ones keeping the original values so you don't get "lost in translation"
  2. only then, introduce your own values, which should actually only be in your #define's at the top

You'll see that the 2 codes from Bill and HowToMechatronics are actually identical, except for two aspects; first they use a different "neutral range", and second :

  • Bill managed the low values of the pots between 0 and POT_NEUTRAL_LOW "by hand", substracting POT_NEUTRAL_LOW and then reversing the negative sign to positive
  • HowToMechatronics used that feature from the map function

Note that the "lower bounds" of either range may be larger or smaller than the "upper bounds" so the map() function may be used to reverse a range of numbers, for example y = map(x, 1, 50, 50, 1);

Otherwise everything is pretty much the same...

 

There remains one hard-coded value in both scripts, that you could (and probably should) replace with a new #define MOTOR_SPEED_MIN , and experiment with for your own motors.  It is  the lowest speed for the motors to avoid buzzing at the end of void loop() : 70 for HowToMechatronics' motors, 8 for Bill's, and probably another value for yours

 

Also beware that we use the same #define'ed values for the two axes of the joystick, so if your joystick has a different behaviour for the X and Y axes (giving readings of 0-661 on one axis and 15-725 on the other for example), then you may want to duplicate/separate all the pots #define's to have for example separate POT_MIN_X and POT_MIN_Y, POT_MAX_X and POT_MAX_Y, etc... available for use in the X axis code and Y axis code respectively.

 

 

Eric


ReplyQuote

Please Login or Register