สวัสดีครับ หลังจากที่เราได้เรียนรู้การใช้งาน Arduino ตั้งแต่ติดตั้งโปรแกรม Arduino IDE ไปจนถึง Upload โปรแกรมลง Arduino board ในบทความ Arduino ตอน3 ติดตั้ง Arduino IDE และเริ่มต้นเขียนโปรแกรมแรก กันไปแล้ว ในบทความนี้เราจะลงลึกการเขียนโปรแกรมควบคุม Arduino กันแบบจริงๆจังๆกันครับ โดยจะเน้นไปในส่วนของโครงสร้างของภาษา C++ สําหรับ Arduino
โครงสร้างของโปรแกรมภาษา C++ ของ Arduino จะประกอบไปด้วย 5 ส่วนคือ
ด้านล่างนี่เป็นตัวอย่างโครงสร้างโปรแกรมภาษา C++ สําหรับ Arduino แบบเต็มๆ ครับ
/*
Blink
Turns an LED on for one second, then off for one second, repeatedly.
Most Arduinos have an on-board LED you can control. On the UNO, MEGA and ZERO
it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN is set to
the correct LED pin independent of which board is used.
If you want to know what pin the on-board LED is connected to on your Arduino
model, check the Technical Specs of your board at:
https://www.arduino.cc/en/Main/Products
modified 8 May 2014
by Scott Fitzgerald
modified 2 Sep 2016
by Arturo Guadalupi
modified 8 Sep 2016
by Colby Newman
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/Blink
*/
#include <Wire.h>
#include <Time.h>
#define AGE 18
int count = 12;
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
ผมจะเริ่มอธิบายไปที่ละส่วนนะครับ
Preprocessor directives เป็นส่วนที่เขียนไว้บนสุดของโปรแกรม และจะขึ้นต้นด้วย # ซึ่ง Code ในส่วนตรงนี้ จะทํางานก่อนที่จะ Compile Code ส่วนใหญ่จะว้ใช้กําหนดค่าคงที่ หรือ Import library เข้ามาใช้ในโปรแกรม ตัวอย่างเช่น
#include <Wire.h>
#include <Time.h>
#define AGE 18
เป็นส่วนของการประกาศตัวแปรภายนอก Function หรือประกาศ Function ต่างๆ ซึ่งจะเป็นการประกาศแบบ Global หมายความว่าทุกๆ Function จะสามารถเรียกใช้ตัวแปร หรือ Function ที่ประกาศแบบนี้ได้ ตัวอย่าง
int count = 12;
หรือ
void setup() {
...
}
ฟังก์ชั่น setup() และฟังก์ชั่น loop() เป็นคำสั่งที่ Arduino บังคับต้องให้มีในทุกโปรแกรม โดยทั้งสอง Function นี้ Arduino กําหนดให้มีหน้าที่ดังนี้
Function setup() จะถูกเรียกใช้ทึกครั้งที่โปรแกรมเริ่มทํางาน ส่วนใหญ่จะเอาไว้ใช้กําหนดค่าตัวแปรเริ่มต้น หรือเริ่มต้นใช้งานไลบารี่ต่างๆ ตัวอย่างเช่น
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
}
Function loop() Function นี้จะถูกเรียกใช้หลังจาก Function setup() และจะทํางานแบบวนลูปไม่รู้จบ หมายความว่าเมื่อ loop() ทํางานเสร็จ ก็จะวนกลับมาทํางาน loop() อีกครั้ง วนแบบนี้ไปเรื่อยๆ ไม่รู้จบ ตัวอย่าง
void loop() {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
นอกจาก Function setup() และ loop() แล้วเรายังสามารถสร้าง Function ขึ้นมาใช้งานเองได้ดังตัวอย่างนี้
void Mode(int pin) {
pinMode(pin, OUTPUT);
}
void setup() {
Mode(13);
}
สําหรับส่วนของ อธิบายโปรแกรม (Progarm comments) ส่วนนี้จะไม่ถูกนํามา Compile มีไว้ใช้เขียนอธิบายโปรแกรม กรณีที่เราต้องการอธิบายขอยายความ Code ของเรา จะถูกเขียนไว้หลังเครื่องหมาย // หรือเขียนอยู่ภายในเครื่องหมาย /* */ ตามตัวอย่างนี้ครับ
/* This is a comment */
หรือ
// This is a comment
เท่านี้เราก็รู้จักส่วนต่างๆของภาษา C++ ที่ใช้กับ Arduino แล้ว ซึ่งมันก็จะคล้ายๆกับ ภาษา C++ ทั่วๆไปเลยครับ
สําหรับบทความนี้พอแค่นี้ก่อนนะครับ แล้วเจอกันใหม่บทความหน้า ขอบคุณที่ติดตามอ่านนะครับ :)
การคูณ และ หาร ระหว่างจำนวนเต็มสองจำนวน อาศัยเรื่องผลคูณของค่าสัมบูรณ์ของจำนวนทั้งสอง โดยมีเครื่องหมาย ดังนี้
ไฟฉาย Nitecore รุ่น EC4GT เป็นไฟฉายรุ่นที่ที่มีความสว่างสูงสุด 1000 Lumens จุดเด่นของรุ่นนี้คือ ตัวโครมลึกทําให้แสงที่ออกเป็น Sport สามารถพุ่งไกลถึง 475 เมตร ใช้แบตเตอรี่ขนาด 18650 สองก้อน คุณสมบัติโดยทั่วไปดังนี้
สวัสดีครับ บทความนี้ผมจะมาแนะนําการใช้งาน Blynk ให้สามารถรับ 1,000,000 energy ไปใช้ฟรีๆ