I need help with my project code.

Hello everyone. I need help with the codes. There are two engines in my boat. I check these with two separate drivers (BTS7960). I use two driving modes on my boat and replace these driving modes with the switch connected to the controller. In the first driving mode, I control the direction of the two motors with the right joystick. no problem in this section, it works fine. In the second driving mode, I wrote code to control an engine with the left and right joystick. However, with the right and left joysticks, I cannot control the right and left motors at the same time. Can you help with the code?

Part of the code that may be wrong.

//ARDUİNO MEGA 2560
 if (leftswitch == HIGH) {
      //LEFTSWİTCH OPEN, SECOND DRIVING MODE ACTIVE.
      // TWO JOYSTİC DRIVING MODE
      if (yAxis < 495) {
        //RIGHT MOTOR BACKWARD
        rightbackward  = map(yAxis, 495, 0, 0, 255);
      }
      else if (xAxis < 495) {
        //LEFT MOTOR BACKWARD
        leftbackward   = map(xAxis, 495, 0, 0, 255);
      }
      else if (yAxis > 520) {
        //RİGHT MOTOR FORWARD
        rightforward  = map(yAxis, 520, 1023, 0, 255);

      }
      else if (xAxis > 520) {
        //LEFT MOTOR FORWARD
        leftforward  = map(xAxis, 520, 1023, 0, 255);
      }
      else {
        leftforward  = 0;
        leftbackward = 0;
        rightforward = 0;
        rightbackward = 0;

      }
    }


    //MOTOR NOİSE REDUCTİON

    if (leftforward < 10) {
      leftforward = 0;
    }
    if (leftbackward < 10) {
      leftbackward = 0;
    }
    if (rightforward < 10) {
      rightforward = 0;
    }
    if (rightbackward < 10) {
      rightbackward = 0;
    }

    analogWrite(left_Lpwm, leftforward);
    analogWrite(left_Rpwm, leftbackward);
    analogWrite(right_Rpwm, rightforward);
    analogWrite(right_Lpwm, rightbackward)
   
  }
  }

This is the code I used on my boat.

//FISH BAİT BOAT WITH TWO DRIVING MODES
// ARDUİNO MEGA 2560
#include <nRF24L01.h>
#include <printf.h>
#include <RF24.h>
#include <RF24_config.h>

#include <Servo.h>
Servo myservo;

#define CE_PIN 9
#define CSN_PIN 53

const uint64_t pipe = 0xE8E8F0F0E1LL;

#define left_Lpwm 2   //left motor LPWM      
#define left_Rpwm 3   //Left motor RPWM       
#define right_Lpwm 4   //Right motor LPWM       
#define right_Rpwm 5  // Right motor LPWM

RF24 radio(CE_PIN, CSN_PIN);

int joystick[10];
int leftforward   = 0;
int leftbackward  = 0;
int rightforward  = 0;
int rightbackward = 0;
int  xAxis, yAxis, zAxis, z0Axis;

int butonright;
int butonleft;
int leftswitch;

void setup()
{
  myservo.attach(6);
  pinMode(left_Lpwm, OUTPUT);
  pinMode(left_Rpwm, OUTPUT);
  pinMode(right_Lpwm, OUTPUT);
  pinMode(right_Rpwm, OUTPUT);
  Serial.begin(115200);
  radio.begin();
  radio.openReadingPipe(1, pipe);
  radio.startListening();
}

