digitalRead()

描述

從指定的數位 pin 腳讀取數值,得到的值只會是 HIGHLOW

語法


digitalRead(pin)

參數

pin: 欲讀取之數位 pin 腳的編號 (int 型別)

回傳

HIGHLOW

範例

把 pin 13 設定成和 pin 7 讀到的值相同,pin 7 設定為輸入端。

int ledPin = 13; // LED 與 pin 13 連接
int inPin = 7;   // 按鈕與 pin 7 連接
int val = 0;     // 用 val 變數來儲存讀取到的數值

void setup()
{
  pinMode(ledPin, OUTPUT);      // 將數位 pin 腳 13 設定為輸出
  pinMode(inPin, INPUT);      // 將數位 pin 腳 7 設定為輸入
}

void loop()
{
  val = digitalRead(inPin);   // 讀取輸入 pin 腳
  digitalWrite(ledPin, val);    // 將 LED 設定成按鈕的數值
}

注意

如果 pin 腳沒有連接任何東西,digitalRead() 可能會回傳 HIGH 或者 LOW (它是隨機改變的)。

跟 Arduino 不一樣,86Duino 的類比輸入 pin 腳 (像是 A0、A1 等) 不能被當作數位 pin 腳。

See also

pinMode()
digitalWrite()


語法參考主頁面

本頁由熱血青年 LBU 譯自英文版。

The text of the 86Duino reference is a modification of the Arduino reference, and is licensed under a Creative Commons Attribution-ShareAlike 3.0 License. Code samples in the reference are released into the public domain.