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
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§