Cover image

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

13 Jun 2019

Share to:

สวัสดีครับ หลังจากที่เราได้เรียนรู้การใช้งาน 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

Auto login on raspberry pi

Auto login on raspberry pi ถ้าเราใช้ debian บน raspberry pi ในโหมด gui ระบบจะ login user: pi ให้เราโดยอัตโนมัติ แต่ถ้าเราต้องการให้ login ด้วย user อื่นๆ ก็สามารถทำได้โดย แก้ไข ไฟล์ "/etc/lightdm/lightdm.conf" ด้วยคำสั่งนี้

Access raspberry pi ผ่าน internet โดยไม่ต้อง forward port

สวัสดีครับ หลายๆคนอาจเจอปัญหานี้ อยากจะ ssh ไปยัง raspberry pi ผ่าน internet จากที่ใดๆก็ได้บนโลก แต่ติดปัญหาคือ คุณอาจจะอยู่ หอพัก อพาร์ทเม้นท์ ฯลฯ ที่คุณไม่สามารถเข้าไปยุ่งกับ rounter ได้ ไม่สามารถ forward port ได้ ทําให้ไม่สามารถ Access raspberry pi ได้ ในบทความนี้มีคําตอบครับ พระเอกของงานนี้คือ weaved ซึ่งจะช่วยจักการการเชื่อมต่อให้เราเองเพียงแค่ติดตั้ง service บน raspberry pi ของเราเท่านั้น

เรื่อง config login ssh ของ user root

สําหรับบทความนี้เป็นบทความสั้นๆนะครับ เป็นเรื่องเกี่ยวกับการ config เพื่อกําหนดการ login ssh ของ user root โดยเราจะแก้ไข file config นี้ "/etc/ssh/sshd_config" ผมจะแบ่งการ config เป็นสองหัวข้อคือ


Copyright © 2019 - 2024 thiti.dev |  v1.39.0 |  Privacy policy | 

Build with ❤️ and Astro.

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