- /*
- 翻译:tom
- 时间:2012年11月18日
- IDE版本号:1.01
- 发表地址:www.geek-workshop.com
- 翻译说明: 根据arduino自带的范例进行翻译,可能根据实际的需要略作修改
- */
- /*
- Button
- 打开和关闭一个连接在数字端口13号的发光二极管(LED),当(你)按下连接在2号数字端口的按纽时
- 电路描述:
- *LED 连接13号数字端口至GND
- *按纽连接在2号数字端口至(控制器上)的+5V
- *10K的电阻连接在2号数字至回路
- 备注:在大多数Arduino,都有一个发光LED连接在13号数字端口
- created 2005
- by DojoDave <http://www.0j0.org>
- modified 30 Aug 2011
- by Tom Igoe
- 这个范例代码在公共范围(不受版权制约)
- http://www.arduino.cc/en/Tutorial/Button
- */
- /*电路原理图
-
- */
- //常量不会改变,它们通常用于设置端口数字
- const int buttonPin = 2; //设置按纽的数字端口
-
- const int ledPin = 13; //LED数字端口设置为13
- // 变量将改变
- int buttonState = 0; // 设置按纽状态buttonState为0
- void setup() {
- //初始化数字端口作为信号输出
- pinMode(ledPin, OUTPUT);
- //初始化pushbutton的数字端口作为输入
- pinMode(buttonPin, INPUT);
- }
- void loop(){
- //读取pushbutton的状态值
- buttonState = digitalRead(buttonPin);
- //检查按纽是否被按下
- //如果是,buttonState值为HIGH
-
- if (buttonState == HIGH) {
-
- //打开LED灯
- digitalWrite(ledPin, HIGH);
- }
- else {
- // 关闭LED灯
- digitalWrite(ledPin, LOW);
- }
- }
复制代码 |