Go Down

### Topic: How to control 2 motors by modifying Sparkfun examples Circuit_12 (Read 1 time)previous topic - next topic

#### jiapei100

##### Mar 18, 2013, 02:08 am

Hi, sorry for this naive question. New to Arduino.
The code of Sparkfun example Circuit_12 is quoted here:

Code: [Select]
`// We'll be controlling the motor from pin 9.// This must be one of the PWM-capable pins.const int motorPin = 9;void setup(){  // Set up the motor pin to be an output:  pinMode(motorPin, OUTPUT);  // Set up the serial port:  Serial.begin(9600);}void loop(){  // Here we've used comments to disable some of the examples.  // To try different things, uncomment one of the following lines  // and comment the other ones. See the functions below to learn  // what they do and how they work.     serialSpeed();}// This function will let you type a speed into the serial// monitor window. Open the serial monitor using the magnifying-// glass icon at the top right of the Arduino window. Then// type your desired speed into the small text entry bar at the// top of the window and click "Send" or press return. The motor// will then operate at that speed. The valid range is 0 to 255.void serialSpeed(){  int speed;    Serial.println("Type a speed (0-255) into the box above,");  Serial.println("then click [send] or press [return]");  Serial.println();  // Print a blank line  // In order to type out the above message only once,  // we'll run the rest of this function in an infinite loop:  while(true)  // "true" is always true, so this will loop forever.  {    // First we check to see if incoming data is available:      while (Serial.available() > 0)    {      // If it is, we'll use parseInt() to pull out any numbers:            speed = Serial.parseInt();        // Because analogWrite() only works with numbers from      // 0 to 255, we'll be sure the input is in that range:        speed = constrain(speed, 0, 255);            // We'll print out a message to let you know that the      // number was received:            Serial.print("Setting speed to ");      Serial.println(speed);        // And finally, we'll set the speed of the motor!            analogWrite(motorPin, speed);    }  }}`

My question is pretty simple:
I'm now using an additional pin 10, and do a parallel connection (2 routes).
And I simply tested two routes, both work fine. I mean, each route is able to drive 1 single motor.
But, how to code in Arduino, by modifying the code quoted above, to make it work with 2 motors controlled by two pins (pin 9 and pin 10) by already configured 2 routes?

Thank you very much.

Best Regards
Pei
Welcome to Vision Open
http://www.visionopen.com

#### AWOL

#1
##### Mar 18, 2013, 09:02 am
If you want to command each motor separately, you'll have to define some method of qualifying the serial input so that the speed is interpreted correctly for each motor.
This is usually done by sending an extra letter (say 'L' or 'R' for left or right) along with the speed.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

#### jiapei100

#2
##### Mar 18, 2013, 09:34 am

To me, it seems it's still a bit tough.
In C/C++, you can always use thread, each thread takes care of each motor. Namely, if I can have 2 independent loops, that will solve the problem.

However, it seems to me for this Arduino programming, loop() function has already been declared in its own library. There is a single loop running there. Is it possible for the user to add my own loop ? If So, any on-line samples for me to follow?

Cheers
Pei

If you want to command each motor separately, you'll have to define some method of qualifying the serial input so that the speed is interpreted correctly for each motor.
This is usually done by sending an extra letter (say 'L' or 'R' for left or right) along with the speed.
Welcome to Vision Open
http://www.visionopen.com

#### AWOL

#3
##### Mar 18, 2013, 09:36 am
Quote
In C/C++, you can always use thread, each thread takes care of each motor. Namely, if I can have 2 independent loops, that will solve the problem.

This is C/C++, but the problem (as I see it) is not one of threads, but one of a single resource (the serial interface) to get commands to your two motors.
Plenty of people manage it though, so it can't be too hard.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Go Up

Please enter a valid email to subscribe