Why does adding a servo motor to pin 10 affect PWM on pin 45?


this perplexing problem me.  new arduinos.


i have arduino mega adafruit motor shield (http://www.adafruit.com/products/1438) on it.

for now, want 2 devices connected -

1) servo motor want position in usual fashion, point between 0 , 180 degrees.  has own power supply.

2) vibratory motor (in dispensing device) functions @ different speeds, based on voltage (0 - 3 v) applied it.  has own 5 v power supply, separate 1 servo, 5v (eventually) cook it, i'm using pwm via analogwrite() reduce safe level , exert fine control how voltage in 0-3v range applied it.

both devices both controlled serial commands received computer.  it's simple one-way communication protocol:

if send ">s123<" arduino, puts servo @ 123 deg.

if send ">v255<" arduino, calls analogwrite put 5v drain pin, , vibratory motor sees 0v potential , off.

if send ">v102<" arduino, calls analogwrite put 2v drain pin (102 / 255 * 5), , vibratory motor sees 3v potential , runs @ full speed.  ">v153"< gets 2v , slower speed, etc.


both features work separately.  servo moves expected.  potential measured between vibratory motor power supply (+5v) , drain pin (#45) expected.

the problem when have both features in same program.  in case, servo still works fine, no matter value analogwrite( 45, [0-254] ) calls, measured potential 5 v, , analogwrite( 45, 255 ) cuts potential 0 v.

it appears me setting , using servo breaks pwm on pin 45.

the following arduino code functions set voltage vibrating dispensing motor.

after labeled photo illustrate part arrangement , wiring, on off chance it's helpful , exposes mistake i'm making there.

if uncomment out lines related servo ... servo functions properly, voltage vibrating dispenser motor appears or none result.  ">v255<" results in 0v number 0 - 254 results in 5v.

what gives?

thank you.


code: [select]
#include <wire.h>
#include <adafruit_motorshield.h>
#include "utility/adafruit_pwmservodriver.h"
#include <servo.h>
#include <ctype.h>

//adafruit_motorshield afms = adafruit_motorshield();
//adafruit_steppermotor *mystepper = afms.getstepper(200, 2);
//adafruit_dcmotor *mymotor = afms.getmotor(1);
//servo servo1;

void setup() {
  pinmode (45, output);
  analogwrite( 45, 255 ); // ensure v motor off @ startup
 
  serial.begin(115200);
//  afms.begin(); 
//  servo1.attach(10);
}

void loop() {
  char c_start, c_end;
  char command_name, command_data[4];
  int command_data_value;
 
  // read serial port until beginning of command received
  while( 1 ) {
    c_start = serial.read(); delay(2);
    if( c_start == '>' ) break;
  }
 
  // rest of command in format >cddd<
  command_name = serial.read(); delay(2);   
  command_data[0] = serial.read(); delay(2);
  command_data[1] = serial.read(); delay(2);
  command_data[2] = serial.read(); delay(2);
  command_data[3] = '\0';
  c_end = serial.read(); delay(2);

  // command formatted properly?
  if( (c_start == '>') && (c_end == '<') &&
      isalpha(command_name) &&
      isdigit(command_data[0]) &&
      isdigit(command_data[1]) &&
      isdigit(command_data[2])               ) {

    command_data_value = atoi( command_data );
   
    if( command_name == 's' ) {  // command position servo

 //     if( command_data_value < 5 ) servo1.write( 5 );
 //     else if( command_data_value > 175 ) servo1.write( 175 );
 //     else servo1.write( command_data_value );
 
    } else if( command_name == 'v' ) { // command vibratory dispenser

      if( command_data_value < 102 ) analogwrite( 45, 102 );  // safe max of 3 v motor
      else if( command_data_value > 255 ) analogwrite( 45, 255 );
      else analogwrite( 45, command_data_value );

    }
  }
}


the servo library uses 16-bit timers time interrupts output pulses.

on uno there 1 16 bit timer, timer1, , servo library uses that
which means pwm on pins 9 , 10 not available driven timer1.

on mega servo libraries uses timer5, timer1, timer3, timer4.  each timer
drives 12 servos typically timer5 needed on mega, , timer5
generates pwm on pins 44, 45, 46.


Arduino Forum > Using Arduino > Motors, Mechanics, and Power (Moderator: fabioc84) > Why does adding a servo motor to pin 10 affect PWM on pin 45?


arduino

Comments