1. 程式人生 > >[arduino]-1-Basics程式碼解讀

[arduino]-1-Basics程式碼解讀

arduino IDE 自帶示例中的Basics系列,

可以說是

向一個擁有C系家族程式語言基礎的人

解釋清楚了它自己的基本操作

BareMinimum(基本檔案結構):

void setup() {
  // put your setup code here, to run once:
  // 這裡放的setup程式碼只會執行一次
}

void loop() {
  // put your main code here, to run repeatedly:
  // 這裡放的主要程式碼會在setup之後不停迴圈執行
}

 

Start(有修改):

// the setup routine runs once when you press reset:
//setup會在你按下reset之後首先執行 void setup() { // initialize the digital pin as an output. // 將數字埠初始化為輸出模式 pinMode(0, OUTPUT); //LED on Model B pinMode(1, OUTPUT); //LED on Model A or Pro } // the loop routine runs over and over again forever: //loop會一遍又一遍的永遠執行下去 void loop() { digitalWrite(0, HIGH); //
turn the LED on (HIGH is the voltage level) // 開啟LED(HIGH代表電壓高低) digitalWrite(1, HIGH); delay(1000); // wait for a second digitalWrite(0, LOW); // turn the LED off by making the voltage LOW // 將電壓降低來關閉LED digitalWrite(1
, LOW); delay(1000); // wait for a second }

 

Blink:

/*
  Blink
  閃爍

  Turns an LED on for one second, then off for one second, repeatedly.
  點亮LED持續1秒鐘,然後熄滅LED一秒鐘,一次迴圈。

  Most Arduinos have an on-board LED you can control.
  許多arduino都有一個板載LED可供使用

  On the UNO, MEGA and ZERO it is attached to digital pin 13, on MKR1000 on pin 6.
  在UNO,MEGA和ZERO上它與數字埠13相連。//後略

  LED_BUILTIN is set to the correct LED pin independent of which board is used.
  LED_BUILTIN被設定為各種主機板相對應的LED而與型號無關。
  
  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:
  如果你想知道你的這種arduino上到底哪個埠連線著板載LED,那麼檢視詳細的說明可以到:
  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
*/

// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  // 將LED_BUILTIN埠初始化為輸出模式。
  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
}

 

以上的程式碼皆可直接下載到UNO上執行

關於具體程式碼的理解,

請各位善用自己的英語知識

與網際網路工具

 

本節關鍵程式碼詞彙含義對照:

setup ≈ set up 意思非常之多,初始化,安排,安裝,構建,都可以

loop 迴圈

pin 埠/針腳,1pin就是一根線,所以電腦裡面的電源插口,4P,6P,24P就是這樣來的

built in 內建/板載

digital 數字,關於模擬和數字的區別,你用著用著就明白了

delay 等待/延遲