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

Review ไฟฉาย Olight sMini limited edition

ไฟฉาย Olight sMini limited edition เป็นรุ่นที่มีขนาดเล็ก ใส่แบตเตอรี่ CR123 ยาวเพียง 5.45 ซม มาดูคุณสมบัติกันครับ

สร้าง Dark Web (Onion Site) ด้วย Tor และ nginx

ก่อนอื่นมาทําความรู้จักกับ Tor hidden service กันก่อน Tor hidden service เป็นการซ่อน Service หรือ Website ไม่ให้สามารถเปิดได้ด้วยเครือข่าย Internet ทั่วไป หรือไม่สามารถค้นหาผ่าน Search engine ทั่วไปได้ เรียกกันว่า Deep web และ Dark web โดยจะซ่อน Service หรือ Website ไว้ในเครือข่าย Tor ซึ่งจะสามารถเข้าได้ผ่าน Tor browser และ Url จะลงท้ายด้วย .onion รายละเอียดสามารถกลับไปอ่านจากบทความเรื่อง Deep web และ Dark web ด้านมืดของ Internet

ESP8266 ควบคุม i/o ผ่าน web (Access point)

ในบทความนี้ผมจะอธิบายถึงวิธีการทําให้ ESP8266 เป็น Access point ให้อุปกรณ์อุปกรณ์อื่นๆ ที่เชื่อมต่อ wifi ได้ เช่น computer, smart phone ฯลฯ มาเชื่อมต่อกับ SEP8266 แล้วควบคุม i/o ปิด/เปิด LED ผ่านเว็บ ด้วยวิธีส่งข้อมูลผ่าน HTTP_GET เช่น


Copyright © 2019 - 2025 thiti.dev |  v1.53.0 |  Privacy policy | 

Build with ❤️ and Astro.

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