การเขียนโปรแกรมเบื้องต้นกับ Arduino C++ (การส่งค่า สัญญาณ PWM)
คำอธิบาย
PWM คือเทคนิดการส่งสัญญาณแบบสวิต หรือ ส่งค่าดิจิตอล 0-1 โดยให้สัญญาณความถี่คงที่ การควบคุมระยะเวลาสัญญาณสูงและสัญญาณต่ำ ที่ต่างกัน ก็จะทำให้ค่าแรงดันเฉลี่ยของสัญญาณสวิต ต่างกันด้วย
สำหรับโมดุล PWM ของ Arduino มีความละเอียด 8 bit หรือ ปรับได้ 255 ระดับ ดังนั้นค่าสัญญาณ 0 โวลต์ถึง 5 โวลต์ จะถูกแสดงเป็นสัญญาณแบบดิจิตอล จะได้ 0 ถึง 255 ซึ่งเราสามารถเทียบสัดส่วนคำนวนจากเลขจริง เป็น เลขทางดิจิตอลได้
ตัวอย่าง Code
// select the input pin for the potentiometer
int sensorPin = A0;// select the pin for the LEDint ledPin = 9;// variable from the sensorint sensorValue;int ledValue;void setup() {// declare the ledPin as an OUTPUT:pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}void loop() {// read the value from the sensor:sensorValue = analogRead(sensorPin);ledValue = map( sensorValue, 0, 1023, 0, 255);
Serial.println(ledValue);
delay(100);
// fade LEDanalogWrite (ledPin , ledValue);}
อ้างอิงhttp://www.myarduino.net

ไม่มีความคิดเห็น:
แสดงความคิดเห็น