1

Topic: I need help with joystick and l298n

Can anyone help me please I want to use two joysticks the first joystick to tell 1 motor to go clockwise when pushed forward and counterclockwise when pushed backward and the second joystick to tell the other motor to go clockwise when pushed left and counterclockwise when pushed right I’m using l298n h bridge the code below controls both motors with one joystick I need two joysticks one for each motor i am using RemoteXY for joysticks My code is below

//////////////////////////////////////////////
//        RemoteXY include library          //
//////////////////////////////////////////////

// RemoteXY select connection mode and include library
#define REMOTEXY_MODE__SOFTSERIAL
#include <SoftwareSerial.h>

#include <RemoteXY.h>

// RemoteXY connection settings
#define REMOTEXY_SERIAL_RX 2
#define REMOTEXY_SERIAL_TX 3
#define REMOTEXY_SERIAL_SPEED 9600


// RemoteXY configurate 
#pragma pack(push, 1)
uint8_t RemoteXY_CONF[] =
  { 255,5,0,0,0,62,0,10,0,0,
  5,48,67,28,29,29,12,24,31,5,
  46,4,29,29,29,12,24,31,2,1,
  39,56,21,5,12,24,31,31,79,78,
  0,79,70,70,0,129,0,25,1,50,
  6,12,82,67,45,88,68,32,67,111,
  110,116,114,111,108,108,101,114,0 };
 
// this structure defines all the variables and events of your control interface
struct {

    // input variables
  int8_t Steering_x; // =-100..100 x-coordinate joystick position
  int8_t Steering_y; // =-100..100 y-coordinate joystick position
  int8_t Acceleration_x; // =-100..100 x-coordinate joystick position
  int8_t Acceleration_y; // =-100..100 y-coordinate joystick position
  uint8_t switch_1; // =1 if switch ON and =0 if OFF

    // other variable
  uint8_t connect_flag;  // =1 if wire connected, else =0

} RemoteXY;
#pragma pack(pop)

/////////////////////////////////////////////
//           END RemoteXY include          //
/////////////////////////////////////////////
/* defined the Steering motor control pins */
#define in1 7
#define in2 6
#define enA 10

/* defined the Acceleration motor control pins */
#define in3 5
#define in4 4
#define enB 9

/* defined the LED pin */
#define PIN_SWITCH_1 13

/* defined two arrays with a list of pins for each motor */
unsigned char SteeringMotor[3] = 
  {in1, in2, enA};
unsigned char AccelerationMotor[3] = 
  {in3, in4, enB};

/*
   speed control of the motor
   motor - pointer to an array of pins
   v - motor speed can be set from -100 to 100
*/
void Wheel1 (unsigned char * motor, int v)
{
  if (v>100) v=100;
  if (v<-100) v=-100;
  if (v>0) {
    digitalWrite(motor[0], HIGH);
    digitalWrite(motor[1], LOW);
    analogWrite(motor[2], v*2.55);
  }
  else if (v<0) {
    digitalWrite(motor[0], LOW);
    digitalWrite(motor[1], HIGH);
    analogWrite(motor[2], (-v)*2.55);
  }
  else {
    digitalWrite(motor[0], LOW);
    digitalWrite(motor[1], LOW);
    analogWrite(motor[2], 0);
  }
}
void Wheel2 (unsigned char * motor, int v)
{
  if (v>100) v=100;
  if (v<-100) v=-100;
  if (v>0) {
    digitalWrite(motor[0], HIGH);
    digitalWrite(motor[1], LOW);
    analogWrite(motor[2], v*2.55);
  }
  else if (v<0) {
    digitalWrite(motor[0], LOW);
    digitalWrite(motor[1], HIGH);
    analogWrite(motor[2], (-v)*2.55);
  }
  else {
    digitalWrite(motor[0], LOW);
    digitalWrite(motor[1], LOW);
    analogWrite(motor[2], 0);
  }
}
void setup()
{
  /* initialization pins */
  pinMode (in1, OUTPUT);
  pinMode (in2, OUTPUT);
  pinMode (in3, OUTPUT);
  pinMode (in4, OUTPUT);
  pinMode (PIN_SWITCH_1, OUTPUT);
 
  /* initialization module RemoteXY */
  RemoteXY_Init ();

}

void loop()
{
  /* event handler module RemoteXY */
  RemoteXY_Handler ();
 
  /* manage LED pin */
  digitalWrite(PIN_SWITCH_1, (RemoteXY.switch_1==0)?LOW:HIGH);

  /* manage the Steering motor */
  Wheel1 (SteeringMotor, RemoteXY.Steering_y - RemoteXY.Steering_x);
  Wheel1 (SteeringMotor, RemoteXY.Steering_x + RemoteXY.Steering_y); 
 
  /* manage the Acceleration motor */
  Wheel2 (AccelerationMotor, RemoteXY.Acceleration_y - RemoteXY.Acceleration_x);
  Wheel2 (AccelerationMotor, RemoteXY.Acceleration_x + RemoteXY.Acceleration_y);
  // TODO you loop code
  // use the RemoteXY structure for data transfer
  // do not call delay()


}

2

Re: I need help with joystick and l298n

Hello,
probably you should describe what your problem is, so that one can concentrate on this looking at your code, instead of searching through it.

The only thing I've found at first glance is the following. You define motor speed to be -100 to +100. And later you multiply the speed with 2.55.
The range of analogWrite is 0 to 255. I'm wondering if this mixed usage of float ( 2.55 ) and int ( v ) might confuse the analogWrite command, as
the type cast to int is missing.

/*
   v - motor speed can be set from -100 to 100
*/
...
    analogWrite(motor[2], v*2.55); 

I haven't tried it, but maybe

analogWrite(motor[2], (int)(v*2.55)); 

could help.
Otherwise use the map() function like this

analogWrite(motor[2], map(v,0,100,0,255); 

rgs