servo detach not working as expected???

I'm attaching, moving and then detaching a servo. I need to detach the servo since it is interfering with the PWM signal I'm sending out on pin 10 via analogWrite.

Per the library docs:

detach()

Description

Detach the Servo variable from its pin. If all Servo variables are detached, then pins 9 and 10 can be used for PWM output with analogWrite().

Here's my code snip:

myservo.attach(3); // attach servo on pin 3 to servo object
myservo.write(85); //set servo to center
delay(200);
myservo.detach(); // detach servo
delay(200);

Entering an integer value for myservo.detach(3); causes the compiler to choke so I'm assuming that using the myservo.detach(); detaches all attached servos?

Only problem is that PWM analogWrites to pins 9 and 10 still do not work after using the detach function.

Any suggestions are kindly appreciated...

Fred

PS: digitalWrites to p9 and p10 work fine.

Why not use different pins for your analogWrite()s?

The Servo library uses the Timer that controls pins 9 and 10 regardless of which pins the servos are connected to.

There is also a ServoTimer2 library - but Timer2 will affect a different pair of PWM pins.

This question nicely illustrates why we always say to people to post a complete program, not a snippet.

...R

Robin,

Using another pair of pins is the obvious "workaround", which was next on the list assuming I can't figure out how to detach the servo library.

I'm not really sure how posting the entire program would enhance my post or contribute more than I've already said. However here is the code using digital writes, in place of analog writes.

#include <IRremote.h>
#include <Servo.h> //servo library
Servo myservo; // create servo object to control servo

int receiverpin = 12;
int in1=6;
int in2=7;
int in3=8;
int in4=9;
int ENA=5;
int ENB=10;

int ABS=100; //PULSE WIDTH SPEED CONTROL

unsigned long RED;
#define A 16736925
#define B 16754775
#define X 16712445
#define C 16720605
#define D 16761405

IRrecv irrecv(receiverpin);
decode_results results;
void _mForward()
{
digitalWrite(ENA,ABS);
digitalWrite(ENB,ABS);
digitalWrite(in1,LOW);
digitalWrite(in2,HIGH);
digitalWrite(in3,LOW);
digitalWrite(in4,HIGH);
Serial.println("go forward!");
}
void _mBack()
{
digitalWrite(ENA,ABS);
digitalWrite(ENB,ABS);
digitalWrite(in1,HIGH);
digitalWrite(in2,LOW);
digitalWrite(in3,HIGH);
digitalWrite(in4,LOW);
Serial.println("go back!");
}
void _mleft()
{
digitalWrite(ENA,HIGH);
digitalWrite(ENB,HIGH);
digitalWrite(in1,HIGH);
digitalWrite(in2,LOW);
digitalWrite(in3,LOW);
digitalWrite(in4,HIGH);
Serial.println("go left!");
}
void _mright()
{
digitalWrite(ENA,HIGH);
digitalWrite(ENB,HIGH);
digitalWrite(in1,LOW);
digitalWrite(in2,HIGH);
digitalWrite(in3,HIGH);
digitalWrite(in4,LOW);
Serial.println("go right!");
}
void _mStop()
{
digitalWrite(ENA,LOW);
digitalWrite(ENB,LOW);
Serial.println("STOP!");
}
void setup() {
// put your setup code here, to run once:

myservo.attach(3); // attach servo on pin 3 to servo object
myservo.write(85); //set servo to center
delay(200);
myservo.detach(); // detach servo
delay(200);

pinMode(in1,OUTPUT);
pinMode(in2,OUTPUT);
pinMode(in3,OUTPUT);
pinMode(in4,OUTPUT);
pinMode(ENA,OUTPUT);
pinMode(ENB,OUTPUT);
pinMode(receiverpin,INPUT);
Serial.begin(9600);
_mStop();
irrecv.enableIRIn();
}
void loop() {
if (irrecv.decode(&results))
{
RED=results.value;
Serial.println(RED);
irrecv.resume();
delay(150);
if(RED==A)
{
_mForward();
}
else if(RED==B)
{
_mBack();
}
else if(RED==C)
{
_mleft();
}
else if(RED==D)
{
_mright();
}

else if(RED==X)
{
_mStop();
}
}
}

PS: I only need to move one pin which as you can see is pin 10.

I'm not really sure how posting the entire program would enhance my post or contribute more than I've already said.

Entering an integer value for myservo.detach(3); causes the compiler to choke

I think that last sentence could do with some enhancement, or at least explanation.

Here's the compiler error msg you get if you send the detach function an integer:

Arduino: 1.6.9 (Windows 7), Board: "Arduino/Genuino Uno"

C:\Users\Fred\AppData\Local\Temp\arduino_modified_sketch_226801_IR_Car_Driving_w_Servo_Center.ino.ino: In function 'void setup()':

_IR_Car_Driving_w_Servo_Center.ino:76: error: no matching function for call to 'Servo::detach(int)'

myservo.detach(3); // detach servo

^

C:\Users\Fred\AppData\Local\Temp\arduino_modified_sketch_226801_IR_Car_Driving_w_Servo_Center.ino.ino:76:17: note: candidate is:

