极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 10770|回复: 0

[翻译]]Arduino自带范例Digital之Button

[复制链接]
发表于 2012-11-18 17:36:37 | 显示全部楼层 |阅读模式
  1. /*
  2. 翻译:tom
  3. 时间:2012年11月18日
  4. IDE版本号:1.01
  5. 发表地址:www.geek-workshop.com
  6. 翻译说明: 根据arduino自带的范例进行翻译,可能根据实际的需要略作修改

  7. */

  8. /*
  9.   Button
  10. 打开和关闭一个连接在数字端口13号的发光二极管(LED),当(你)按下连接在2号数字端口的按纽时

  11. 电路描述:
  12. *LED 连接13号数字端口至GND
  13. *按纽连接在2号数字端口至(控制器上)的+5V
  14. *10K的电阻连接在2号数字至回路

  15. 备注:在大多数Arduino,都有一个发光LED连接在13号数字端口

  16. created 2005
  17. by DojoDave <http://www.0j0.org>
  18. modified 30 Aug 2011
  19. by Tom Igoe

  20. 这个范例代码在公共范围(不受版权制约)
  21. http://www.arduino.cc/en/Tutorial/Button
  22. */
  23. /*电路原理图


  24. */

  25. //常量不会改变,它们通常用于设置端口数字
  26. const int buttonPin = 2;    //设置按纽的数字端口

  27. const int ledPin =  13;      //LED数字端口设置为13
  28. // 变量将改变
  29. int buttonState = 0;         // 设置按纽状态buttonState为0
  30. void setup() {
  31.   //初始化数字端口作为信号输出
  32.   pinMode(ledPin, OUTPUT);      
  33. //初始化pushbutton的数字端口作为输入
  34.    pinMode(buttonPin, INPUT);     
  35. }

  36. void loop(){
  37. //读取pushbutton的状态值
  38.   buttonState = digitalRead(buttonPin);
  39. //检查按纽是否被按下
  40. //如果是,buttonState值为HIGH

  41.   if (buttonState == HIGH) {     
  42.   
  43. //打开LED灯
  44.     digitalWrite(ledPin, HIGH);  
  45.   }
  46.   else {
  47.     // 关闭LED灯
  48.     digitalWrite(ledPin, LOW);
  49.   }
  50. }
复制代码

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-4-20 00:03 , Processed in 0.039003 second(s), 18 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表