const int buttonPin = 2;const int ledPin = 13;将开关的状态保存在buttonState值内 int buttonState = 0; 将LED接口设置为输出口;将开关接口设置为输入口 pinMode(ledPin, OUTPUT);pinMode(buttonPin, INPUT);使用digitalRead功能检查开关状态 buttonState = digitalRead(buttonPin);如果开关被按,那么。。。
int reading = digitalRead(buttonPin); 将开关的状态读取到本地变量 // check to see if you just pressed the button // (i.e. the input went from LOW to HIGH), and you've waited long enough // since the last press to ignore any noise: 检查是否刚刚已经按了按钮 (也就是说输入从 LOW ...
pinMode(LED,OUTPUT);//设定LED引脚为输出接口 pinMode(BUTTON,INPUT);//设定BUTTON引脚为输入接口 } void loop() { val=digitalRead(BUTTON);//读取按钮状态并存储至val //检测按钮状态变化过程(设定LED灯状态) //如果你的按钮开关返回值和我的相反,请修改这个条件语句,将LOW和HIGH交换一下就可以了。 if((...
pinMode(BUTTON, INPUT_PULLUP);//INPUT —— 输入模式 OUTPUT —— 输出模式 INPUT_PULLUP —— 输入上拉模式 Serial.begin(9600);//用于串口输出 } void loop(){ val = digitalRead(BUTTON);//读取输入数值并且存储 if((val==HIGH)&&(old_val==LOW))//检查按钮的变化情况 { Serial.println("Input"...
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 ...
使用digitalRead功能检查开关状态 buttonState = digitalRead(buttonPin); 如果开关被按,那么。。。 if (buttonState == HIGH) { 将LED灯打开 digitalWrite(ledPin, HIGH); 如果是其他状态(开关没有被按)。。。 } else { 将LED灯熄灭 digitalWrite(ledPin, LOW); ...
pinMode(Button2, INPUT_PULLUP); //按钮2引脚设置为输入上拉模式 } void loop() { // 读取输入引脚的值 boolean button1State = digitalRead(Button1); boolean button2State = digitalRead(Button2); // 打印结果到串口 Serial.print("button1:"); ...
intbuttonState = 0; voidsetup() { pinMode(ledPin, OUTPUT); pinMode(buttonPin, INPUT); } voidloop() { buttonState = digitalRead(buttonPin); if(buttonState == HIGH) { digitalWrite(ledPin, LOW); }else{ digitalWrite(ledPin, HIGH); ...
while(digitalRead(Button)==HIGH) //判断按钮状态,如果仍然按下的话,等待松开。防止一直按住导致LED输出端连续反转{delay(1);} }}}下面是这段代码的效果:当没有下拉电阻的时候,可以明显看到下面的示波器波形,按键断开时低电平漂浮不定,这样子容易出现误判。看按键按下与松开的波形,也能明显感觉到低电平不稳。
pinMode(Button1, INPUT);//按钮1引脚设置为输入 pinMode(Button2, INPUT_PULLUP); //按钮2引脚设置为输入上拉模式 } void loop() { // 读取输入引脚的值 boolean button1State = digitalRead(Button1); boolean button2State = digitalRead(Button2); ...