Servo Motor

This library allows you to control RC (hobby) servo motors. Servos have integrated gears and a shaft that can be precisely controlled. Standard servos allow the shaft to be positioned at various angles, usually between 0 and 180 degrees. Continuous rotation servos allow the rotation of the shaft to be set to various speeds. To use, specify #include <servo.h>. You will need to create Servo myservo or similar instance.

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: he number of the pin that the servo is attached to; 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 (uS) to the servo, controlling the shaft accordingly. On a standard servo, this will set the angle of the shaft. On standard servos a parameter value of 1000 is fully counter-clockwise, 2000 is fully clockwise, and 1500 is in the middle.
Syntax servo.writeMicroseconds(int us)
Parameters us: the value of the pulse width in microseconds.
Returns None

read

Description Read the current angle of the servo.
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 pluse width output from the specified pin.
Syntax servo.detach()
Parameters None
Returns None


Sample Program

This sample uses pin 9 as the servo motor output pin, enabling the motor to be run by pushing a switch on the GR-KAEDE board.

#include <Arduino.h>
#include <servo.h> 

#define INTERVAL 50
char g_inc = 10;
unsigned char g_pos = 0;
Servo servo0;

void setup() 
{ 
    pinMode(PIN_SW, INPUT);
    servo0.attach(9);
    servo0.write(g_pos);
} 

void loop() {
    if(digitalRead(PIN_SW) == LOW){
        g_pos = g_pos + g_inc;
        servo0.write(g_pos);
        if(g_pos == 180 | g_pos == 0){
            g_inc = g_inc * -1;
        }
    
    }
    delay(INTERVAL);
} 


Made by Gadget Renesas Project
Contents are CC BY-SA 3.0