Developer World
Spresense Arduino Library v3.2.0-77d75a4
Servo Class Reference

#include <Servo.h>

Public Member Functions

 Servo ()
 Construct a new Servo instance. More...
 
 ~Servo ()
 Destructor a Servo instance. More...
 
bool attach (uint8_t pin, uint16_t min_pulse_width=SERVO_MIN_PULSE_WIDTH, uint16_t max_pulse_width=SERVO_MAX_PULSE_WIDTH, uint16_t min_angle=SERVO_MIN_ANGLE, uint16_t max_angle=SERVO_MAX_ANGLE)
 Associate this instance with a servomotor whose input is connected to pin. More...
 
bool detach ()
 Stop driving the servo pulse train. More...
 
void write (uint16_t angle)
 Set the servomotor target angle. More...
 
void writeMicroseconds (uint16_t pulse_width)
 Set the pulse width, in microseconds. More...
 
uint16_t read () const
 
bool attached () const
 Check if this instance is attached to a servo. More...
 
uint8_t attachedPin () const
 Get the pin this instance is attached to. More...
 

Detailed Description

Class for interfacing with RC servomotors.

Constructor & Destructor Documentation

◆ Servo()

Servo::Servo ( )

Construct a new Servo instance.

The new instance will not be attached to any pin.

◆ ~Servo()

Servo::~Servo ( )

Destructor a Servo instance.

The instance will be detached if it is attached to any pin.

Member Function Documentation

◆ attach()

bool Servo::attach ( uint8_t  pin,
uint16_t  min_pulse_width = SERVO_MIN_PULSE_WIDTH,
uint16_t  max_pulse_width = SERVO_MAX_PULSE_WIDTH,
uint16_t  min_angle = SERVO_MIN_ANGLE,
uint16_t  max_angle = SERVO_MAX_ANGLE 
)

Associate this instance with a servomotor whose input is connected to pin.

If this instance is already attached to a pin, it will be detached before being attached to the new pin. This function doesn't detach any interrupt attached with the pin's timer channel.

Parameters
pinPin connected to the servo pulse wave input. This pin must be capable of PWM output.
min_pulse_widthMinimum pulse width to write to pin, in microseconds. This will be associated with a min_angle degree angle. Defaults to SERVO_DEFAULT_MIN_PW = 544.
max_pulse_widthMaximum pulse width to write to pin, in microseconds. This will be associated with a max_angle degree angle. Defaults to SERVO_DEFAULT_MAX_PW = 2400.
min_angleTarget angle (in degrees) associated with min_pulse_width. Defaults to SERVO_DEFAULT_MIN_ANGLE = 0.
max_angleTarget angle (in degrees) associated with max_pulse_width. Defaults to SERVO_DEFAULT_MAX_ANGLE = 180.

@sideeffect May set pinMode(pin, PWM).

Returns
true if successful, false when pin doesn't support PWM.

◆ detach()

bool Servo::detach ( )

Stop driving the servo pulse train.

If not currently attached to a motor, this function has no effect.

Returns
true if this call did anything, false otherwise.

◆ write()

void Servo::write ( uint16_t  angle)

Set the servomotor target angle.

Parameters
angleTarget angle, in degrees. If the target angle is outside the range specified at attach() time, it will be clamped to lie in that range.
See also
Servo::attach()

◆ writeMicroseconds()

void Servo::writeMicroseconds ( uint16_t  pulse_width)

Set the pulse width, in microseconds.

Parameters
pulse_widthPulse width to send to the servomotor, in microseconds. If outside of the range specified at attach() time, it is clamped to lie in that range.
See also
Servo::attach()

◆ read()

uint16_t Servo::read ( ) const

Get the servomotor's target angle, in degrees. This will lie inside the range specified at attach() time.

See also
Servo::attach()

◆ attached()

bool Servo::attached ( ) const
inline

Check if this instance is attached to a servo.

Returns
true if this instance is attached to a servo, false otherwise.
See also
Servo::attachedPin()

◆ attachedPin()

uint8_t Servo::attachedPin ( ) const
inline

Get the pin this instance is attached to.

Returns
Pin number if currently attached to a pin, NOT_ATTACHED otherwise.
See also
Servo::attach()

The documentation for this class was generated from the following file: