Arduino with MAX485 module

#define updown1 6
#define updown2 9
#define forwardbackward 10
#define leftright 11

#define in1updown1 4
#define in2updown1 2
#define in1updown2 A0
#define in2updown2 A1
#define in1forwardbackward A2
#define in2forwardbackward A3
#define in1leftright A4
#define in2leftright A5

#define pwmlight 3
#define gripper 5
#define fronttorch 12
#define griptorch 13

#include <Servo.h>
Servo servogripper;

//-------------------------------------------------------------------------------------

char controller;                  // wasd, c=up, v=down, i=slowmode, o=normalmode, p=fastmode, f=fullstop, h=frontlight, j=fronttorch, l=griptorch, r=opengripper, t=closegripper

int goforwardbackward = 0;        // 1=running, 0=stop              
int goleftright = 0;              // 1=running, 0=stop
int goupdown = 0;                 // 1=running, 0=stop
int onfrontlight = 0;             // 0=off, 1=on
int grippermoving = 0;             // 0=stop, 1=opening/closing
int onfronttorch = 0;             // 0=off, 1=on
int ongriptorch = 0;              // 0=off, 1=on
int dir_updown = 1;               // 1=up, -1=down 
int dir_forwardbackward = 1;      // 1=forward, -1=backward
int dir_leftright = 1;            // 1=left, -1=right
int mode = 0;                     // 0=normal, -1=slow, 1=fast+

const long interval = 100;        // time interval [ms] between motor starting and desired speed
int slow = 50;                   // slow mode speed
int sadnormal = 100;                 // "sad" normal mode speed
int happynormal = 120;                 // "happy" normal mode speed
int fast = 140;                   // fast mode speed
int light = 255;                   // lights dutycycle (light/255)

//----------------------------
//---------------------------------------------------------

