硬件平台:PC机一台,ibox卡片电脑一只,arduino扩展板一个 软件平台:WIN7操作系统,android4.0或android4.4系统 打开arduino的IDE开发工具,依次点击文件->示例->01.Basics->Debounce,Debounce的示例程序将会被打开,其源码如下: - // 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 ledState = HIGH; // the current state of the output pin
- int buttonState; // the current reading from the input pin
- int lastButtonState = LOW; // the previous reading from the input pin
-
- // the following variables are long's because the time, measured in miliseconds,
- // will quickly become a bigger number than can be stored in an int.
- long lastDebounceTime = 0; // the last time the output pin was toggled
- long debounceDelay = 50; // the debounce time; increase if the output flickers
-
- void setup() {
- pinMode(buttonPin, INPUT);
- pinMode(ledPin, OUTPUT);
-
- // set initial LED state
- digitalWrite(ledPin, ledState);
- }
-
- void loop() {
- // read the state of the switch into a local variable:
- int reading = digitalRead(buttonPin);
-
- // check to see if you just pressed the button
- // (i.e. the input went from LOW to HIGH), and you've waited
- // long enough since the last press to ignore any noise:
-
- // If the switch changed, due to noise or pressing:
- if (reading != lastButtonState) {
- // reset the debouncing timer
- lastDebounceTime = millis();
- }
-
- if ((millis() - lastDebounceTime) > debounceDelay) {
- // whatever the reading is at, it's been there for longer
- // than the debounce delay, so take it as the actual current state:
-
- // if the button state has changed:
- if (reading != buttonState) {
- buttonState = reading;
-
- // only toggle the LED if the new button state is HIGH
- if (buttonState == HIGH) {
- ledState = !ledState;
- }
- }
- }
-
- // set the LED:
- digitalWrite(ledPin, ledState);
-
- // save the reading. Next time through the loop,
- // it'll be the lastButtonState:
- lastButtonState = reading;
- }
复制代码本程序将会采集按键数据,每按一次,将会对ledState取反一次。因此,按按一下ibox上中间的按钮,LED灯状态会变一次。注意,程序中两次通过if语句判断按键的状态,是用于消除按键抖动。否则,每按一次按键时,LED灯将会不断的开关数次,最终灯的状态也是未知的。
|