存檔 | 收藏
點閱數:8,978
[綜合討論] 水箱出現黑渣
無頭像
   j66

小50殺手
文章 131
用戶失蹤天數 118
點數 108   戰績 0   改裝 0   故障 1
臺南市 東 區 
發表於 2022-6-20 12:53 AM 
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);                           
}


 




加入贊助商  |  免責聲明  |  聯絡我們