九鼎创展论坛
标题: arduino ibox项目实战13-按键控制LED灯实验(一) [打印本页]
作者: armeasy 时间: 2014-10-25 18:43
标题: arduino ibox项目实战13-按键控制LED灯实验(一)
硬件平台:PC机一台,ibox卡片电脑一只,arduino扩展板一个
软件平台:WIN7操作系统,android4.0或android4.4系统
打开arduino的IDE开发工具,依次点击文件->示例->01.Basics->Button,Button的示例程序将会被打开,其源码如下:
- // constants won't change. They're used here to
- // set pin numbers:
- const int buttonPin = KEY0; // the number of the pushbutton pin 24(KEY0)
- const int ledPin = LED1; // the number of the LED pin 22(LED1)
-
- // variables will change:
- int buttonState = 0; // variable for reading the pushbutton status
-
- void setup() {
- // initialize the LED pin as an output:
- pinMode(ledPin, OUTPUT);
- // initialize the pushbutton pin as an input:
- pinMode(buttonPin, INPUT);
- }
-
- void loop() {
- // read the state of the pushbutton value:
- buttonState = digitalRead(buttonPin);
-
- // check if the pushbutton is pressed.
- // if it is, the buttonState is LOW:
- if (buttonState == LOW) {
- // turn LED on:
- digitalWrite(ledPin, HIGH);
- }
- else {
- // turn LED off:
- digitalWrite(ledPin, LOW);
- }
- }
复制代码 在setup函数中,通过pinMode函数将LED控制管脚设置为输出,将按键管脚设置为输入,然后在loop函数中通过digitalRead函数读取按键的状态,若为按下状态,则buttonState为0,LED灯将被点亮,苦为松开状态,则buttonState为1,LED灯会熄灭。
作者: studylinuxlong 时间: 2015-9-16 15:12
这里涉及到了arduino,这里的LED是在扩展板上的原因,才这样操作吧~
欢迎光临 九鼎创展论坛 (http://bbs.9tripod.com/) |
Powered by Discuz! X3.2 |