controlling 40 motors using 1 due and 3 mega

Hello,
I have a project where I need control 40 DC motors to move 20 objects, each DC motor has a reed switch (I'm using satellite dish motor). The position of each motor is calculated by arduino due and then the values are sent through serial communication to 3 mega, the due and mega are connected together with digitial logic converter and the motors are controlled with DC motor driver (H Bridge). This is working good for one motor but I'm sure it will differ for practical operation

The sketches is working fine for both the due and the 3 megas for the serial communication but I have the following questions for stable and reliable operation, I'm going to try it very soon and I don't want any surprises:
1- Is the serial communication is the best method for my case? some times I feel a small electric "shock" on the mega and the due
2- Is it okay to use arduino mega pins no. 50,51,52 and 53 for reed sensor input (INPUT_PULLUP), or these pins are for SPI communications and not suitlabe for input?
3- the mega and the reed switch are connnected with 2 X 1mm cable with maximum lenght of 25 meters,I'm using INPUT_PULLUP, should I use external resistor? and what is the resistor size?
4- The reed switch is normally closed, I'm confuesd to use pullup or pulldown?

Any help will be highly appreciated. Thank you for help in advance :slight_smile:

Note: I removed the repeated lines from H2 to H6 with the line //and so on till H7

This is the motor and the reed switch:

Due

  Serial1.print('

Mega 1

int ExtraReedCounter;
boolean ReedState = HIGH;
unsigned long StartMillis = 0;
unsigned long currentMillis  = 0;
unsigned long interval;

const int H1ENA = 22;
const int H1ENB = 23;
const int H1IN13 = 24;
const int H1IN24 = 25;
const int H1M1ReedPin = 50;
const int H1M2ReedPin = 51;

const int H2ENA = 26;
const int H2ENB = 27;
const int H2IN13 = 28;
const int H2IN24 = 29;
const int H2M1ReedPin = 52;
const int H2M2ReedPin = 53;

// and so on till H7
const int H7ENA = 46;
const int H7ENB = 47;
const int H7IN13 = 48;
const int H7IN24 = 49;
const int H7M1ReedPin = 10;
const int H7M2ReedPin = 11;




int H1M2Pulse;
int H1M1Pulse;
// and so on till H7
int H7M2Pulse;
int H7M1Pulse;

int H1M2Counter;
int H1M1Counter;
// and so on till H7

int H7M2Counter;
int H7M1Counter;



void setup() {
  pinMode(13, OUTPUT);

  pinMode (H1M1ReedPin , INPUT_PULLUP);
  pinMode (H1M2ReedPin , INPUT_PULLUP);

// and so on till H7

  pinMode (H7M1ReedPin , INPUT_PULLUP);
  pinMode (H7M2ReedPin , INPUT_PULLUP);


  pinMode(H1ENA, OUTPUT);
  pinMode(H1ENB, OUTPUT);
  pinMode(H1IN13, OUTPUT);
  pinMode(H1IN24, OUTPUT);

  pinMode(H2ENA, OUTPUT);
  pinMode(H2ENB, OUTPUT);
  pinMode(H2IN13, OUTPUT);
  pinMode(H2IN24, OUTPUT);

  pinMode(H3ENA, OUTPUT);
  pinMode(H3ENB, OUTPUT);
  pinMode(H3IN13, OUTPUT);
  pinMode(H3IN24, OUTPUT);

  pinMode(H4ENA, OUTPUT);
  pinMode(H4ENB, OUTPUT);
  pinMode(H4IN13, OUTPUT);
  pinMode(H4IN24, OUTPUT);

  pinMode(H5ENA, OUTPUT);
  pinMode(H5ENB, OUTPUT);
  pinMode(H5IN13, OUTPUT);
  pinMode(H5IN24, OUTPUT);

  pinMode(H6ENA, OUTPUT);
  pinMode(H6ENB, OUTPUT);
  pinMode(H6IN13, OUTPUT);
  pinMode(H6IN24, OUTPUT);


  pinMode(H7ENA, OUTPUT);
  pinMode(H7ENB, OUTPUT);
  pinMode(H7IN13, OUTPUT);
  pinMode(H7IN24, OUTPUT);



  Serial.begin(9600);
  Serial1.begin(9600);

  delay(50);
}

void loop() {

  while (!Serial1.available()) delay(10);
  if (Serial1.read() == '

); delay(100);
Serial1.print(H1M1Pulse); Serial1.print('\n'); delay(100);
Serial1.print(H1M2Pulse); Serial1.print('\n'); delay(100);
//and so on till H7
Serial1.print(H7M1Pulse); Serial1.print('\n'); delay(100);
Serial1.print(H7M2Pulse); Serial1.print('\n'); delay(100);

Serial2.print('


Mega 1

§DISCOURSE_HOISTED_CODE_1§


); delay(100); //the same as Serial1


  Serial3.print('

Mega 1

§_DISCOURSE_HOISTED_CODE_1_§

); delay(100); //the same as Serial1

delay(50);


Mega 1

§DISCOURSE_HOISTED_CODE_1§


) {
    
    Serial1.read();
    H1M2Pulse  = readInt();
    H1M1Pulse  = readInt();
//and so on till H7
    H7M2Pulse  = readInt();
    H7M1Pulse  = readInt();

  }

  delay(50);
  H1M1Counter = MoveMotor(H1M1Pulse, H1M1Counter, H1ENA , H1IN13, H1IN24, H1M1ReedPin);
  delay(50);
  H1M2Counter = MoveMotor(H1M1Pulse, H1M1Counter, H1ENA , H1IN13, H1IN24, H1M2ReedPin); 
  delay(50);
//and so on till H7
  H7M1Counter = MoveMotor(H7M1Pulse, H7M1Counter, H7ENA , H7IN13, H7IN24, H7M1ReedPin);
  delay(50);
  H7M2Counter = MoveMotor(H7M1Pulse, H7M1Counter, H7ENA , H7IN13, H7IN24, H7M2ReedPin);


  delay(5000);


}


int readInt() {
  while (!Serial1.available()) delay(10);
  int reading = 0;
  int incomingInt = Serial1.read();
  while (incomingInt != '\n') {
    if (incomingInt >= '0' && incomingInt <= '9')
      reading = reading * 10 + (incomingInt - '0');
    else;
    incomingInt = Serial1.read();
  }
  Serial1.flush();
  return reading;
}


int MoveMotor(int Pulse, int Counter, int EN , int IN13, int IN24, int ReedPin) {

  delay(50);


  interval = Pulse - Counter;
  if (interval < 10) interval = interval * 10000;
  else if ((interval >= 10) && ( interval < 50)) interval = interval * 500;
  else  interval = 120000;

  delay(10);

  StartMillis = millis();

  while ( Pulse > Counter) {
    currentMillis = millis();

    if (currentMillis - StartMillis >= interval) {
      digitalWrite(13, LOW);
      Serial.println("Motor Error");
      break;
    }


    digitalWrite(13, HIGH);
    digitalWrite(IN24, LOW);
    digitalWrite(EN, HIGH);
    digitalWrite(IN13, HIGH);



    if (debounceReed(ReedState, ReedPin) == HIGH && ReedState == LOW)
    {
      Counter++;
      //$Serial.print(Counter); Serial.print('\n');

      ReedState = HIGH;
    }
    else if (debounceReed(ReedState, ReedPin) == LOW && ReedState == HIGH)
    {
      ReedState = LOW;
    }

  }



  while (Pulse < Counter) {
    /*     currentMillis = millis();

          if (currentMillis - StartMillis >= interval) {
            digitalWrite(13, LOW);
            Serial.println("Motor Error");
            break;
          }
    */

    digitalWrite(13, HIGH);
    digitalWrite(IN13, LOW);
    digitalWrite(EN, HIGH);
    digitalWrite(IN24, HIGH);

    if (debounceReed(ReedState, ReedPin) == HIGH && ReedState == LOW)
    {
      Counter--;
      // Serial.print(Counter); Serial.print('\n');

      ReedState = HIGH;
    }
    else if (debounceReed(ReedState, ReedPin) == LOW && ReedState == HIGH)
    {
      ReedState = LOW;
    }

  }


  if (Pulse == Counter) {
    digitalWrite(13, LOW);
    digitalWrite(IN24, HIGH);
    digitalWrite(IN13, HIGH);
    digitalWrite(EN, HIGH);
    delay(50);

    digitalWrite(IN24, LOW);
    digitalWrite(IN13, LOW);
    digitalWrite(EN, LOW);

    if (debounceReed(ReedState, ReedPin) == HIGH && ReedState == LOW)
    {
      ExtraReedCounter++;


      Serial.print("number of EXTRA Reed Pulses:  ");
      Serial.println(ExtraReedCounter);

      ReedState = HIGH;
    }
    else if (debounceReed(ReedState, ReedPin) == LOW && ReedState == HIGH)
    {
      ReedState = LOW;
    }
  }

  return Counter;
}



boolean debounceReed(boolean State, int ReedPin)
{
  boolean StateNow = digitalRead(ReedPin);
  if (State != StateNow)
  {
    delay(50);
    StateNow = digitalRead(ReedPin);
  }
  return StateNow;
}

); delay(100);
Serial1.print(H1M1Pulse); Serial1.print('\n'); delay(100);
Serial1.print(H1M2Pulse); Serial1.print('\n'); delay(100);
//and so on till H7
Serial1.print(H7M1Pulse); Serial1.print('\n'); delay(100);
Serial1.print(H7M2Pulse); Serial1.print('\n'); delay(100);