void loop()
{
  if ( radio.available() )
  {
    radio.read( joystick, sizeof(joystick) );
    xAxis  = joystick[0];
    yAxis  = joystick[1];
    zAxis  = joystick[2];
    z0Axis = joystick[3];


    int butonright  = joystick[4];
    int butonleft   = joystick[5];
    int leftswitch  = joystick[6];


    if (butonleft == HIGH) {
      myservo.write(25); //Bait Release
    }
    else if (butonright == HIGH) {
      myservo.write(170); //Bait Release
    }
    else {
      myservo.write(90);
    }

    if (leftswitch == LOW) {
      // FIRST DRIVE MODE ACTIVE
      // TWO MOTOR CONTROL WITH RIGHT JOISTIC Y AND Z AXİS
      if (yAxis < 495) {
        //TWO MOTOR BACKWARD
        leftbackward  = map(yAxis, 495, 0, 0, 255);
        rightbackward  = map(yAxis, 495, 0, 0, 255);
      }
      else if (yAxis > 520) {
        //TWO MOTOR FORWARD
        rightforward  = map(yAxis, 520, 1023, 0, 255);
        leftforward   = map(yAxis, 520, 1023, 0, 255);

      }
      else {
        leftforward   = 0;
        leftbackward  = 0;
        rightforward  = 0;
        rightbackward = 0;
      }
      if (zAxis < 495) {
        //TURN LEFT
        int xMapped = map(zAxis, 495, 0, 0, 255);
        leftforward = leftforward - xMapped;
        rightforward = rightforward + xMapped;
        if (leftforward < 0) {
          leftforward = 0;
        }
        if (rightforward > 255) {
          rightforward = 255;
        }
      }
      if (zAxis > 520) {
        //TURN RİGHT
        int xMapped = map(zAxis, 520, 1023, 0, 255);
        leftforward = leftforward + xMapped;
        rightforward = rightforward - xMapped;
        if (leftforward > 255) {
          leftforward = 255;
        }
        if (rightforward < 0) {
          rightforward = 0;
        }
      }
    }
    if (leftswitch == HIGH) {
      //LEFTSWİTCH OPEN, SECOND DRIVING MODE ACTIVE.
      // TWO JOYSTİC DRIVING MODE
      if (yAxis < 495) {
        //RIGHT MOTOR BACKWARD
        rightbackward  = map(yAxis, 495, 0, 0, 255);
      }
      else if (xAxis < 495) {
        //LEFT MOTOR BACKWARD
        leftbackward   = map(xAxis, 495, 0, 0, 255);
      }
      else if (yAxis > 520) {
        //RİGHT MOTOR FORWARD
        rightforward  = map(yAxis, 520, 1023, 0, 255);

      }
      else if (xAxis > 520) {
        //LEFT MOTOR FORWARD
        leftforward  = map(xAxis, 520, 1023, 0, 255);
      }
      else {
        leftforward  = 0;
        leftbackward = 0;
        rightforward = 0;
        rightbackward = 0;

      }
    }


    //MOTOR NOİSE REDUCTİON

    if (leftforward < 10) {
      leftforward = 0;
    }
    if (leftbackward < 10) {
      leftbackward = 0;
    }
    if (rightforward < 10) {
      rightforward = 0;
    }
    if (rightbackward < 10) {
      rightbackward = 0;
    }

    analogWrite(left_Lpwm, leftforward);
    analogWrite(left_Rpwm, leftbackward);
    analogWrite(right_Rpwm, rightforward);
    analogWrite(right_Lpwm, rightbackward);


    Serial.print("X = ");
    Serial.print(xAxis);
    Serial.print(" Y = ");
    Serial.print(yAxis);
    Serial.print(" Z = ");
    Serial.print(zAxis);
    Serial.print(" Z0 = ");
    Serial.print(z0Axis);
    Serial.print(" BUTON RIGHT = ");
    Serial.print(joystick[4]);
    Serial.print(" BUTON LEFT = ");
    Serial.print(joystick[5]);
    Serial.print(" LEFT SWİTCH = ");
    Serial.println(joystick[6]);

  }
}

On most Arduinos pins 2 and 4 are not PWM enabled.

wildbill:
On most Arduinos pins 2 and 4 are not PWM enabled.

Arduino Mega 2560 ?

jackal_949:
Arduino Mega 2560 ?

What does the datasheet say?

gfvalvo:
What does the datasheet say?

I do not know english. I'm using google translate. I looked at the pictures I found on the internet. Which pins should I use?

