Arduino Cấu trúc mã nguồn

Phần cứng cần chuẩn bị

1×Arduino Uno R3 Amazon
1×USB 2.0 cable type A/B (for USB-A PC) Amazon
1×USB 2.0 cable type C/B (for USB-C PC) Amazon
1×(Khuyến nghị) Screw Terminal Block Shield for Arduino Uno Amazon
1×(Khuyến nghị) Sensors/Servo Expansion Shield for Arduino Uno Amazon
1×(Khuyến nghị) Breadboard Shield for Arduino Uno Amazon
1×(Khuyến nghị) Enclosure for Arduino Uno Amazon
1×(Khuyến nghị) Prototyping Base Plate & Breadboard Kit for Arduino UNO Amazon

Or you can buy the following kits:

1×DIYables STEM V3 Starter Kit (Arduino included) Amazon
1×DIYables Sensor Kit (18 sensors/displays) Amazon
Tiết lộ: Một số liên kết trong bài viết này là liên kết tiếp thị Amazon và có thể nhận hoa hồng theo chính sách của chương trình.

Cấu trúc cơ bản

Mã Arduino (còn được gọi là sketch Arduino) bao gồm hai phần chính: phần thiết lập và phần vòng lặp.

Mã Thiết Lập

  • Là mã trong hàm setup().
  • được thực thi ngay sau khi cấp nguồn hoặc khởi động lại
  • Chỉ thực thi một lần.
  • được dùng để khởi tạo biến, chế độ của các chân (pin modes), bắt đầu sử dụng các thư viện,

Mã vòng lặp

  • Là mã trong hàm loop().
  • được thực thi ngay sau hàm setup.
  • được thực thi lặp đi lặp lại (vô hạn).
  • được dùng để thực hiện nhiệm vụ chính của ứng dụng.

Ví dụ

void setup() { // put your setup code here, to executed once: Serial.begin(9600); Serial.println("This is setup code"); } void loop() { // put your main code here, to run repeatedly: Serial.println("This is loop code"); delay(1000); }

Hướng dẫn từng bước

  • Sao chép mã ở trên và mở bằng Arduino IDE
  • Nhấn nút Tải lên trên Arduino IDE để tải mã lên board Arduino
  • Mở Serial Monitor
  • Xem đầu ra trên Serial Monitor
Newbiely | Arduino IDE 2.3.8
──
File
Edit
Sketch
Tools
Help
Arduino Uno
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno' on 'COM15')
New Line
9600 baud
This is setup code This is loop code This is loop code This is loop code This is loop code This is loop code This is loop code This is loop code
Ln 11, Col 1
Arduino Uno on COM15
2

Như bạn có thể thấy, “Đây là mã thiết lập” được in ra một lần, nhưng “Đây là mã vòng lặp” được in ra nhiều lần. Điều đó có nghĩa là mã thiết lập được thực thi một lần, mã vòng lặp được thực thi lặp đi lặp lại. Mã thiết lập được thực thi trước.

※ Lưu ý:

setup()loop() là các hàm phải nằm trong mã Arduino. Nếu không, sẽ xảy ra một lỗi.

Các phần tùy chọn

Ngoại trừ mã setup và loop, một bản phác thảo Arduino có thể bao gồm một số phần sau:

  • Block comment: usually used to write some information about the author, the wiring instruction, the license ... Arduino will ignore this part.
  • Libraries inclusion: is used to include libraries into the sketch.
  • Constant definition: used to define constant
  • Global variables declaration
/* * Mã Arduino này được phát triển bởi newbiely.vn * Mã Arduino này được cung cấp để sử dụng công khai, không có ràng buộc. * Để xem hướng dẫn chi tiết và sơ đồ kết nối, vui lòng truy cập: * https://newbiely.vn/tutorials/arduino/arduino-code-structure */ #include <Servo.h> #include <LiquidCrystal.h> #define MAX_COUNT 180 Servo servo; LiquidCrystal lcd(3, 4, 5, 6, 7, 8); int loop_count = 0; void setup() { Serial.begin(9600); lcd.begin(16, 2); servo.attach(9); Serial.println("This is setup code"); } void loop() { loop_count++; Serial.print("This is loop code, count: "); Serial.println(loop_count); lcd.print("Hello World!"); servo.write(loop_count); if(loop_count >= MAX_COUNT) loop_count = 0; delay(1000); }

Hướng dẫn từng bước

  • Sao chép mã ở trên và mở bằng Arduino IDE
  • Nhấn nút Tải lên trên Arduino IDE để nạp mã vào Arduino
  • Mở Serial Monitor
  • Xem dữ liệu đầu ra trên Serial Monitor
Newbiely | Arduino IDE 2.3.8
──
File
Edit
Sketch
Tools
Help
Arduino Uno
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno' on 'COM15')
New Line
9600 baud
This is setup code This is loop code, count: 1 This is loop code, count: 2 This is loop code, count: 3 This is loop code, count: 4 This is loop code, count: 5 This is loop code, count: 6 This is loop code, count: 7
Ln 11, Col 1
Arduino Uno on COM15
2

Hiện tại chúng ta KHÔNG cần hiểu mã nguồn theo từng dòng. Chúng ta chỉ cần biết về cấu trúc của mã nguồn. Mã theo từng dòng sẽ được giải thích trong các bài hướng dẫn tiếp theo.