Created
May 8, 2016 18:37
-
-
Save jackrobotics/7b864c686a6350850e3bff66d89f22b1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int pushButton = 2; | |
/*เป็นการประกาศตัวแปรชนิด int (เก็บจำนวนเต็ม) ชื่อว่า pushButton โดยมีค่าเท่ากับ 2 เพื่อเก็บค่านี้ไว้ใช้ต่อในส่วนต่อไปของโปรแกรม*/ | |
void setup() { | |
Serial.begin(9600); | |
/*ฟังก์ชั่น Serial.begin(9600); เป็นการเรียกใช้งาน Serial Communication ผ่านการเชื่อมต่อ Serial Port (ในที่นี้ Arduino จะใช้การเชื่อมต่อผ่าน USB โดยมี USB to Serial ภายใน) ด้วยความเร็วที่เชื่อมต่อคือ 9600 หรือเรียกว่าบรอทเรต 9600*/ | |
pinMode(pushButton, INPUT); | |
/*ฟังก์ชั่น pinMode(pushButton,INPUT); คือการกำหนดโหมดของการทำงานของขานั้นๆ เช่นคำสั่งนี้จะทำให้ขา pushButton นั้นคือขา 2 ตามที่ได้มีการประกาศตัวแปรด้านบน int pushButton = 2; มีสถานะคือการ input นั้นคือสามารถกำหนดให้มีไฟต่อเข้ามายังไมโครคอนโทรเลอร์ได้ว่าจะให้เข้า 0V (GND) หรือ 5V ตาม logic ที่เราต้องการในโปรแกรม */ | |
} | |
void loop() { | |
int buttonState = digitalRead(pushButton); | |
/*ฟังก์ชั่น digitalRead(pushButton); เป็นการเรียกใช้งานการอ่าน Logic ดิจิตอล โดยค่าที่ได้จะเป็น 0 หรือ 1โดยอ่านที่ขา pushButton โดยขา pushButton นั้นเราประกาศตัวแปรไว้คือ pin 2 เมื่ออ่านค่าเข้ามาแล้วเก็บในตัวแปร buttonState ซึ่งตัวแปร int เป็นการเก็บค่าจำนวนเต็ม 0 หรือ 1 จากฟังก์ชั่น digitalRead(pushButton);*/ | |
Serial.println(buttonState); | |
/*ฟังก์ชั่น Serial.println(); เป็นการเรียกใช้งานออปเจค Serial ซึ่งเป็นการสื่อสารแบบ Serial ผ่าน usb to serial และ println คือการแสดงค่าออกทาง serial เพื่อให้คอมพิวเตอร์หรืออุปกรณ์ที่ต่ออยู่สามารถรับและแสดงผลค่านั้นๆ ได้ println โดย ln คือการ line new หรือขึ้นบรรทัดใหม่ครับ กล่างคือบรรทัดนี้จะทำให้สามารถสื่อสารผ่าน Serial โดยแสดงค่า buttonState ที่อ่านจาก digitalRead(pushButton); แล้วขึ้นบรรทัดใหม่*/ | |
delay(1); | |
/* delay(1); คือการหน่วงเวลา (หลับหูหลับตา Microcontroller) เป็นเวลา 1 ms หรือ 1 ส่วน 1000 วินาทีนั่นเอง */ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment