บทความประกอบการเรียนรู้ => ไมโครคอนโทรลเลอร์ (Arduino) => ข้อความที่เริ่มโดย: admin ที่ พฤษภาคม 02, 2014, 09:47:58 AM

หัวข้อ: งานครั้งที่ 27 เขียนโปรแกรมควบคุม RC Servo Motor ด้วยโพเทนทิโอมิเตอร์
เริ่มหัวข้อโดย: admin ที่ พฤษภาคม 02, 2014, 09:47:58 AM
เขียนโปรแกรมควบคุม RC Servo Motor ด้วยโพเทนทิโอมิเตอร์
ฟังก์ชั่นที่ใช้ในกลุ่ม Servo http://arduino.cc/en/Reference/Servo (http://arduino.cc/en/Reference/Servo)
-attached() http://arduino.cc/en/Reference/ServoAttached (http://arduino.cc/en/Reference/ServoAttached)
-write() http://arduino.cc/en/Reference/ServoWrite (http://arduino.cc/en/Reference/ServoWrite)
**ไลบารี่ Servo.h ไม่ต้องดาวน์โหลดใหม่เนื่องจากมีมาพร้อมใน Arduino IDE อยู่แล้ว

ฟังก์ชั่นทั่วไป
-pinMode() http://arduino.cc/en/Reference/PinMode (http://arduino.cc/en/Reference/PinMode)
-digitalWrite() http://arduino.cc/en/Reference/DigitalWrite (http://arduino.cc/en/Reference/DigitalWrite)
-delay() http://arduino.cc/en/Reference/Delay (http://arduino.cc/en/Reference/Delay)
-delayMicroseconds() http://arduino.cc/en/Reference/DelayMicroseconds (http://arduino.cc/en/Reference/DelayMicroseconds)
-analogRead() http://arduino.cc/en/Reference/AnalogRead (http://arduino.cc/en/Reference/AnalogRead)
-map() http://arduino.cc/en/Reference/Map (http://arduino.cc/en/Reference/Map)


โจทย์โปรแกรม
-เขียนโปรแกรมควบคุมการหมุนของ RC Servo motor โดยใช้โพเทนธิโอมิเตอร์
-โปรแกรมรายละเอียดพิเศษรายกลุ่ม (แจ้งให้ทราบเมื่อถึงชั่วโมงเรียน)

วงจรที่ใช้ในการทดลองสำหรับผู้ที่ใช้บอร์ด Arduino
(http://www.praphas.com/PhotoForum/arduino/Lab-27-01.png)

วงจรที่ใช้ในการทดลองสำหรับผู้ที่ใช้ไอซี ATmega328 (ที่มี Boot Loader Arduino)
(http://www.praphas.com/PhotoForum/arduino/Lab-27-02.png)

บริเวณใช้งานบอร์ดทดลอง
(http://www.praphas.com/PhotoForum/arduino/Lab-27-03.png)

โมดูลสำเร็จรูปเพิ่มเติม
-Futaba S3003 Servo Motor
(http://www.praphas.com/PhotoForum/arduino/Lab-27-a.jpg)
http://www.arduinoall.com (คลิก) (http://www.arduinoall.com/product/114/futaba-s3003-servo-motor-s3003-servo-motor-38g-s3003-standard-servo)

ตัวอย่างโปรแกรม
โค๊ด: [Select]
#include <Servo.h>
Servo myservo;  // create servo object to control a servo
int potpin = 0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin
void setup()
{
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}
void loop()
{
  val = analogRead(potpin);            // reads the value of the potentiometer
  val = map(val, 0, 1023, 0, 180);     // scale it to use it with the servo (value between 0 and 180)
  myservo.write(val);                  // sets the servo position according to the scaled value
  delay(15);                           // waits for the servo to get there
}