Pages: [1]   Go Down
Author Topic: Controlling Two Motors Simultaneously  (Read 269 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 46
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Guys,

I am aware that Arduino, like all single core CPU's can only do one thing at a time but what I want to do seems like something that must have been accomplished pre multi-core.

I have to control 15 motors independently but for the sake of this argument we'll focus on just two. I have to control the acceleration, deceleration, the top speed it reaches, the length of time it stays there etc. of each motor independently and at the same time!

At the moment with this following code, I have to wait for 'motor 1' to accelerate and decelerate before 'motor 2' starts but I need them to do it at the same time. What is the best way to do this? Or is it impossible?

Code:
//Initialize Motor 1
int InA1 = 2;
int InB1 = 4;
int PWM1 = 3;

//Initialize Motor 2
int InA2 = 11;
int InB2 = 10;
int PWM2 = 9;

void setup()

{  pinMode (InA1, OUTPUT);
   pinMode (InB1, OUTPUT);
   pinMode (PWM1, OUTPUT);
   pinMode (InA2, OUTPUT);
   pinMode (InB2, OUTPUT);
   pinMode (PWM2, OUTPUT);
   Serial.begin(9600);
   
}
 
void loop ()
{

//MOTOR 1 
 
   //Motor 1 Direction
       digitalWrite(InA1, HIGH);
       digitalWrite(InB1, LOW);
   
   //Motor 1 Acceleration Control
       {for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5){
         analogWrite(PWM1, fadeValue);
         delay(10000); }
       }
       
   //Motor 1 Run Time       
       delay(10000)
       
   //Motor 1 Decceleration Control
       {for(int fadeValue = 0 ; fadeValue <= 255; fadeValue -=5){
         analogWrite(PWM1, fadeValue);
         delay(10000); }
       } 
         
   //Motor 1 Stop Time
       delay(10000)
   }
   


//MOTOR 2       
       
       
   //Motor 2 Direction
       digitalWrite(InA2, HIGH);
       digitalWrite(InB2, LOW);
   
   //Motor 2 Acceleration Control
       {for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5){
         analogWrite(PWM2, fadeValue);
         delay(10000); }
       }
       
   //Motor 2 Run Time       
       delay(10000)
       
   //Motor 2 Decceleration Control
       {for(int fadeValue = 0 ; fadeValue <= 255; fadeValue -=5){
         analogWrite(PWM2, fadeValue);
         delay(10000); }
       } 
         
   //Motor 2 Stop Time
       delay(10000)
   }
       
                                                                         
}   
   
   
   
 

Many thanks,
Pete
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 553
Posts: 46300
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You have far to many { and } in that code, which won't even compile.

You have far to many delay() calls, too. Look at the Blink Without Delay example. Understand how it works, and you'll never use delay() again.

When you really understand that example, you'll see that what you want to do is not at all hard. Not trivial, in that there are lot of things to check on each pass through loop, but not all that hard, either.
Logged

Offline Offline
Full Member
***
Karma: 2
Posts: 212
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

well i think the way to do this, is by using a shared clock signal.
you can set direction and other things for stepper motors and then set clock signal to activate it i think.
for some chips like l297 and l298 you can connect their clock from their pins together (see l297 datasheets), i've not done this but red about it.

it might dough that for highspeed you need some other IC, i'm not into that at all.

oh and you shouldnt drive motors directly from an arduino it will damage the arduino. (use driver stearing ic combo)
Logged

Pages: [1]   Go Up
Jump to: