Arduino ตอน4 ภาษา C++ สําหรับ Arduino

13 Jun 2019,
Share: 
Cover image

สวัสดีครับ หลังจากที่เราได้เรียนรู้การใช้งาน Arduino ตั้งแต่ติดตั้งโปรแกรม Arduino IDE ไปจนถึง Upload โปรแกรมลง Arduino board ในบทความ Arduino ตอน3 ติดตั้ง Arduino IDE และเริ่มต้นเขียนโปรแกรมแรก กันไปแล้ว ในบทความนี้เราจะลงลึกการเขียนโปรแกรมควบคุม Arduino กันแบบจริงๆจังๆกันครับ โดยจะเน้นไปในส่วนของโครงสร้างของภาษา C++ สําหรับ Arduino

โครงสร้างของโปรแกรมภาษา C++ ของ Arduino จะประกอบไปด้วย 5 ส่วนคือ

  1. Preprocessor directives
  2. ส่วนของการกำหนดค่า (Global declarations)
  3. ฟังก์ชั่น setup() และ ฟังก์ชั่น loop()
  4. การสร้างฟังก์ชั่น และการใช้งานฟังก์ชั่น (Users-defined function)
  5. ส่วนอธิบายโปรแกรม (Progarm comments)

ด้านล่างนี่เป็นตัวอย่างโครงสร้างโปรแกรมภาษา 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
}

ผมจะเริ่มอธิบายไปที่ละส่วนนะครับ

1. Preprocessor directives

Preprocessor directives เป็นส่วนที่เขียนไว้บนสุดของโปรแกรม และจะขึ้นต้นด้วย # ซึ่ง Code ในส่วนตรงนี้ จะทํางานก่อนที่จะ Compile Code ส่วนใหญ่จะว้ใช้กําหนดค่าคงที่ หรือ Import library เข้ามาใช้ในโปรแกรม ตัวอย่างเช่น

#include <Wire.h>

#include <Time.h>

#define AGE 18

2. ส่วนของการกำหนดค่า (Global declarations)

เป็นส่วนของการประกาศตัวแปรภายนอก Function หรือประกาศ Function ต่างๆ ซึ่งจะเป็นการประกาศแบบ Global หมายความว่าทุกๆ Function จะสามารถเรียกใช้ตัวแปร หรือ Function ที่ประกาศแบบนี้ได้ ตัวอย่าง

int count = 12;

หรือ

void setup() {
  ...
}

3. ฟังก์ชั่น setup() และ ฟังก์ชั่น loop()

ฟังก์ชั่น 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
}

4. การสร้างฟังก์ชั่น และการใช้งานฟังก์ชั่น (Users-defined function)

นอกจาก Function setup() และ loop() แล้วเรายังสามารถสร้าง Function ขึ้นมาใช้งานเองได้ดังตัวอย่างนี้

void Mode(int pin) {

  pinMode(pin, OUTPUT);

}

void setup() {

  Mode(13);

}

5. ส่วนอธิบายโปรแกรม (Progarm comments)

สําหรับส่วนของ อธิบายโปรแกรม (Progarm comments) ส่วนนี้จะไม่ถูกนํามา Compile มีไว้ใช้เขียนอธิบายโปรแกรม กรณีที่เราต้องการอธิบายขอยายความ Code ของเรา จะถูกเขียนไว้หลังเครื่องหมาย // หรือเขียนอยู่ภายในเครื่องหมาย /* */ ตามตัวอย่างนี้ครับ

/* This is a comment */

หรือ

// This is a comment

เท่านี้เราก็รู้จักส่วนต่างๆของภาษา C++ ที่ใช้กับ Arduino แล้ว ซึ่งมันก็จะคล้ายๆกับ ภาษา C++ ทั่วๆไปเลยครับ

สําหรับบทความนี้พอแค่นี้ก่อนนะครับ แล้วเจอกันใหม่บทความหน้า ขอบคุณที่ติดตามอ่านนะครับ :)

Suggestion blogs

ตัวหารร่วมมาก และการนำไปใช้

ตัวหารร่วมมาก (ห.ร.ม.) คือ ตัวหารร่วม (หรือตัวประกอบร่วม) ที่มีค่ามากที่สุด ที่นำไปหารจำนวนนับชุดใด(ตั้งแต่สองจำนวนขึ้นไป) ได้ลงตัว ต่อไปนี้เราจะเรียกว่าการหา ห.ร.ม. เช่น ห.ร.ม. ของ 8 และ 12 คือ 4 เพราะ 4 คือจำนวนที่มากที่สุดที่หารทั้ง 8 และ 12 ได้ลงตัว

Go EP.11 Panic ในภาษา Go

สวัสดีครับ ในบทความนี้ก็เป็น EP.11 แล้วนะครับ โดยเนื้อหาจะเป็นเรื่องเกี่ยวกับ Panic คืออะไร ใช้ทําอะไร และมีวิธีการใช้อย่างไรสําหรับท่านใดที่ยังไม่ได้อ่าน EP.10 ท่านสามารถกลับไปอ่านก่อนได้นะครับที่นี่ Go EP.10 Defer ในภาษา Goมาเริ่มเรียนรู้ไปด้วยกันตามหัวข้อด้านล่างเลยครับ

Flutter EP.3 Widget ใน Flutter

สวัสดีครับ ใน EP.2 เราได้เรียนรู้ Syntax ของภาษา Dart กันไปแล้ว สําหรับเนื้อหาต่อไปในบทความนี้จะเป็นเรื่อง Widget ซึ่งเป็นพื้นฐานที่จําเป็นต้องรู้ในการเขียน Mobile application ด้วย Flutter


Copyright © 2019 - 2026 thiti.dev | v1.61.0 | Privacy policy | 

Build with ❤️ and Astro.

Github profile Linkedin profile Instagram X profile Nostr Youtube channel Telegram Email contact วงแหวนเว็บ