Arduino multiple motor driver switch system

I'm building a project for school.

In that project I would like to have one Arduino to control multiple arduino's (up to 4). Those 'slaves' than control an H-Bridge to control 2 motors seperatly.

What I've managed so far is to make the H-Bridge and set 1 one motor to react like I want with the following sketch.

 //const int switchPin = 2;    // switch input
  const int motor1Pin = 3;    // H-bridge leg 1 (pin 2, 1A)
  const int motor2Pin = 4;    // H-bridge leg 2 (pin 7, 2A)
  const int enablePin = 9;    // H-bridge enable pin


 void setup() {
    // set the switch as an input:
    //pinMode(switchPin, INPUT); 
    Serial.begin(9600);
    // set all the other pins you're using as outputs:
    pinMode(motor1Pin, OUTPUT); 
    pinMode(motor2Pin, OUTPUT); 
    pinMode(enablePin, OUTPUT);

    // set enablePin high so that motor can turn on
    digitalWrite(enablePin,   LOW); 
  }

void loop(){
    
      // go 
      digitalWrite(enablePin, HIGH);
      digitalWrite(motor1Pin, HIGH);  // set leg 1 of the H-bridge high
      digitalWrite(motor2Pin, LOW);   // set leg 2 of the H-bridge low
      delay(2000);
      Serial.println("1");
      // break1
      digitalWrite(enablePin, LOW); 
      digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low
      digitalWrite(motor2Pin, LOW);  // set leg 2 of the H-bridge high
      Serial.println("2");
      delay(300);
      
      digitalWrite(enablePin, HIGH); 
      digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low
      digitalWrite(motor2Pin, HIGH);  // set leg 2 of the H-bridge high
      Serial.println("3");
      delay(300);
      
      digitalWrite(enablePin, LOW); 
      digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low
      digitalWrite(motor2Pin, LOW);  // set leg 2 of the H-bridge high
      delay(3000);
      
      digitalWrite(enablePin, HIGH); 
      digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low
      digitalWrite(motor2Pin, HIGH);  // set leg 2 of the H-bridge high
      delay(300);
      
      digitalWrite(enablePin, LOW); 
      digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low
      digitalWrite(motor2Pin, LOW);  // set leg 2 of the H-bridge high
      delay(2000);
      
      digitalWrite(enablePin, LOW); 
      digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low
      digitalWrite(motor2Pin, LOW);  // set leg 2 of the H-bridge high
      delay(300);
      // back 
      digitalWrite(enablePin, HIGH);
      digitalWrite(motor1Pin, LOW);  // set leg 1 of the H-bridge high
      digitalWrite(motor2Pin, HIGH);   // set leg 2 of the H-bridge low
      delay(2000);
      // break2
      digitalWrite(enablePin, LOW); 
      digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low
      digitalWrite(motor2Pin, LOW);  // set leg 2 of the H-bridge high
      delay(10000);
      
    }

I've put this in project guidance to get as much guidance as could be.
To first come up with a way to make my system work with the arduino's. (looked up on I2C, but didn't find a clean easy tutorial, for a beginner like me, alternatives)
Second how to control 2 motors separately of the H-Bridge with the former sketch.

Than I would try program the master arduino, to command arduino 3 and 1 for example to control their motor with the above sketch.

Hope it's clear to everybody what I'm trying to build.

In that project I would like to have one Arduino to control multiple arduino's (up to 4).

That is almost always a mistake and one that beginners are more prone to.
What can 4 arduinos do that one can not? If it is just to control motors then you can control as many motors as you like with just one arduino.

Second how to control 2 motors separately of the H-Bridge with the former sketch.

What do you mean by "separately of the H-Bridge" ?
Do you mean "independently from" ?
Or mean "using two"?

If it is the latter it is just a matter of driving the different pins each H-bridge is connected to.

Remember that all hardware does not have to be on a shield.

Those 'slaves' than control an H-Bridge to control 2 motors seperatly.

