1. 程式人生 > >Arduino學習(一)

Arduino學習(一)

5.5 nbsp most The this created ati 一個 都是

Arduino IDE自然是從官網下載的。
pinMode(pin, mode):設置引腳。第一個參數表示設置的引腳,第二個參數表示將要把引腳設置成的模式。
digitalWrite(pin, value):輸出信號。第一個參數為引腳,第二個參數為輸出模式。HIGH即為高電平,LOW即為低電平。

setup():該函數中的代碼只會運行一次。
loop():該函數中的代碼會不斷重復運行。

數字I/O的使用:
1.數字信號:
數字信號是以0、1表示的不連續信號。高電平為數字信號1,低電平為數字信號0。Arduino上每一個帶有數字編號的引腳都是數字引腳,包括寫有“A”編號的模擬輸入引腳。使用這些引腳可以完成輸入/輸出數字


的功能。
使用引腳前,先用pinMode()將引腳設置為某種模式:
INPUT 輸入模式;OUTPUT 輸出模式;INPUT_PULLUP 輸入上拉模式
配置模式之後,用digitalWrite函數使該引腳輸出高電平或低電平。
除了輸出,數字引腳還可以用digitalRead()讀取外部輸入的數字信號,其形式為:digitalRead(pin);
其中參數為引腳編號。
當Arduino以5V供電時,會將範圍為-0.5~1.5V的輸入電壓作為低電平識別,範圍在3~5.5的輸入電壓作為高電平識別。所以即使輸入電壓不太準,Arduino也可以正常識別。
輸出的低電平是0,高電平是當前Arduino板的工作電壓。
Arduino核心庫中,OUTPUT被定義為1,INPUT被定義為0,HIGH是1,LOW是0,可以使用數字代替這些定義。

delay(time):毫秒延時函數。time即為毫秒數。

2.
流水燈程序。

void setup() {
// put your setup code here, to run once:
for (int i=2; i<8; i++) pinMode(i,OUTPUT);
}

void loop() {
// put your main code here, to run repeatedly:
for (int i=2; i<8; i++)
{
digitalWrite(i,1);
delay(1000);
digitalWrite(i,0);
}
for (int i=7; i>1
; i--) { digitalWrite(i,1); delay(1000); digitalWrite(i,0); } }

3.
按鍵控制LED程序。
連接大電阻是為了確保2號引腳一直是低電平。這個電阻被稱為下拉電阻。

/*
Button

Turns on and off a light emitting diode(LED) connected to digital pin 13,
when pressing a pushbutton attached to pin 2.

The circuit:
- LED attached from pin 13 to ground
- pushbutton attached to pin 2 from +5V
- 10K resistor attached to pin 2 from ground

- Note: on most Arduinos there is already an LED on the board
attached to pin 13.

created 2005
by DojoDave <http://www.0j0.org>
modified 30 Aug 2011
by Tom Igoe

This example code is in the public domain.

http://www.arduino.cc/en/Tutorial/Button
*/

// constants won‘t change. They‘re used here to set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin

// variables will change:
int buttonState = 0; // variable for reading the pushbutton status

void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}

void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);

// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
} else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}

倘若去掉下拉電阻,將按鍵一端接2號引腳, 一端接GND,將
pinMode(buttonPin,INPUT); 改為 pinMode(buttonPin,INPUT_PULLUP); 就可以使用引腳內部的上拉電阻。這個電阻也很大,一般為20~50kΩ。它也可以穩定電平,並可以穩定在高電平。


如下程序可實現按一下按鍵燈開,再按一下燈滅。

int buttonPin=2;
int ledPin=13;
boolean ledstate=false;//初始時候燈是滅的

void setup() {
// put your setup code here, to run once:
pinMode(ledPin,OUTPUT);
pinMode(buttonPin,INPUT_PULLUP);

}

void loop() {
// put your main code here, to run repeatedly:
while (digitalRead(buttonPin)==HIGH)
{
if (ledstate==true)
{
digitalWrite(ledPin,LOW);
ledstate=!ledstate;
}
else
{
digitalWrite(ledPin,HIGH);
ledstate=!ledstate;
}
}
delay(500);
}

人體感應燈
使用繼電器模塊與數字傳感器。
1.繼電器模塊。使用電磁繼電器,加壓會使繼電器中產生電流,繼而產生磁力,將開關吸向另一側。
2.人體熱釋電紅外傳感器。無人在其檢測範圍內運動時,模塊保持輸出低電平,有人再其檢測範圍內運動時,模塊輸出一個高電平脈沖信號。
具體代碼思路簡單,只是用到了新函數:
Serial.begin(int num); 打開串口,參數是傳輸速率,有幾個值可供選擇,通常使用9600。
Serial.println(format string); 輸出信息,表現形式和printf差不多。

Arduino學習(一)