九鼎创展论坛中文版English
登录 | 立即注册 设为首页收藏本站 切换到宽版
查看: 4660|回复: 0
打印 上一主题 下一主题

arduino ibox项目实战7-按键读取实验

[复制链接]
跳转到指定楼层
楼主
发表于 2014-10-25 17:49:51 | 只看该作者 |只看大图 回帖奖励 |正序浏览 |阅读模式
硬件平台:PC机一台,ibox卡片电脑一只,arduino扩展板一个
软件平台:WIN7操作系统,android4.0android4.4系统
打开arduino的IDE开发工具,依次点击文件->示例->01.Basics->DigitalReadSerial,DigitalReadSerial的示例程序将会被打开,其源码如下:
  1. // digital pin 24 has a pushbutton attached to it. Give it a name:
  2. int pushButton = KEY0;        //KEY0 = 24;
  3. // the setup routine runs once when you press reset:
  4. void setup() {
  5.   // initialize serial communication at 9600 bits per second:
  6.   Serial.begin(9600);
  7.   // make the pushbutton's pin an input:
  8.   pinMode(pushButton, INPUT);
  9. }

  10. // the loop routine runs over and over again forever:
  11. void loop() {
  12.   // read the input pin:
  13.   int buttonState = digitalRead(pushButton);
  14.   // print out the state of the button:
  15.   Serial.println(buttonState);
  16.   delay(1);        // delay in between reads for stability
  17. }
复制代码
读取按键的程序依然相当简单,在setup函数中设置串口波特率,然后通过pinMode函数将要读取的按键设置为输入,在loop函数中就可以通过digitalRead函数读取按键的状态了。最终按键的状态会在调试窗口上打印出来,如下图所示:

当按住ibox上中间的按键时,调试窗口上不断打印0,松开时,不断打印1

回复

使用道具 举报

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

本版积分规则

QQ|手机版|小黑屋|深圳市九鼎创展科技官方论坛 ( 粤ICP备11028681号-2  

GMT+8, 2024-6-15 13:44 , Processed in 0.019955 second(s), 25 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

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