pinMode()
描述:将指定的引脚配置为充当输入或输出。
语法:pinMode(pin, mode)
参数:pin:要设置其模式的引脚编号;mode:输出(OUTPUT)模式、输入(INPUT)模式、输入上拉(INPUT_PULLUP)模式
例子:
int ledPin = 13; // LED connected to digital pin 13
void setup()
{
pinMode(ledPin, OUTPUT); // sets the digital pin as output
}
void loop()
{
digitalWrite(ledPin, HIGH); // sets the LED on
delay(1000); // waits for a second
digitalWrite(ledPin, LOW); // sets the LED off
delay(1000); // waits for a second
}
digitalWrite()
描述:将高值或低值写入数字引脚。
语法:digitalWrite(pin, value)
参数:pin:引脚编号;value:HIGH、LOW
例子:
int ledPin = 13; // LED connected to digital pin 13
void setup()
{
pinMode(ledPin, OUTPUT); // sets the digital pin as output
}
void loop()
{
digitalWrite(ledPin, HIGH); // sets the LED on
delay(1000); // waits for a second
digitalWrite(ledPin, LOW); // sets the LED off
delay(1000); // waits for a second
}
digitalRead()
描述:从指定的数字引脚(高电平或低电平)读取值。
语法:digitalRead(pin)
参数:pin:要读取的数字引脚的编号(整数)
例子:
int ledPin = 13; // LED connected to digital pin 13
int inPin = 7; // pushbutton connected to digital pin 7
int val = 0; // variable to store the read value
void setup()
{
pinMode(ledPin, OUTPUT); // sets the digital pin 13 as output
pinMode(inPin, INPUT); // sets the digital pin 7 as input
}
void loop()
{
val = digitalRead(inPin); // read the input pin
digitalWrite(ledPin, val); // sets the LED to the button's value
}