#define SERVO_PIN 19 // Any pin on the Arduino or Gertboard will work.
void setup()
{
pinMode(SERVO_PIN, OUTPUT);
}
int lenMicroSecondsOfPeriod = 20 * 1000; // 20 milliseconds (ms)
int minPulseWidth = 0.5 * 1000; //0,5 adalah minimum pulsa / sinyal dalam (ms)
int maxPulseWidth = 2.5 * 1000; //2,5 adalah maksimal pulsa / sinyal dalam (ms)
int increment = 0.02 * 1000; //variabel untuk penambahan nilai
void loop()
{
int posisi = 0;
for(posisi = minPulseWidth; posisi < maxPulseWidth; posisi+=increment){
// Servos work by sending a 20 ms pulse.
// 0.5 ms at the start of the pulse will turn the servo to the 0 degree position
// 1.5 ms at the start of the pulse will turn the servo to the 90 degree position
// 2.4 ms at the start of the pulse will turn the servo to the 180 degree position
// Turn voltage high to start the period and pulse
digitalWrite(SERVO_PIN, HIGH);
// Delay for the length of the pulse
delayMicroseconds(posisi);
// Turn the voltage low for the remainder of the pulse
digitalWrite(SERVO_PIN, LOW);
// Delay this loop for the remainder of the period so we don't
// send the next signal too soon or too late
delayMicroseconds(lenMicroSecondsOfPeriod - posisi);
}
for(posisi = maxPulseWidth; posisi > minPulseWidth; posisi-=increment){
// Servos work by sending a 20 ms pulse.
// 0.5 ms at the start of the pulse will turn the servo to the 0 degree position
// 1.5 ms at the start of the pulse will turn the servo to the 90 degree position
// 2.4 ms at the start of the pulse will turn the servo to the 180 degree position
// Turn voltage high to start the period and pulse
digitalWrite(SERVO_PIN, HIGH);
// Delay for the length of the pulse
delayMicroseconds(posisi);
// Turn the voltage low for the remainder of the pulse
digitalWrite(SERVO_PIN, LOW);
// Delay this loop for the remainder of the period so we don't
// send the next signal too soon or too late
delayMicroseconds(lenMicroSecondsOfPeriod - posisi);
}
}