硬件平台: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灯会熄灭。
|