なお GitHub にアップされていて、こちらからファイルをダウンロードし、Arduino IDE の所定の場所に置くか(例えば C:\Program Files (x86)\Arduino\libraries)zip ファイルをダウンロードし、Arduino IDE で取り込めば、ライブラリが使えるようになります。
いきなりサンプルスケッチですが・・
#include <VarSpeedServo.h>
VarSpeedServo myservo; // create servo object to control a servo
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop() {
myservo.write(180, 30, true); // move to 180 degrees, use a speed of 30, wait until move is complete
myservo.write(0, 30, true); // move to 0 degrees, use a speed of 30, wait until move is complete
}
1行目は、VarSpeedServo.h をインクルードする
3行目は、servo オブジェクトを作る
6行目は、サーボ 9番ピンに接続していることを指定する
10行目と11行目でサーボを動かしていますが、
myservo.write(180, 30, true); // move to 180 degrees, use a speed of 30, wait until move is complete
サーボモーターを現在の位置から、180°の位置まで、30のスピードで動かし、wait する myservo.write(0, 30, true); // move to 0 degrees, use a speed of 30, wait until move is complete
/*
ServoSequence
Reads an analog input, and plays different servo sequences depending on the analog value
This example code is in the public domain.
*/
#include <VarSpeedServo.h>
VarSpeedServo myservo1;
const int servoPin1 = 9; // the digital pin used for the servo
// sequences are defined as an array of points in the sequence
// each point has a position from 0 - 180, and a speed to get to that position
servoSequencePoint slow[] = {{100,20},{20,20},{60,50}}; // go to position 100 at speed of 20, position 20 speed 20, position 60, speed 50
servoSequencePoint twitchy[] = {{0,255},{180,40},{90,127},{120,60}};
const int analogPin = A0;
// the setup routine runs once when you press reset:
void setup() {
myservo1.attach(servoPin1);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(analogPin);
if (sensorValue > 200) {
myservo1.sequencePlay(slow, 3); // play sequence "slowHalf" that has 3 positions, loop and start at first position
} else {
myservo1.sequencePlay(twitchy, 4, true, 2); // play sequence "twitchy", loop, start at third position
}
delay(2); // delay in between reads for analogin stability
}