Go Down

Topic: PWM not working after detach servos (Read 200 times) previous topic - next topic

xun_yang

As pointed out by http://arduino.cc/en/Reference/ServoDetach, pwm on pin 9 and 10 should be available once I detached the servo. However it doesn't work with my example:

This one has an LED attached to pin 9, and servo attached to pin 4.

Code: [Select]
#include <Servo.h>

Servo srv;
int srvPos=0;

void setup(){
}
void loop(){
  analogWrite(9,100);
  delay(1000);
  analogWrite(9,0);
  runServo();
}

void runServo(){
  srv.attach(4);
  srvPos+=50;
  srvPos=srvPos%360;
  int toWrite;
  if(srvPos>180)toWrite=360-srvPos;
  else toWrite=srvPos;
 
  srv.write(toWrite);
  delay(1000);
  srv.detach();
}


Does anybody know why detach doesn't work here? Thanks!

MarkT

Looking at the code of the Servo library it appears just to inhibit timer
interrupts when a timer is no longer needed for Servo use, not reset its
mode and period for analogWrite().

Either the Servo library or its documentation has a bug that should be reported.
[ I won't respond to messages, use the forum please ]

Go Up