servo.detach wanting to know how it is used in this piece of code

I have been studying arduino coding for a few days now and found a website that talked about millis and servos. I'm basically delving into lots of different codes, pulling them apart and seeing how they work etc.

The website I found this code on is All together now! | Multi-tasking the Arduino - Part 1 | Adafruit Learning System
Wonderful topic and the code works very nicely and I learned heaps from it.

I used one part of the code to move a servo and took away all the rest of the code and found one part very interesting.

The code that I am looking at is>>>
#include <Servo.h>

class Sweeper
{
Servo servo; // the servo
int pos; // current servo position
int increment; // increment to move for each interval
int updateInterval; // interval between updates
unsigned long lastUpdate; // last update of position

public:
Sweeper(int interval)
{
updateInterval = interval;
increment = 1;
}

void Attach(int pin)
{
servo.attach(pin);
}
void Detach()
{
servo.detach();
}
void Update()
{
if((millis() - lastUpdate) > updateInterval) // time to update
{
lastUpdate = millis();
pos += increment;
servo.write(pos);
Serial.println(pos);
if ((pos >= 80) || (pos <= 0)) // end of sweep
{
// reverse direction
increment = -increment;
}
}
}
};
Sweeper sweeper1(15);
Sweeper sweeper2(15);

void setup()
{
sweeper1.Attach(9);
sweeper2.Attach(5);
}
void loop()
{
sweeper1.Update();
sweeper2.Update();
}

The part of the code I wanted to find out about is...
void Detach()
{
servo.attach(pin);
}
The code still moves the servo when this bit of code is removed, so I just wanted to find out why it is there.

I'm thinking it may be part of a loop that has to detach then re-attach in order for the code to work.
Cheers,
Chris.

The Detach function is not being called in the improperly posted code.

groundFungus:
The Detach function is not being called in the improperly posted code.

groundFungus:
The Detach function is not being called in the improperly posted code.

I apologise to the board for the improperly posted code, will make sure I do it correctly for future posts.

detach() is a just standard function of the Servo.h library. It's not used very often in normal servo coding.

Why whoever wrote the Sweeper class chose to include a public method to call it is anyone's guess. Perhaps just for completeness or they thought they might need it one day.

Steve

slipstick:
detach() is a just standard function of the Servo.h library. It's not used very often in normal servo coding.

Why whoever wrote the Sweeper class chose to include a public method to call it is anyone's guess. Perhaps just for completeness or they thought they might need it one day.

Steve

Cheers steve, thanks for the great explanation and it was very helpful for my learning.
Have a great new year.
Chris.

If servo is detached then reattached it will move to the default 90 degree position when attached.

outsider:
If servo is detached then reattached it will move to the default 90 degree position when attached.

If you write() to the servo before attaching it then it will move to the commanded position when attached

Correct. Save the last commanded position, then write it back before reattaching:

servo.write(posX);
servo.detach();
...
...
servo.write(posX);
servo.attach(9); // or whatever pin

All true but none of that really explains why detach() would be needed in a Sweeper class.

Steve

outsider:
If servo is detached then reattached it will move to the default 90 degree position when attached.

Does it? I thought it retained the last value written.

@AWOL:
Well you were correct again. (Did I detect a smirk?) :stuck_out_tongue: