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

arduino ibox项目实战13-按键控制LED灯实验(一)

[复制链接]
跳转到指定楼层
楼主
发表于 2014-10-25 18:43:22 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
硬件平台:PC机一台,ibox卡片电脑一只,arduino扩展板一个
软件平台:WIN7操作系统,android4.0android4.4系统
打开arduino的IDE开发工具,依次点击文件->示例->01.Basics->Button,Button的示例程序将会被打开,其源码如下:
  1. // constants won't change. They're used here to
  2. // set pin numbers:
  3. const int buttonPin = KEY0;     // the number of the pushbutton pin 24(KEY0)
  4. const int ledPin =  LED1;      // the number of the LED pin 22(LED1)

  5. // variables will change:
  6. int buttonState = 0;         // variable for reading the pushbutton status

  7. void setup() {
  8.   // initialize the LED pin as an output:
  9.   pinMode(ledPin, OUTPUT);
  10.   // initialize the pushbutton pin as an input:
  11.   pinMode(buttonPin, INPUT);
  12. }

  13. void loop() {
  14.   // read the state of the pushbutton value:
  15.   buttonState = digitalRead(buttonPin);

  16.   // check if the pushbutton is pressed.
  17.   // if it is, the buttonState is LOW:
  18.   if (buttonState == LOW) {
  19.     // turn LED on:
  20.     digitalWrite(ledPin, HIGH);
  21.   }
  22.   else {
  23.     // turn LED off:
  24.     digitalWrite(ledPin, LOW);
  25.   }
  26. }
复制代码
       setup函数中,通过pinMode函数将LED控制管脚设置为输出,将按键管脚设置为输入,然后在loop函数中通过digitalRead函数读取按键的状态,若为按下状态,则buttonState0LED灯将被点亮,苦为松开状态,则buttonState1LED灯会熄灭。

回复

使用道具 举报

沙发
发表于 2015-9-16 15:12:21 | 只看该作者
这里涉及到了arduino,这里的LED是在扩展板上的原因,才这样操作吧~
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-5 21:28 , Processed in 0.018901 second(s), 18 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

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