九鼎创展论坛

标题: arduino ibox项目实战13-按键控制LED灯实验(一) [打印本页]

作者: armeasy    时间: 2014-10-25 18:43
标题: arduino ibox项目实战13-按键控制LED灯实验(一)
硬件平台: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灯会熄灭。


作者: studylinuxlong    时间: 2015-9-16 15:12
这里涉及到了arduino,这里的LED是在扩展板上的原因,才这样操作吧~




欢迎光临 九鼎创展论坛 (http://bbs.9tripod.com/) Powered by Discuz! X3.2