Basic Library
Digital I/O
Analog I/O
Advanced I/O
Time
Math
Trigonometry
Random Numbers
Bits and Bytes
Interrupts
Serial Comm.
Standard Library
Servo Motor
Stepping Motor
Character LCD
EEPROM
SPI
I2C (Wire)
SD Card
SD (File Operations)
Ethernet
Ethernet (Server)
Ethernet (Client)
Firmata
Periodic Operation
Power Save
Clock (RTC)
SoftwareSerial
Utility
Servo Motor (Servo Class)
This library supports up to 12 motors on the GR-COTTON board and allows GR-COTTON to control servo motors. To use, download as a library, and specify #include <servo.h>
. You will need to create Servo servo0; or similar instances. (See the sample 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 (uS) 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 (uS) 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
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-COTTON 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);
}