บทความประกอบการเรียนรู้ => IOT : Internet of Thing (ESP8266, NodeMCU, WeMos D1 mini) => ข้อความที่เริ่มโดย: admin ที่ พฤศจิกายน 11, 2017, 08:10:45 AM

หัวข้อ: งานครั้งที่ 21 เขียนโปรแกรมควบคุม RC Servo Motor ด้วยโพเทนทิโอมิเตอร์
เริ่มหัวข้อโดย: admin ที่ พฤศจิกายน 11, 2017, 08:10:45 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)


ขั้วต่อของเซอร์โวมอเตอร์ในแต่ละยี่ห้อ
(http://www.imagesco.com/servo/connectors.jpg)

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


วงจรที่ใช้ทดลอง
กรณีใช้บอร์ดรุ่น NodeMCU
(http://www.praphas.com/PhotoForum/iot/Lab-21-Servo/1.png)
กรณีใช้บอร์ดรุ่น WeMos D1 mini
(http://www.praphas.com/PhotoForum/iot/Lab-21-Servo/2.png)

ตัวอย่างโปรแกรม
โค๊ด: [Select]
#include <Servo.h>
Servo myservo;          // create servo object to control a servo
void setup()
{
  myservo.attach(D3);   // attaches the servo on pin D3 to the servo object
}
void loop()
{
  int val = analogRead(A0);            // reads the value of the potentiometer (value 0-1023)
  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
}
http://www.youtube.com/v/4tUwaNiVM2Q