9 years, 2 months ago.

suggest any program to control both dc and servo motor ??

hi I have been working with a project which requires dc motor and servo motor I have referred programs form cookbook for both motors and I have dumped the dc motor and servo motor programs seperately. Both motors are running when i do seperate programs for them. but when i combine both programs only dc motor is running..So suggest any program by which i will be able to run both ..

1 Answer

9 years, 2 months ago.

Within each separate program, there's almost certain to be a "while (1)" loop. You need to combine the two loops into one, if you want both motors to spin at the same time. If you just copy the two loops one after the other, then only the first loop will run; the second loop will never execute because the first loop will never exit. You also need to make sure the pin names are different for the two motors. If you copy and paste the code so we can figure out where it's going wrong, I'm pretty sure you'll get more assistance.

Thank you Aaron, In my program only one while 1 loop is there, I want to run these motors sequentially one after another. Please suggest me solution .. My code is ::

float s=1; m.speed(s);

myled = 0; wait(2); myled = 1; m.speed(0); wait(1); for(float p=0; p<1.0; p += 0.1) { myservo = p; wait(0.2); }

posted by Amit Shaha 03 Apr 2014

Servos use pulse widths to change the servo position. Here's a rough sketch I used for an HD-1501MG servo. I cut and pasted different parts from a bigger app, so this may not work as is, but it should give you an idea of what you need to do. This should swing the servo from one end to the other. The key difference is the "servo.period()" and "servo.pulsewidth_us()" calls. Just assigning a float value to the servo won't change it, as it does for DC motors.

#define PULSEWIDTH 20
#define SMALLESTPULSE 500  // These apply to HD-1501MG servo
#define LARGESTPULSE 2100
#define PULSEINC 25  // change this for larger / smaller increments
#define SERVOHOMEPULSE (PULSEINC * 12)  // determined by hand

PwmOut servo(p21);

int main() {
  servo.period( 0.020 );         // servo requires a 20ms period
  servo.pulsewidth_us( width );  // servo home, empirically determined

  while ( 1 ) {
    width += ( PULSEINC * dir );
    if ( width < SMALLESTPULSE ) {
      width = SMALLESTPULSE;
      dir = 1;
    if ( width > LARGESTPULSE ) {
      width = LARGESTPULSE;
      dir = -1;
    servo.pulsewidth_us( width );
    wait( 0.05 );
posted by Aaron Minner 03 Apr 2014