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.