I was going to ask the same question. I believe the answer is going to be that his professor want him to prove that he can SYNCHRONIZE the communication between more than one arduino such that one of them controls one pair of the mosfets
and the other controls the other totem pole pair on the opposite side of the motor (other terminal).

In that project I would like to have one Arduino to control multiple arduino's (up to 4).
That is almost always a mistake and one that beginners are more prone to.
What can 4 arduinos do that one can not? If it is just to control motors then you can control as many motors as you like with just one arduino.

So I don't need the H-bridge? And how many motors can be attached to an uno and to still be able to make the motors go both ways.

Second how to control 2 motors separately of the H-Bridge with the former sketch.

What do you mean by "separately of the H-Bridge" ?
Do you mean "independently from" ?
Or mean "using two"?

If it is the latter it is just a matter of driving the different pins each H-bridge is connected to.

Remember that all hardware does not have to be on a shield
[/quote]

So the H-bridge for me is to have the ability to give the motors a two way direction only and that's necessary .

I was going to ask the same question. I believe the answer is going to be that his professor want him to prove that he can SYNCHRONIZE the communication between more than one arduino such that one of them controls one pair of the mosfets
and the other controls the other totem pole pair on the opposite side of the motor (other terminal).

I don't know what your talking about?

I'm really sure it isn't something that complicated I just want to be able to control 8 +- motors in both direction on certain times that I programmed? Whereas I don't know how the sketch would look like, ( I can only base it on my sketch for the moment) And how to I wire the arduino (if it's al possible on one arduino uno)

I'm really sure it isn't something that complicated I just want to be able to control 8 +- motors in both direction on certain times that I programmed? Whereas I don't know how the sketch would look like, ( I can only base it on my sketch for the moment) And how to I wire the arduino (if it's al possible on one arduino uno)

Thank goodness for that. It really would have been nice if you had posted all this information in your initial post instead of beating around the bush. In the future, FYI, the correct way to post is to describe your project objective in it's entirety in your initial post. (like what you just did in your last post) You have already figured out how to control one . You can use more chips or you can get a motor driver with multiple outputs. As far as the sketch for controlling multiple arduino SLAVES (that's the term ) from one MASTER, there are several ways. One of them is I2C. The attached sketches are for an I2C example using two ATtiny85's ( please google it if your not familiar with it) and one Mega to communicate via I2C. One of the ATtiny85s, is the MASTER, the other is a SLAVE as is the Mega. Why am I talking about these chips when you asked about arduinos ? Look at the ATtiny85 PINS diagram. See how pins 0 & 1 are PWM ? Well you can't use 0 if your going to use I2C because that is hardwired as the SDA pin but you can use 1 for the PWM pin and either 3 or 4 for direction. You can use one of these 8-pin $2.00 chips for each of the SLAVE arduinos and you can use your Mega for the master. Four arduinos is a waste of processing power if all you want to do is send a pair of signals (DIR/PWM) . Only one of them is PWM. The other is just HIGH or LOW. The attached example Shows you how to communicate. You will have to get some further help to expand on this scheme to send more than a simple "1" or "0" like this sketch does. You'll need to figure out a way for the Master to tell the SLAVE the speed using integers or some other scheme. The Master doesn't need to send PWM to do that. If you want to use UNOs , the MASTER can send a filtered PWM signal to each SLAVE (RC LOW PASS FILTER) using four PWM output pins and the SLAVES can read it as analog on their analog pins. What could be simpler ?

MegaSlave.ino (465 Bytes)

TrinketMaster.ino (664 Bytes)

TrinketSlave.ino (294 Bytes)

So I don't need the H-bridge?

Yes you do.

And how many motors can be attached to an uno and to still be able to make the motors go both ways.

10 without adding extra outputs like a shift register or port expander.

If it is the latter it is just a matter of driving the different pins each H-bridge is connected to.

Yes.

Try and scaly your pictures so they are not mostly in the room next door.

Hi,

