ส่อื การเรยี นเร่อื งโครงสร้างโปรแกรมภาษาซขี อง Arduino
โดย
สมบูรณ์ เนียมกล่า
22 กันยายน 2563
โครงสรา้ งโปรแกรมภาษาซขี อง Arduino
โปรแกรมภาษาซขี อง Arduino มีโครงสรา้ งเชน่ เดยี วกบั ภาษา C++ แตท่ ่ฟี ังกช์ ่นั ท่เี ก่ิยวขอ้ งกบั การ
ประมวลผลดา้ น I/O และการใชง้ านของฟังกช์ ่นั โมดภู ายในตวั ชิพ MCU จะแตกตา่ งกนั ไป โปรแกรมภาษาซี
ของ Arduino จะมีองคป์ ระกอบหลกั ๆ ดงั ตอ่ ไปนี้ คอื
- ไฟลท์ ่อี าจตอ้ งนามาผนวกใชใ้ นการประมวลผลโปรแกรม เรยี กวา่ “Included file”
หรอื เรยี กวา่ “Library program” เป็นโปรแกรมท่ีผอู้ อกแบบภาษาไดก้ าหนดเสรมิ
ขนึ้ มาใหท้ างานเฉพาะอยา่ งท่ตี อ้ งเก่ียวขอ้ งกบั การทางานของโปรแกรมท่เี ราเขยี น
โปรแกรมไลบราร่จี ะมีนามสกลุ .h การใชง้ าน เราตอ้ งกาหนดไวส้ ว่ นบนสดุ ของ
โปรแกรมท่จี ะเขยี น และตอ้ งนาหนา้ ดว้ ย # ตวั อยา่ ง เชน่
#include <LiquidCrystal.h>
- ตวั แปรท่กี าหนดเป็นคา่ คงท่ี(Constant values) ท่ตี อ้ งใชใ้ นโปรแกรม หรอื คา่ คงท่ี ท่ี
อ่านมาจาก Flash memory ของ MCU การใชง้ าน ตอ้ งกาหนดเป็นลาดบั ถดั มาจาก
included files ตวั อยา่ ง เช่น
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
int sensorPin = A0;
int ledPin = 13;
int sensorValue = 0;
- ฟังกช์ ่นั ท่ตี อ้ งทาหนา้ ท่กี าหนดคา่ ตวั แปรตา่ ง ๆ ไวล้ ว่ งหนา้ กอ่ นเขา้ สกู่ ารประมวลผล
และรวมถึงการการกาหนดโหมดสถานะของขา I/O วา่ เป็น INPUT หรอื OUTPUT
เป็นตน้ ฟังกช์ ่นั นเี้ รยี กวา่ “void setup()” การใชง้ าน เราตอ้ งกาหนดเป็นลาดบั ถดั
มาตอ่ จากการกาหนดคา่ คงท่ตี า่ ง ๆ ตวั อยา่ ง เช่น
2
#include <LiquidCrystal.h>
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
void setup()
{
lcd.begin(16, 2);
lcd.print("hello, world!");
}
- ฟังกช์ ่นั ท่ตี อ้ งทางานตามคาส่งั ท่เี รากาหนดไวใ้ นโปรแกรม ฟังกช์ ่นั นจี้ ะทางานวนลปู
แบบไม่รูจ้ บ เรยี กวา่ void loop() หรอื ในโปรแกรมภาษาเบสกิ เรยี กวา่ main
program น่นั เอง ฟังกช์ ่นั นจี้ ะกาหนดไวใ้ นลาดบั ถดั จาก void setup ตวั อยา่ งเช่น
#include <LiquidCrystal.h>
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
void setup()
{
lcd.begin(16, 2);
lcd.print("hello, world!");
}
void loop()
{
// Turn off the display:
lcd.noDisplay();
delay(500);
// Turn on the display:
lcd.display();
3
delay(500);
} // End of Void loop
- ฟังกช์ ่นั ยอ่ ย(sub functions) เป็นฟังกช์ ่นั ท่ผี เู้ ขียนโปรแกรมไดเ้ ขยี นไวใ้ ชง้ านเฉพาะ
หนา้ ท่ีท่จี ะตอ้ งทางานอยบู่ อ่ ย ๆ ฟังกช์ ่นั ประเภทนปี้ กติจะไมท่ างาน จนกวา่ จะมีการ
เรยี กใชม้ า ในภาษาเบสกิ เรยี กฟังกช์ ่นั ประเภทนวี้ า่ sub routine ฟังกช์ ่นั ยอ่ ยเหลา่ นี้
อาจถกู เรยี กใชง้ านมาจากฟังกช์ ่นั ใดก็ได้ แตเ่ ม่อื ทางานเสรจ็ ก็จะยอ้ นกลบั ไป
ทางานตอ่ ในฟังกช์ ่นั ท่เี รยี กมาตามลาดบั ขนึ้ ไป ตวั อยา่ งเช่น
int firstSensor = 0;
int secondSensor = 0;
int thirdSensor = 0;
int inByte = 0;
void setup()
{
Serial.begin(9600);
while (!Serial)
{
; // wait for serial port to connect.
}
pinMode(2, INPUT);
establishContact();
}
void loop()
{
if (Serial.available() > 0)
{
4
inByte = Serial.read();
firstSensor = analogRead(A0);
secondSensor = analogRead(A1);
thirdSensor = map(digitalRead(2), 0, 1, 0, 255);
Serial.print(firstSensor);
Serial.print(",");
Serial.print(secondSensor);
Serial.print(",");
Serial.println(thirdSensor);
}
}
// โปรแกรมสว่ นตอ่ ไปนี้ เป็นฟังกช์ ่นั ยอ่ ย
void establishContact()
{
while (Serial.available() <= 0)
{
Serial.println("0,0,0");
delay(300);
}
}
หลกั ในการเขียนโปรแกรมท่ดี ีนนั้ การเขยี นโปรแกรมในแตล่ ะ statement เราตอ้ งเขยี นคาอธิบาย
กากบั ไวด้ ว้ ย วา่ มนั คอื อะไร ผทู้ ่มี าศกึ ษาเรยี นรูภ้ ายหลงั จะไดเ้ ขา้ ใจ เราเรยี กวา่ “comment” การเขยี น
comment นนั้ ในภาษาซจี ะมีการเขียนอยู่ 2 แบบ คือ
- การเขียนแบบสนั้ ๆ จบในบรรทดั จะใชเ้ คร่อื งหมาย // นาหนา้
ตวั อยา่ ง เช่น
void setup() {
5
// declare the ledPin as an OUTPUT:
pinMode(ledPin, OUTPUT);
}
- การเขยี น comment แบบยาวหลายบรรทดั เพ่อื อธิบายการทางานของโปรแกรม จะ
ใชเ้ ครอ่ื งหมาย /* ปิดหวั - ปิดทา้ ยขอ้ ความ
ตวั อยา่ ง เชน่
/* --------------------------------------------------------------------------------------
LiquidCrystal Library - display() and noDisplay()
Demonstrates the use a 16x2 LCD display. The LiquidCrystal
library works with all LCD displays that are compatible with the
Hitachi HD44780 driver. There are many of them out there, and you
can usually tell them by the 16-pin interface.
This sketch prints "Hello World!" to the LCD and uses the
display() and noDisplay() functions to turn on and off
the display.
The circuit:
* LCD RS pin to digital pin 12
* LCD Enable pin to digital pin 11
* LCD D4 pin to digital pin 5
* LCD D5 pin to digital pin 4
* LCD D6 pin to digital pin 3
* LCD D7 pin to digital pin 2
* LCD R/W pin to ground
* 10K resistor:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3)
-------------------------------------------------------------------------------------- /
6