Serial2.print('


Mega 1

§DISCOURSE_HOISTED_CODE_1§


); delay(100); //the same as Serial1


  Serial3.print('

Mega 1

§_DISCOURSE_HOISTED_CODE_1_§

); delay(100); //the same as Serial1

delay(50);


Mega 1

§DISCOURSE_HOISTED_CODE_1§

Referring to your questions ...

{1} Serial will be fine IF it is implemented properly. I don't think what you are doing is very robust. Have a look at the 3rd example in Serial Input Basics.

You can send data in a compatible format with code like this

Serial.print('<'); // start marker
Serial.print(value1);
Serial.print(','); // comma separator
Serial.print(value2);
Serial.println('>'); // end marker

{2} Yes, if you are not using SPI you can use the pins for norma I/O

{3} I'm not sure. The internal pullup is about 30k IIRC. A lower value external resistor may help - try 10k if you have problems with the internal resistor.

{4} The normal state of the switch does not matter. Using a pullup resistor at the Arduino end of the cable ensures there is no risk of a short circuit.

...R

Is the serial communication is the best method for my case?

For any distance over 0.5 meters, serial is the best choice (about the only choice). If TTL serial doesn't work then RS485 might be called for.

some times I feel a small electric "shock" on the mega and the due

That is not good. The voltages normally present in an Arduino, of any sort, are too low to shock you. Are you picking up static from the environment? If the area that you are working in a "staticy" area you should wear a "Wriststat" or some other approved method of keeping yourself grounded.

Is it okay to use arduino mega pins no. 50,51,52 and 53 for reed sensor input

If you are not going to use SPI those pins are perfectly acceptable to use for digital inputs.

maximum lenght of 25 meters,I'm using INPUT_PULLUP, should I use external resistor?

You can try with the internal pullups, but 25 meters of wire is going to pick up a lot of noise so an external pullup will probably be necessary. I would go no lower than 1K for the pullups. Shielded cables and/or twisted pairs can help if the lower pullup doesn't do the job. Terminate the shield only at the Arduino end.

Thank you Robin2 and groundFungus for your help. I appreciate it the most :smiley: