硬件平台:PC机一台,ibox卡片电脑一只,arduino扩展板一个 软件平台:WIN7操作系统,android4.0或android4.4系统 打开arduino的IDE开发工具,依次点击文件->示例->01.Basics->DigitalReadSerial,DigitalReadSerial的示例程序将会被打开,其源码如下: - // digital pin 24 has a pushbutton attached to it. Give it a name:
- int pushButton = KEY0; //KEY0 = 24;
- // the setup routine runs once when you press reset:
- void setup() {
- // initialize serial communication at 9600 bits per second:
- Serial.begin(9600);
- // make the pushbutton's pin an input:
- pinMode(pushButton, INPUT);
- }
-
- // the loop routine runs over and over again forever:
- void loop() {
- // read the input pin:
- int buttonState = digitalRead(pushButton);
- // print out the state of the button:
- Serial.println(buttonState);
- delay(1); // delay in between reads for stability
- }
复制代码 读取按键的程序依然相当简单,在setup函数中设置串口波特率,然后通过pinMode函数将要读取的按键设置为输入,在loop函数中就可以通过digitalRead函数读取按键的状态了。最终按键的状态会在调试窗口上打印出来,如下图所示:
当按住ibox上中间的按键时,调试窗口上不断打印0,松开时,不断打印1。
|