2015年11月11日 星期三

[Arduino] setup() loop() 不在*.ino草稿檔中,如何做?

一般而言在Arduino中,開啟新檔後,就會在其INO檔中看到setup()及loop()這二個函數

例如:
Blink.ino 內容如下:
void setup() {
  // initialize digital pin 13 as an output.
  pinMode(13, OUTPUT);
}

void loop() {
  digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);              // wait for a second
  digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);              // wait for a second
}

本文打算要來把setup和loop這二個函數搬位置,應該如何實現呢?
在開啟新的arduino檔案時,會有一個INO檔
我們可以加入其它的檔案,如*.h的標頭檔,或是*.cpp的檔



以下二個檔:
setup_loop_not_in.INO及test.CPP

setup_loop_not_in.INO內容如下:
/*我們將這裡的程式註解起來,變成是一個空白程式。
void setup() {
  // initialize digital pin 13 as an output.
  pinMode(13, OUTPUT);
}

void loop() {
  digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);              // wait for a second
  digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);              // wait for a second
}
*/

test.CPP內容如下:
# include "Arduino.h"    //打上這一行就可以了
void setup() {
  // initialize digital pin 13 as an output.
  pinMode(13, OUTPUT);
  pinMode(12, OUTPUT);
}

void loop() {
  digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);              // wait for a second
  digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);              // wait for a second
}

沒有留言:

張貼留言