}
โปรแกรมที่ 1 ใช้คำสั่ง analogWrite
// set output pin for the PWM
int pwm_out = 9;
void setup() {
// declare the PWM as an OUTPUT:
pinMode(pwm_out, OUTPUT);
}
void loop() {
analogWrite(pwm_out, 127);
}
โปรแกรมที่ 2 ใช้ ไลบรารี่ TimerOne.h
#include "TimerOne.h“
int pwm_out = 9;
void setup()
{
Timer1.initialize(1000); // initialize timer1, and set 1000 uS
Timer1.pwm(pwm_out, 512); // setup pwm on pin 9, 50% duty cycle
}
void loop()
{
การควบคุม เซอร์โวมอเตอร์
โปรแกรมที่ 3 ควบคุมเซอร์โวมอเตอร์โดยใช้ ไลบรารี่ TimerOne.h
#include "TimerOne.h“
int pwm_out = 9;
void setup()
{
Timer1.initialize(20000); // initialize timer1, and set 1000 uS
Timer1.pwm(pwm_out, 76); // setup pwm on pin 9, for 90 degree
delay(200) // delay time
Timer1.stop();
}
void loop()
{
}
โปรแกรมที่ 4 ควบคุมเซอร์โวมอเตอร์โดยป้อนมุมผ่านทาง Serial Monitor