Servo Motor (Servo Class)

This library allows the GR-ADZUKI board to control servo motors. The Servo library supports up to 12 motors on the GR-ADZUKI board. To use, download as a library and specify #include <servo.h>. You will need to create Servo servo0; or similar instance. (See the example program.)

attach

Description

Initializes and attaches the Servo output to a pin.

Syntax

servo.attach(int pin)servo.attach(int pin, int min, int max)

Parameters

pin: The number of the pin to which the servo is attached
min: The pulse width, in microseconds, corresponding to the minimum (0-degree) angle on the servo (defaults to 544)
max: The pulse width, in microseconds, corresponding to the maximum (180-degree) angle on the servo (defaults to 2400)

Returns

None

write

Description

Writes a value to the servo to set the shaft angle, controlling the shaft accordingly. The pulse for moving the shaft to that orientation is output from the pin specified in attach.

Syntax

servo.write(int angle)

Parameters

angle: The value of the angle to write to the servo, from 0 to 180

Returns

None

writeMicroseconds

Description

Writes a value in microseconds (µS) to the servo to set the shaft angle, controlling the shaft accordingly. The pulse is sent to the servo motor in 20ms intervals, meaning the high-duration time (µS) per interval.

Syntax

servo.writeMicroseconds(int us)

Parameters

us: The value of the pulse width in microseconds. Range: 1 to 19999. Set to 0 to turn output OFF

Returns

None

read

Description

Read the current angle of the servo (the value passed to the last call to write()). However, the value given in writeMicroseconds cannot be read.

Syntax

int servo.read()

Parameters

None

Returns

pulse width
[us]

attached

Description

Check whether the Servo variable is attached to a pin.

Syntax

bool servo.attached()

Parameters

None

Returns

True if the Servo is attached to pin; false otherwise.

detach

Description

Detach the Servo variable from its pin. This stops the pulse width output from the specified pin.

Syntax

servo.detach()

Parameters

None

Returns

None


Example

This example uses pin 4 as the servo motor output pin, and rotates the motor in steps 10 degrees every 500ms.


        #include <Arduino.h>
        #include <servo.h> 
         
        #define INTERVAL 500
        unsigned char g_pos = 0;
        Servo servo0;
           
        void setup() 
        { 
            servo0.attach(4);
        } 
           
        void loop() {
            servo0.write(g_pos);
            g_pos+=10;
            if(g_pos > 180){
                    g_pos = 0;
            }
            delay(INTERVAL);
        }