void setup()
{
ServoPin(15);
}
void loop(){
for(int pos=0 ; pos<=180 ; pos++){
Servo(pos);
delay(10);}
for(int pos=180 ; pos>=0 ; pos--){
Servo(pos);
delay(10);}
}
void ServoPin(const int Pin){
//f = 1/T ; karena 1 siklus pulse servo = 20 ms, maka f= 1/0,02s = 50 Hz
const int freq = 50;
const int ledChannel = 0 ;
const int resolution = 16;
ledcSetup(ledChannel, freq, resolution);
// attach the channel to the GPIO to be controlled
ledcAttachPin(Pin, ledChannel);
}
void Servo(int Position ){
const int ledChannel = 0;
const int resolution = 16 ;
int BIT = pow(2,resolution);
int Posisi_0 = BIT*0.025; // 0,5 ms /20 ms PWM Posisi 0
int Posisi_180 = BIT*0.125; // 2,5 ms /20 ms PWM Posisi 180
int PWM = map(Position,0,180,Posisi_0,Posisi_180);
ledcWrite(ledChannel, PWM);
}