#LXD-227制御例
#define SERVO_PIN 9
#define CYCLE 22000
#define MIN_PULSE 1
#define ANGLE_0 600
#define ANGLE_180 2000
#define ANGLE_90 1300
int pulse = 600;//500-2100 (500-2500)
int _pulse = 2000;
void ServoWrite(int min_pulse) {
if (_pulse - pulse > min_pulse) {
_pulse -= min_pulse;
}
else if (_pulse - pulse < -min_pulse) {
_pulse += min_pulse;
}
else {
_pulse = pulse;
}
digitalWrite(SERVO_PIN, HIGH);
delayMicroseconds(_pulse);
digitalWrite(SERVO_PIN, LOW);
delayMicroseconds(CYCLE - _pulse);
}
void ServoTest(int speed) {
for (int i = 0; i < 500; i++) {
ServoWrite(speed);
}
}
void ServoSetup() {
pinMode(SERVO_PIN, OUTPUT);
int speed = 30;
pinMode(SERVO_PIN, OUTPUT);
ServoTest(speed);
pulse = ANGLE_180;
ServoTest(speed);
pulse = ANGLE_0;
ServoTest(speed);
pulse = ANGLE_180;
ServoTest(speed);
pulse = ANGLE_90;
}
void setup() {
Serial.begin(115200);
ServoSetup();
Serial.println("START");
}
void loop() {
if (Serial.available()) {
pulse = Serial.parseInt();
if (pulse < 500)pulse = 500;
if (pulse > 2100)pulse = 2100;
}
ServoWrite(MIN_PULSE);
}