PART A.
https://byjus.com/physics/pulse-width-modulation/
https://www.digikey.tw/zh/blog/pulse-width-modulation
https://www.digikey.com/en/blog/pulse-width-modulation
PART B.
[筆記]Arduino實驗五
WM調控
#include <Servo.h>
#define PIN_VR A1
#define PIN_SERVO 3
#define PIN_PWM 6
#define PIN_LED 13
Servo myservo; // create servo object to control a servo
int val; // variable to read the value from the analog pin
void setup() {
// put your setup code here, to run once:
pinMode( PIN_VR, INPUT );
pinMode( PIN_SERVO, OUTPUT );
pinMode( PIN_PWM, OUTPUT );
pinMode( PIN_LED, OUTPUT );
// attaches the servo on pin 9 to the servo
myservo.attach( PIN_SERVO );
Serial.begin( 9600 );
delay( 100 );
}
void loop() {
static int cnt = 0;
static int flag_led = 0;
// put your main code here, to run repeatedly:
// reads the value of the potentiometer (value between 0 and 1023)
val = analogRead( PIN_VR );
Serial.println( val );
analogWrite( PIN_PWM, val/4 );
// scale it to use it with the servo (value between 0 and 180)
val = map(val, 0, 1023, 500, 1500);
// sets the servo position according to the scaled value
myservo.writeMicroseconds( val );
cnt++;
if( cnt >= 25 ) { flag_led ^= 1; cnt = 0; }
digitalWrite( PIN_LED, flag_led );
delay(20);
}