So where I am now...

  1. I can control 6 H-Bridges on one Uno. = 12 motors
  2. I don't have to use the IC2 communication. (see below)
  3. I used the sketch with delay, so the Arduino can't control multiple motors together at the same time. (wich I want, but not all of the time.)
  4. Now I have a dc motor with gears (lens in former picture) but I also have a brushless motor witch I assume isn't possible to control with the H-bridge.

For the communication of my idea to create a multitude of motors acting like a motor orchestra where every motor has it's starting - direction - pausing - stopping in any order. I've found out or a accepted the fact that for now one Arduino will suffice to try this project.
My teacher told me to look more in the way of a sequencer, and for that became much clearer than the serial communication.
Picture below shows what the intentions are of the motors thru time.
We've also made a sketch together to make the lens (1motor) change the way I want with a millis sketch instead of working with delays. I want to know how I can expand that sketch to make it possible for multiple motors to act.
Do I make multiple Enablepin('s) ?

This is the picture of the idea of the sequencer, that I would like to get wired on the arduino and then coded.

Sketch with millis.

 //const int switchPin = 2;    // switch input
  const int motor1Pin = 3;    // H-bridge leg 1 (pin 2, 1A)
  const int motor2Pin = 4;    // H-bridge leg 2 (pin 7, 2A)
  const int enablePin = 9;    // H-bridge enable pin
  long int timer;
  int timeGo = 2000;
  int timeBreak1 = 5000;
  int timeBack = 2000;
  int timeBreak2 = 10000;


 void setup() {
    // set the switch as an input:
    //pinMode(switchPin, INPUT); 

    // set all the other pins you're using as outputs:
    pinMode(motor1Pin, OUTPUT); 
    pinMode(motor2Pin, OUTPUT); 
    pinMode(enablePin, OUTPUT);

    // set enablePin high so that motor can turn on
    digitalWrite(enablePin,   LOW); 
  }
  
  
  void loop() {
    
    timer = millis() % (timeGo + timeBreak1 + timeBack + timeBreak2);
    if (timer > (timeGo + timeBreak1 + timeBack)) { 
    // Break2 
      digitalWrite(enablePin, LOW);
      digitalWrite(motor1Pin, LOW);  // set leg 1 of the H-bridge high
      digitalWrite(motor2Pin, LOW);   // set leg 2 of the H-bridge low
    } else if (timer > (timeGo + timeBreak1))  {
      // BACK
      digitalWrite(enablePin, HIGH); 
      digitalWrite(motor1Pin, HIGH);   // set leg 1 of the H-bridge low
      digitalWrite(motor2Pin, LOW);  // set leg 2 of the H-bridge high
    } else if (timer > (timeGo)) {
      // Break1 
      digitalWrite(enablePin, LOW);
      digitalWrite(motor1Pin, LOW);  // set leg 1 of the H-bridge high
      digitalWrite(motor2Pin, HIGH);   // set leg 2 of the H-bridge low
    } else if (timer > 0) {
      // GO
      digitalWrite(enablePin, HIGH); 
      digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low
      digitalWrite(motor2Pin, HIGH);  // set leg 2 of the H-bridge high
      
    }
  }

Any help!!! is kindly appreciated.

beyondal:

  1. I can control 6 H-Bridges on one Uno. = 12 motors

Just to make sure you have got your sums right:

If you want to drive the motors forwards and reverse, you need one h-bridge per motor.

Some common h-bridge chips provide two h-bridges (one chip drives two motors). Usually, these chips provide separate controls for each motor so they need twice as many pins as a single h-bridge. Make sure you don't confuse the number of driver chips with the number of h-bridges.

  1. I can control 6 H-Bridges on one Uno. = 12 motors

No but you can control 6 H-bridges that is 6 motors with 12 output pins, is that what you meant?

  1. I don't have to use the IC2 communication. (see below)

Correct

  1. I used the sketch with delay, so the Arduino can't control multiple motors together at the same time.

Yes you NEVER use delay if you want things to happen at the same time.

I also have a brushless motor witch I assume isn't possible to control with the H-bridge

Correct.

acting like a motor orchestra where every motor has it's starting - direction - pausing - stopping in any order.

Just hold on there.
Whole new ball game opened up which you did not mention before. This is very annoying, why do you do this?
Just what are you trying to do?
Turning a motor on fir a fixed time is not going to result in the motor having traveled the same distance each time or ending up in the same place.
You haven't even grasped how to drive a motor and now it seems you want to make it do something impossible.

So come clean and say what it is you are doing.

Well what I'm trying to do isn't that clear myself. Let me explain.

The idea is to make a kind of robotic orchestra of movement. Where movement is the composition.
Zo I have dc motors, dc motors with gears (the lens), brushless motor (graphic card fan), laser of cd'rom drive etc..

So I'm trying to make movement in the way I described before.
Thus If you see it like the drawing it should go as:

Motor 1 (lens: thus left or right direction) goes for goes up real quick and stops real quick, than continues opening en does this for a few times. Somewhere in the middle of that process Motor 2 (brushless fan) starts slow and speeds up slowly till reaching full speed instantly stops en reverses. Meanwhile Motor 3 ( CD-Rom drive) that has electrical magnets slides left and right fast continuous for 10 second to then stop and again in the meanwhile a 4th motor starts doing something, etc...

So I limited myself to one arduino for now and the maximum of motors that it's able to drive, I thought mostly 3 wires per Motor, so 12 motors are possible. (maybe try a mega later, first walking then running).
I've already made sketches for solenoids, dc speed change with pwm and reverse-forward for the dc with the gears (lens).
The solenoid and gear are made with the delay sketch and the H-bridge l298.., the dc speed with the basic pwm sketch that already exists.

That's the main idea of the project.

So I have to work with millis that I found out. But how I can do such a thing with making it easy to control so much steps. (is jumping for me) I thought I would be able to find it on my own build I'm trying for a week or two and just got stuck and frustrated.
So that's why I need help..

I'm also facing a deadline, but frankly I don't mind that, It would be cool to see how it could been done and to so get of the idea of the logic of the sketch in general to learn from that.

I thought mostly 3 wires per Motor,

Let me try for the last time.
You only need 2 wires to control a DC motor forwards or back or stop.

You need to program this like the blink without delay example. Each motor will have an on time and an off time. Do not use millis directly but make a not of the value given by millis at the start of an on time then keep checking until it is time to turn it off.
See this for an example:-
http://www.thebox.myzen.co.uk/Tutorial/State_Machine.html

beyondal:
So I have to work with millis that I found out. But how I can do such a thing with making it easy to control so much steps. (is jumping for me) I thought I would be able to find it on my own build I'm trying for a week or two and just got stuck and frustrated.

This is a completely separate problem from how you can drive all these motors.

In order to control this many things independently you need to use a non-blocking approach. This means that your code does not ever stop (block) and wait for something to happen - instead, it checks repeatedly whether anything has happened and handles the things that have happened. In this case, all the activities you're looking at would be controlled on a timed basis. The blink without delay shows you how to run code on timed basis without blocking. That is only a very simple example and your code would be more complex, but it would use the same fundamental approach.

Where you want to control a sequence of actions, use a finite state machine to keep track of the current action so that you can walk through the sequence. A very simple way to implement a finite state machine is to define a number for each state, and use a switch/case statement to execute the code associated with the current state.

Grumpy_Mike:

I thought mostly 3 wires per Motor,

Let me try for the last time.
You only need 2 wires to control a DC motor forwards or back or stop.

You need to program this like the blink without delay example. Each motor will have an on time and an off time. Do not use millis directly but make a not of the value given by millis at the start of an on time then keep checking until it is time to turn it off.
See this for an example:-
http://www.thebox.myzen.co.uk/Tutorial/State_Machine.html

THE BOX - that's what I'm looking for, almost got it completely. Except for this part.

boolean LEDstate1, LEDstate2 = LOW;
void functionGo1(){
if(LEDstate1 == HIGH) {
digitalWrite(13,LOW);
LEDstate1 = LOW;
}
else {
digitalWrite(13,HIGH);
LEDstate1 = HIGH;
}
goTime1 = millis() + nextTime1;
}

This state make the light go off and on. okay

How can this be done for the motor sketch?

in this way?

=>boolean Motorstate1, Motorstat2 = LOW;

void functionGo1(){
if(Motorstate1 == HIGH) {
      digitalWrite(enableLensPin, HIGH); // direction one
      digitalWrite(LensPin1, HIGH);  // set leg 1 of the H-bridge high
      digitalWrite(LensPin2, LOW);   // set leg 2 of the H-bridge low
}
else {
       digitalWrite(enableLensPin, LOW);  // direction two
      digitalWrite(LensPin1, LOW);  // set leg 1 of the H-bridge high
      digitalWrite(LensPin2, HIGH);   // set leg 2 of the H-bridge low
}
goTime1 = millis() + nextTime1;
}
      digitalWrite(enableLensPin, HIGH); // direction one
      digitalWrite(LensPin1, HIGH);  // set leg 1 of the H-bridge high
      digitalWrite(LensPin2, LOW);   // set leg 2 of the H-bridge low

LOOK
You only need 2 wires to control a DC motor forwards or back or stop.
Why, why why are you using an enable AND control of the two legs of the bridge.
YOU DON"T NEED THE ENABLE!!!!
Wire it permanently HIGH and don't bother giving it an arduino pin.

As to the code it will turn the motor forward and reverse with equal times, you want to be able to turn it forward backwards and off each with its own specified period. Therefore your code needs to be a bit more complex. You need another state variable that specifies the motor state and in that code once you decide it is time to change the motor you need to look at this state variable to decide if to turn it on forward , on backwards or stop ( stop is both legs of the h-bridge being the same logic level ). When you have done that set the 'goTime1' to the time when the next change should occur. This will be different for each state and so you do it directly after doing the motor control bit. That means you have three values of 'nextTime1' say call them nextTime1off, nextTime1forward, nextTime1reverse.

Well the dc control I have from Physical computing ITP I labs => http://itp.nyu.edu/physcomp/Labs/DCMotorConrtrol
uses the code with the enable pin, I don't see how to do it otherwise?

Then following is a start at what I think is the wright way to get it working. But lots of things are still unclear to me.
And this is just to look if I'm doing it in the correct logic.

boolean MOTORstate1, MOTORstate2, MOTORstate3, MOTORstate4, MOTORstate5 = LOW;

const int LensPin1 = 3; // H-bridge leg 1 (pin 2,1A)
const int LensPin2 = 4; // H-bridge leg 2 (pin 7,2A)
const int enableLensPin = 9; // H-bridge enable pin
const int Fanpin1 = 8;



int nextTimeLeft1 = 300; //doe dit elke 300ms  = do this every 300ms
int nextTimeRight1 = 150; //doe dit elke 150ms

int nextTimeLeft2 = 10000; // doe dit elke 10s
int nextTimeRight2 = 5000; //doe dit elke 5s

int nextTimeLeft3 = 1000; // doe dit elke 1s
int nextTimeRight3 = 500; //doe dit elke 5ms

int nextTimeLeft4 = 800000; // doe dit elke 800s
int nextTimeRight4 = 50000; //doe dit elke 50s

int nextTimeLeft5 = 359; // doe dit elke 359ms
int nextTimeRight5 = 1129; //doe dit elke 1129ms


int nextTimeSpin1 = 20000;

long int 
goTimeLens_Action1,
goTimeLens_Action2, 
goTimeLens_Action3,
goTimeFan_Action1;


void setup(){
  
// moet dit?   Serial.begin(9600);  = is this necessary 
  pinMode(LensPin1, OUTPUT);
  pinMode(LensPin2, OUTPUT);
  pinMode(enableLensPin, OUTPUT);
   // set enablePin high so that motor can turn on
  digitalWrite(enableLensPin, HIGH);
  

goTimeLens_Action1 = millis();
goTimeLens_Action2 = millis();
goTimeLens_Action3 = millis();
goTimeFan_Action1 = millis();


}

void loop(){
  
  if (millis() >= goTimeLens_Action1) functionGo1();
  if (millis() >= goTimeLens_Action2) functionGo2();
  if (millis() >= goTimeLens_Action3) functionGo3();
  
  if (millis() >= goTimeFan_Action1) functionGoSpin();

}


void functionGo1(){
  
  if(Motorstate1 == HIGH) {
    digitalWrite(LensPin1, HIGH); // TURN  RIGHT?! example?!
  }
  goTimelens_Action1 = millis() + nextTimeLeft2; //THEN TURN LEFT?!
}

void functionGo2(){
  
  if(Motorstate2 == LOW) {
    digitalWrite(LensPin2,LOW); // TURN LEFT?!
  }
  goTimelens_Action1 = millis() + nextTimeRight3; //THEN TURN RIGHT?!
}


void functionGo3(){
  
  if(Motorstate3 == LOW) {
    digitalWrite(LensPin3,LOW); // STOP
  }
  goTimelens_Action1 = millis() + nextTimeLeft1; //STOP!?
}

void functionGoSpin(){
  
  if(Motorstate4 == HIGH) {
    digitalWrite(Fanpin1, HIGH); // SPIN
  }
  goTimeFan_Action1 = millis() + nextTimeSpin1;
   
}

Well the dc control I have from Physical computing ITP I labs

Oh god not them again, they have a very odd view of electronics and very detailed and bad web pages.

I don't see how to do it otherwise?

Simply wire the enable pin permanently high. It does need to be connected to an arduino. Then if both sides of the H-bridge are driven to the same level the motor is off, If driven to different levels it is ON, and the state of those levels determine the direction.

The code looks better although each variable is initialized with millis in the setup. This means that as soon as it starts ALL the functions trigger. Once they have triggered for the first time the next time they will be spaced.

goTimeLens_Action1 = millis() + // first Time delay to go here

By adjusting the time offset you can have different on and off times

void functionGo1(){
  
  if(Motorstate1 == HIGH) {
    digitalWrite(LensPin1, HIGH); // TURN  RIGHT?! example?!
    goTimelens_Action1 = millis() + nextTimeOff2; // time for the motor to be on
  }
 else {
  // add the turn motor off action here
  goTimelens_Action1 = millis() + nextTimeToBeOn; //next time for it to be on or change direction
}
}

Oh god not them again, they have a very odd view of electronics and very detailed and bad web pages.

Can you suggest where I can find correct tutorials of driving 3fase brushless, normal dc, dc geared motors and solenoids.

I'll post my new script/sketch as soon as I get it somewhat further than it is now.

Something similar: I also have a lcd mobile phone screen that when power with 3.7v turns on the backlight of the screen. I would like to toggle that too, I looked up to the blink without delay sketch for this but this doesn't work.
On/Off relais isn't that build in the arduino?

Buy an ESC (brushless) from Hobbyking and use the Servo librsry.
DONE. Do you understand?

Buy an ESC (brushless) from Hobbyking and use the Servo librsry.
DONE. Do you understand?

I do understand, but no. I'm working with found objects or salvaged objects for my purpose and that's a choice I've made.
And just for the aesthetic point of view those things are awfully ugly ^^.

But I would be content if I could get the 3phase brushless motor to go on and of alone.

I direct you to this post more than a year ago.
http://forum.arduino.cc/index.php?topic=160596.0
Notice how the OP's post suddenly stopped as soon as he asked basically the same thing you just asked.
Since then there have been a couple of links to do that:

BLDC 3-PHASE MOTOR CONTROL.jpg

AN857.pdf (573 KB)