In file included from C:\Users\Fred\AppData\Local\Temp\arduino_modified_sketch_226801_IR_Car_Driving_w_Servo_Center.ino.ino:2:0:

C:\Users\Fred\Documents\Arduino\libraries\Servo\src/Servo.h:100:8: note: void Servo::detach()

void detach();

^

C:\Users\Fred\Documents\Arduino\libraries\Servo\src/Servo.h:100:8: note: candidate expects 0 arguments, 1 provided

exit status 1
no matching function for call to 'Servo::detach(int)'

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

Just including the servo.h library doesn't cause a problem. The problem happens when you attach a servo, therefore one would reason that detaching the servo should re-enable the use of PWM on p9 and p10.

Based on the error msg you can't detach a single servo.

Based on the error msg you can't detach a single servo.

Of course you can. Each instance of the servo class has a detach() method. Call the detach() method for the servo you want to detach.

Of course, until you detach() ALL of them, detaching one is useless.

ftherrmann:
Robin,

Using another pair of pins is the obvious "workaround", which was next on the list assuming I can't figure out how to detach the servo library.

Then just do it. The servo is normally attached in setup() and never detached.

I'm not really sure how posting the entire program would enhance my post or contribute more than I've already said. However here is the code using digital writes, in place of analog writes.

I had assumed you would have read How to use the Forum and would have used the code button </>

so your code looks like this

and is easy to copy to a text editor.

The purpose of seeing the whole program is so that we can see how all the pins etc are used.

...R

note: candidate expects 0 arguments, 1 provided

Massive hint there.

The servo object knows which pin it is attached to; why do you think would need to tell it which pin to detach from?

Forgot that the servo object is an instance... So you're right. There is no need for an argument.

However this still doesn't solve the problem that the myservo1.detach function does NOT re-enable the PWM on p10.

Do you have a solution for this other than moving to another pin?

You have actually brought up another inconsistency. Logically the pin number should be assigned to the servo object upon instantiation, and not when doing the attach. The attach and detach should be opposites of each other, including their acceptance or nonacceptance of an integer argument.

Servo myservo(10); // create servo object to control servo

When does my "Exceeding" more than one post in 5 minutes stop???

"A good C programmer can program C in any language."

When does my "Exceeding" more than one post in 5 minutes stop???

I think it's 100 posts. IMO, that's excessive.

Is there something wrong with your '?' key?

???????

Seems to be working. Didn't it show up on the previous post?????????? :slight_smile:

You have actually brought up another inconsistency. Logically the pin number should be assigned to the servo object upon instantiation, and not when doing the attach. The attach and detach should be opposites of each other, including their acceptance or nonacceptance of an integer argument.

Servo myservo(10); // create servo object to control servo

Only 90 more posts and the 5 minute waiting period drops away...

ftherrmann:
However this still doesn't solve the problem that the myservo1.detach function does NOT re-enable the PWM on p10.

I thought I covered that in Reply #1

In any case why persist with a complex solution when a simple solution (that you were already aware of) was pointed out in Reply #1.

Perhaps you should ask why Americans drive on the wrong side of the road?

...R

Because it's "wrong".

Just pointing out the obvious "workaround" doesn't fix the problem.

With only 10 posts to my credit I'm obviously not an "enforcer' or part of the management here, but I have pointed out valid problems with the use of the creation of a servo object and the use of the attach and detach functions.

As a newbie to the Arduino I was sort of baffled when the servo function broke the PWM on pin 10. Not exactly obvious or what you'd expect.

If the "management" wishes to ignore what I perceive to be an obvious problem that's fine with me. I'll just keep using pin 13 instead of pin 10.

Forgot to address your driving statement... (my period key is still working)

The reason you limeys drive on the correct side of the road is probably related to the same reason you left the EU. :slight_smile:

ftherrmann:
If the "management" wishes to ignore what I perceive to be an obvious problem that's fine with me. I'll just keep using pin 13 instead of pin 10.

I do have some sympathy with this.

Unfortunately the management (as you call them) don't participate in this Forum. i have got tired complaining about that.

...R
PS. While I am in the UK at the moment I am Irish, Ireland has not left the EU and Ireland also drives on the correct side :slight_smile:

If you are implying that the UK was foolish to leave the EU then I agree with you.

ftherrmann:
???????

Seems to be working. Didn't it show up on the previous post?????????? :slight_smile:

You have actually brought up another inconsistency. Logically the pin number should be assigned to the servo object upon instantiation, and not when doing the attach. The attach and detach should be opposites of each other, including their acceptance or nonacceptance of an integer argument.

Servo myservo(10); // create servo object to control servo

Logically, the servo should be detached from the pin it was attached to, not necessarily a pin the object was instantiated with.

The reason we drive on the correct side of the road (do people really use the word "limey"?) is, I believe, because like the Swedes, we were never conquered by Napoleon.
Though that's also true of Japan, I'm guessing that their reason is not the same.

AWOL:
we were never conquered by Napoleon.

Interesting. which side do the Russians drive on?

...R

The Russians probably decided, like the Swedes did in the 1960s, that it was sensible to drive on the same side as everyone else on the contiguous landmass they shared.

More here