ผู้เขียน หัวข้อ: งานครั้งที่ 27 เขียนโปรแกรมควบคุม RC Servo Motor ด้วยโพเทนทิโอมิเตอร์  (อ่าน 10231 ครั้ง)

admin

  • Administrator
  • Hero Member
  • *****
  • กระทู้: 706
    • ดูรายละเอียด
    • อีเมล์
เขียนโปรแกรมควบคุม RC Servo Motor ด้วยโพเทนทิโอมิเตอร์
ฟังก์ชั่นที่ใช้ในกลุ่ม Servo http://arduino.cc/en/Reference/Servo
-attached() http://arduino.cc/en/Reference/ServoAttached
-write() http://arduino.cc/en/Reference/ServoWrite
**ไลบารี่ Servo.h ไม่ต้องดาวน์โหลดใหม่เนื่องจากมีมาพร้อมใน Arduino IDE อยู่แล้ว

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


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

วงจรที่ใช้ในการทดลองสำหรับผู้ที่ใช้บอร์ด Arduino


วงจรที่ใช้ในการทดลองสำหรับผู้ที่ใช้ไอซี ATmega328 (ที่มี Boot Loader Arduino)


บริเวณใช้งานบอร์ดทดลอง


โมดูลสำเร็จรูปเพิ่มเติม
-Futaba S3003 Servo Motor

http://www.arduinoall.com (คลิก)

ตัวอย่างโปรแกรม
โค๊ด: [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
}
« แก้ไขครั้งสุดท้าย: มิถุนายน 27, 2017, 08:19:43 PM โดย admin »