void setup() {
  Serial.begin(9600);

  pinMode(6, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);

  pinMode(3, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(A0, OUTPUT);
  pinMode(A1, OUTPUT);
  pinMode(A2, OUTPUT);
  pinMode(A3, OUTPUT);
  pinMode(A4, OUTPUT);
  pinMode(A5, OUTPUT);

  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(5, OUTPUT);

  servogripper.attach(gripper);
}
//-------------------------------------------------------------------------------------

void loop() {

  if (Serial.available() > 0) {        // receive input
    controller = Serial.read();
    Serial.println(controller);
  }

  if (controller == 'w') {
    if (goforwardbackward == 0) {
      forward();
    }
    else {
      stopforwardbackward();
    }
  }

  if (controller == 's') {
    if (goforwardbackward == 0) {
      backward();
    }
    else {
      stopforwardbackward();
    }
  }

  if (controller == 'a') {
    if (goleftright == 0) {
      left();
    }
    else {
      stopleftright();
    }
  }

  if (controller == 'd') {
    if (goleftright == 0) {
      right();
    }
    else {
      stopleftright();
    }
  }

  if (controller == 'c') {
    if (goupdown == 0) {
      up();
    }
    else {
      stopupdown();
    }
  }

  if (controller == 'v') {
    if (goupdown == 0) {
      down();
    }
    else {
      stopupdown();
    }
  }

  if (controller == 'f') {
    fullstop();
  }

  if (controller == 'i') {
    mode = -1;
    Serial.println("mode = slow");

    controller = "";
  }

  if (controller == 'o') {
    mode = 0;
    Serial.println("mode = normal");

    controller = "";
  }

  if (controller == 'p') {
    mode = 1;
    Serial.println("mode = fast");

    controller = "";
  }

  if (controller == 'h') {
    if (onfrontlight == 0) {
      analogWrite(pwmlight, light);
      Serial.println("luci dispersive di fronte = ON");
      onfrontlight = 1;
    }
    else {
      analogWrite(pwmlight, 0);
      onfrontlight = 0;
      Serial.println("luci dispersive di fronte = OFF");
    }

  controller = "";
  }

  if (controller == 'r') {
    if (grippermoving == 0) {
      servogripper.write(100);
      Serial.println("gripper opening");
      grippermoving == 1;
    }
    else {
      servogripper.write(90);
      Serial.println("gripper stopped");
      grippermoving == 0;
    }
    
  controller = "";
  }

  if (controller == 't') {
    if (grippermoving == 0) {
      servogripper.write(80);
      Serial.println("gripper closing");
      grippermoving == 1;
    }
    else {
      servogripper.write(90);
      Serial.println("gripper stopped");
      grippermoving == 0;
    }

  controller = "";
  }

  if (controller == 'j') {
    if (onfronttorch == 0) {
      digitalWrite(fronttorch, HIGH);
      Serial.println("luce profondità di fronte = ON");
      onfronttorch = 1;
    }
    else {
      digitalWrite(fronttorch, LOW);
      onfronttorch = 0;
      Serial.println("luce profondità di fronte = OFF");
    }

  controller = "";
  }

  if (controller == 'l') {
    if (ongriptorch == 0) {
      digitalWrite(griptorch, HIGH);
      Serial.println("luce profondità gripper = ON");
      ongriptorch = 1;
    }
    else {
      digitalWrite(griptorch, LOW);
      Serial.println("luce profondità gripper = OFF");
      ongriptorch = 0;
    }

  controller = "";
  }

}
//-------------------------------------------------------------------------------------

void forward() {
  if (dir_forwardbackward == -1) {
    digitalWrite(in1forwardbackward, LOW);
    digitalWrite(in2forwardbackward, HIGH);
    dir_forwardbackward = 1;
  }

  analogWrite(forwardbackward, 255);
  Serial.println("forward");
  goforwardbackward = 1;

  if (mode == 1) {    // fast mode
    delay(interval);
    analogWrite(forwardbackward, fast);
  }

  if (mode == 0) {    // normal mode
    delay(interval);
    analogWrite(forwardbackward, happynormal);
  }

  if (mode == -1) {    // slow mode
    delay(interval);
    analogWrite(forwardbackward, slow);
  }

controller = "";
}

void backward() {
  if (dir_forwardbackward == 1) {
    digitalWrite(in1forwardbackward, HIGH);
    digitalWrite(in2forwardbackward, LOW);
    dir_forwardbackward = -1;
  }

  analogWrite(forwardbackward, 255);
  Serial.println("backward");
  goforwardbackward = 1;

  if (mode == 1) {    // fast mode
    delay(interval);
    analogWrite(forwardbackward, fast);
  }

  if (mode == 0) {    // normal mode
    delay(interval);
    analogWrite(forwardbackward, happynormal);
  }

  if (mode == -1) {    // slow mode
    delay(interval);
    analogWrite(forwardbackward, slow);
  }

controller = "";
}

void left() {
  if (dir_leftright == -1) {
    digitalWrite(in1leftright, LOW);
    digitalWrite(in2leftright, HIGH);
    dir_leftright = 1;
  }

  analogWrite(leftright, 255);
  Serial.println("left");
  goleftright = 1;

  if (mode == 1) {    // fast mode
    delay(interval);
    analogWrite(leftright, fast);
  }

  if (mode == 0) {    // normal mode
    delay(interval);
    analogWrite(leftright, sadnormal);
  }

  if (mode == -1) {    // slow mode
    delay(interval);
    analogWrite(leftright, slow);
  }

controller = "";
}

void right() {
  if (dir_leftright == 1) {
    digitalWrite(in1leftright, HIGH);
    digitalWrite(in2leftright, LOW);
    dir_leftright = -1;
  }

  analogWrite(leftright, 255);
  Serial.println("right");
  goleftright = 1;

  if (mode == 1) {    // fast mode
    delay(interval);
    analogWrite(leftright, fast);
  }

  if (mode == 0) {    // normal mode
    delay(interval);
    analogWrite(leftright, sadnormal);
  }

  if (mode == -1) {    // slow mode
    delay(interval);
    analogWrite(leftright, slow);
  }

controller = "";
}

void up() {
  if (dir_updown == -1) {
    digitalWrite(in1updown1, LOW);
    digitalWrite(in2updown1, HIGH);
    digitalWrite(in1updown2, LOW);
    digitalWrite(in2updown2, HIGH);
    dir_updown = 1;
  }

  analogWrite(updown1, 255);
  analogWrite(updown2, 255);
  Serial.println("up");
  goupdown = 1;

  if (mode == 1) {    // fast mode
    delay(interval);
    analogWrite(updown1, fast);
    analogWrite(updown2, fast);
  }

  if (mode == 0) {    // normal mode
    delay(interval);
    analogWrite(updown1, happynormal);
    analogWrite(updown2, happynormal);
  }

  if (mode == -1) {    // slow mode
    delay(interval);
    analogWrite(updown1, slow);
    analogWrite(updown2, slow);
  }

controller = "";
}

void down() {
  if (dir_updown == 1) {
    digitalWrite(in1updown1, HIGH);
    digitalWrite(in2updown1, LOW);
    digitalWrite(in1updown2, HIGH);
    digitalWrite(in2updown2, LOW);
    dir_updown = -1;
  }

  analogWrite(updown1, 255);
  analogWrite(updown2, 255);
  Serial.println("down");
  goupdown = 1;

  if (mode == 1) {    // fast mode
    delay(interval);
    analogWrite(updown1, fast);
    analogWrite(updown2, fast);
  }

  if (mode == 0) {    // normal mode
    delay(interval);
    analogWrite(updown1, happynormal);
    analogWrite(updown2, happynormal);
  }

  if (mode == -1) {    // slow mode
    delay(interval);
    analogWrite(updown1, slow);
    analogWrite(updown2, slow);
  }

controller = "";
}

void fullstop() {
  analogWrite(updown1, 0);
  analogWrite(updown2, 0);
  analogWrite(forwardbackward, 0);
  analogWrite(leftright, 0);
  Serial.println("fullstop");
  goupdown = 0;
  goforwardbackward = 0;
  goleftright = 0;

  controller = "";
}

void stopforwardbackward() {
  analogWrite(forwardbackward, 0);
  Serial.println("stop forward/backward");
  goforwardbackward = 0;

  controller = "";
} 

void stopleftright() {
  analogWrite(leftright, 0);
  Serial.println("stop left/right");
  goleftright = 0;

  controller = "";
}

void stopupdown() {
  analogWrite(updown1, 0);
  analogWrite(updown2, 0);
  Serial.println("stop up/down");
  goupdown = 0;

  controller = "";
}

This Is the sketch, what do I need to add or modify? Im Sorry but I dont know what half-duplex means...

Furthermore, I dont know what DTR is either.. what PIN is It and on which component is It? How should I connect It?

@baschi777
As you are finding out, what you want to do is not possible with a single RS485 connection or multiple connections.

Note: this is for normal communication between PC and Nano, not for upload.

Have a look at Gammon Forum : Electronics : Microprocessors : RS485 communications

Google broken :wink: half duplex - Google Search

See the schematic of the Nano (you seem to be using a classic Nano). The DTR signal connects to the reset pin of the processor. It's only needed for upload (or if you have a need to reset the board by opening a terminal program like serial monitor).

Thank you so much for everything.

Im using an Arduino Uno R3.
If I upload the sketch beforehand via normal USB and then connect the two RS485 I should not have the need to connect DTR right?

Btw, how do I double up the RS485 connections? Do I need two 40m cables?

As has already been said, you only need DTR if you intend to upload a new sketch or want to reset your UNO remotely using your RS422/RS485 link.

I would suggest you experiment with your submarine drone. 40m of cable can be quite heavy for a small underwater drone to drag about.

I'm not very knowledgeable on the subject of noise on wires but I wonder if you need a simple 4-core + screen cable were a pair of cores are twisted together so you have 2 twisted pairs. There may be some lightweight audio cable available.

As has already been suggested, I would probably look at RS422 rather than RS485.

If it doesn't work with RS485 it also won't work with RS422.

I agree, Rs422 might be better in certain situations, but here it makes no sense to change standard and all components. Both are ok for 40 meters.

Did you ever read my post#6?

Mind to explain?

Currently the code is based on full-duplex and hence it will not work as is with RS485. RS422 is full-duplex and hence I think that it will work.

RS422 is similiar the RS485 and neither are full duplex. In addition, unlike RS485, RS422 only allows one driver on the bus. Neither will work nor will RS232 or RS423

I disagree, RS422 is full duplex. See e.g. the MAX22502E datasheet (https://www.analog.com/media/en/technical-documentation/data-sheets/MAX22502E.pdf); page 13 shows a full duplex setup.

Not relevant for OP's scenario :wink:

The MAX22502 is a RS485/RS422 transceiver. You can create a full duplex system by using the transmitter/receiver pair in a 4 wire system. However, that does not make RS485 or RS422 full duplex, both are defined as half duplex. USB is only half-duplex

The OP could use a 4 wire RS485 system just as easily as a 4 wire RS422 system but the 485 would be preferred.

I guess I misunderstood.
I though that communications needed to be bidirectional

Let's just agree to disagree :wink:

I don't understand what that means but in any case what the OP want's to do is not possible.