" Arduino Mega 2 - 13, 44 - 46 490 Hz (pins 4 and 13: 980 Hz)"

jackal_949:
analogWrite() - Arduino Reference

" Arduino Mega 2 - 13, 44 - 46 490 Hz (pins 4 and 13: 980 Hz)"

Looks like you're ok on pins then.

I don't understand how it works but it looks to me as though there is potential for data to be remembered too long.

If you're running in mode two the if else structure means that you only use what one axis is telling you and in a specific order at that. If the y axis is less than 495, you set rightbackward. But then it never reacts to what the x axis is doing.

I think you need to apply the settings independently.

Also, I suspect that you can end up in a situation where both forward and backward are set, which doesn't sound good.

wildbill:
I don't understand how it works but it looks to me as though there is potential for data to be remembered too long.

If you're running in mode two the if else structure means that you only use what one axis is telling you and in a specific order at that. If the y axis is less than 495, you set rightbackward. But then it never reacts to what the x axis is doing.

I think you need to apply the settings independently.

Also, I suspect that you can end up in a situation where both forward and backward are set, which doesn't sound good.

my boat model is catamaran. There is an engine on the right and left. Using the right and left motors separately gives me more precision. Because I made the propellers, the shaft system and all the mechanical parts I can think of with my own hands.
"I think you need to apply the settings independently." how can I do that?
"Also, I suspect that you can end up in a situation where both forward and backward are set, which doesn't sound good."
Both the forward and reverse adjustment can be used to rotate the boat where it is on the reverse axis. How can I change the if and else if structure? Two-axis information comes simultaneously, but I cannot process it.

I still don't understand. I would expect that each engine can go forward or backwards, not both.

However, assuming that's not true, this will at least read both axes:

    if (leftswitch == HIGH)
    {
      //LEFTSWİTCH OPEN, SECOND DRIVING MODE ACTIVE.
      // TWO JOYSTİC DRIVING MODE
      if (yAxis < 495)
      {
        //RIGHT MOTOR BACKWARD
        rightbackward  = map(yAxis, 495, 0, 0, 255);
      }
      else if (yAxis > 520)
      {
        //RİGHT MOTOR FORWARD
        rightforward  = map(yAxis, 520, 1023, 0, 255);
      }
      else
      {
        rightforward = 0;
        rightbackward = 0;
      }

      if (xAxis < 495)
      {
        //LEFT MOTOR BACKWARD
        leftbackward   = map(xAxis, 495, 0, 0, 255);
      }
      else if (xAxis > 520)
      {
        //LEFT MOTOR FORWARD
        leftforward  = map(xAxis, 520, 1023, 0, 255);
      }
      else
      {
        leftforward  = 0;
        leftbackward = 0;
      }
    }

wildbill:
I still don't understand. I would expect that each engine can go forward or backwards, not both.

However, assuming that's not true, this will at least read both axes:

    if (leftswitch == HIGH)

{
      //LEFTSWİTCH OPEN, SECOND DRIVING MODE ACTIVE.
      // TWO JOYSTİC DRIVING MODE
      if (yAxis < 495)
      {
        //RIGHT MOTOR BACKWARD
        rightbackward  = map(yAxis, 495, 0, 0, 255);
      }
      else if (yAxis > 520)
      {
        //RİGHT MOTOR FORWARD
        rightforward  = map(yAxis, 520, 1023, 0, 255);
      }
      else
      {
        rightforward = 0;
        rightbackward = 0;
      }

if (xAxis < 495)
      {
        //LEFT MOTOR BACKWARD
        leftbackward  = map(xAxis, 495, 0, 0, 255);
      }
      else if (xAxis > 520)
      {
        //LEFT MOTOR FORWARD
        leftforward  = map(xAxis, 520, 1023, 0, 255);
      }
      else
      {
        leftforward  = 0;
        leftbackward = 0;
      }
    }

Thank you so much. This solved my problem. Now I can use both axes at the same time.