在arduino中R如何变成按下按钮才会刷新

在arduino中R如何变成按下按钮才会刷新,第1张

设置一个按钮计数器,判断按钮放开的次数,做不同的动作。按键检测上,要检测按钮放开的动作。

您可以像按下按钮一样使用Arduino Uno防护板的重置按钮。数据存储在SRAM中,复位后将保留。

使用一些简单的代码,Arduino可以作为键盘呈现给你的电脑,能够输入文字,按住键和触发键盘快捷键。您甚至可以将Arduino编码为鼠标或媒体控制器。

使用按键(PUSHBUTTON)控制LED灯号的开关,当按键被按下时打开LED灯号,按键放开时关闭LED灯号。

材料

Arduino的主板×1

LED×1

按钮或开关开关×1

10K电阻×1

面包板×1

单心线X N

接线

把LED接到PIN13,长脚(阳极)接到PIN13,短脚(阴极)接到GND;

按钮一支脚接到+5 V;

PIN2接到按钮的另一支脚,同一支脚位接一个10K的电阻连到GND;

源码如下:

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)

}

说明:

L01〜L02:定义按键与LED的脚位,按键接在PIN2码,而LED接在PIN13;

L16:读取按键的状态并保存到buttonState变数里;

L20〜L26:这支程式的目的是按下按键时要打开LED灯号,放开按键时要关闭的LED灯号,因此,假如buttonState为高,代表按键状态是按下(压制)的,此时要打开LED,反之,假如buttonState为低,代表按键状态是放开的,此时要关闭LED。

注:这支是Arduino的内建的程序,点选

File >Examples >2.Digital >Button

就可以找到。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/tougao/11551374.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-16
下一篇 2023-05-16

发表评论

登录后才能评论

评论列表(0条)

保存