งานที่มอบหมาย ครูประภาส

บทความประกอบการเรียนรู้ => ไมโครคอนโทรลเลอร์ (Arduino) => ข้อความที่เริ่มโดย: admin ที่ กุมภาพันธ์ 21, 2015, 03:19:21 PM

หัวข้อ: งานครั้งที่ 37 เขียนโปรแกรมรับค่าจากคีย์แพดแสดงผลผ่าน UART
เริ่มหัวข้อโดย: admin ที่ กุมภาพันธ์ 21, 2015, 03:19:21 PM
เขียนโปรแกรมรับค่าจากคีย์แพดแสดงผลผ่าน UART
ฟังก์ชั่นอยู่ในกลุ่ม Serial http://arduino.cc/en/Reference/Serial (http://arduino.cc/en/Reference/Serial)
-Serial.begin() http://arduino.cc/en/Serial/Begin (http://arduino.cc/en/Serial/Begin)
-if (Serial) http://arduino.cc/en/Serial/IfSerial (http://arduino.cc/en/Serial/IfSerial)
-Serial.available() http://arduino.cc/en/Serial/Available (http://arduino.cc/en/Serial/Available)
-Serial.read() http://arduino.cc/en/Serial/Read (http://arduino.cc/en/Serial/Read)
-Serial.print() http://arduino.cc/en/Serial/Print (http://arduino.cc/en/Serial/Print)
-Serial.println() http://arduino.cc/en/Serial/Println (http://arduino.cc/en/Serial/Println)
-Serial.flush() http://arduino.cc/en/Serial/Flush (http://arduino.cc/en/Serial/Flush)

ฟังก์ชั่นที่ใช้งาน
-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)

ไลบารี่เพิ่มเติม
-Keypad.h
http://playground.arduino.cc/Code/Keypad#Download (http://playground.arduino.cc/Code/Keypad#Download)

การเพิ่มไลบารี่ลงในโปรแกรม Arduino IDE จากไฟล์ Zip
1. ดาวน์โหลดไฟล์ Zip ดังรูป
(http://www.praphas.com/PhotoForum/arduino/Lab-37-a.png)

2. ทำการเพิ่มไลบรารี่ลงในโปรแกรม Arduino IDE โดยการเพิ่มจากไฟล์ zip แล้วทำการหาไฟล์ zip ที่ได้จากการดาวน์โหลดในข้อ 1
(http://www.praphas.com/PhotoForum/arduino/Lab-16-b.png)

ฟังก์ชั่นในไลบารี่ Keypad.h http://playground.arduino.cc/Code/Keypad (http://playground.arduino.cc/Code/Keypad)
-begin(
-waitForKey()
-getKey()
-getState()
-keyStateChanged()
-setHoldTime()
-setDebounceTime()
-addEventListener()
*หมายเหตุ จากการทดลองไลบรารี่ keypad เมื่อใช้ขา D13 สำหรับผู้ที่ใช้งานบอร์ด Arduino รุ่นต่าง ๆ เนื่องจากขา D13 มีการต่อ LED อยู่บนบอร์ดจึงทำให้ไม่สามารถใช้งานได้  ดังนั้นเวลาใช้งานขา D13 จึงห้ามใช้

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

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

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

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

โมดูลสำเร็จรูปเพิ่มเติม
Keypad
http://www.arduinoall.com (คลิก) (http://www.arduinoall.com/product/119/3x4-matrix-keypad)

ตำแหน่งขาต่อของคีย์แพด
(http://www.praphas.com/PhotoForum/arduino/Lab-37-b.png)

ตัวอย่างโปรแกรมรับค่าจากคีย์แพด
โค๊ด: [Select]
#include <Keypad.h>
const byte ROWS = 4;       //four rows
const byte COLS = 3;       //three columns
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};
byte rowPins[ROWS] = {12,11,10,9};    //connect to the row pinouts of the keypad
byte colPins[COLS] = {8,7,6};         //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup()
{
     Serial.begin(9600);
}
void loop()
{
     char key = keypad.getKey();
     if (key)
     {
           Serial.println(key);
     }
}