之前,我们介绍过如何用Arduino Uno打造一个PPT遥控器【参考1】,缺点是制作过程复杂,用到的器件较多导致整体稳定性不好。这里介绍使用 Arduino Pro Micro来做一个同样的PPT遥控器。
原理上和之前的并没有多少差别,都是通过模拟USB键盘的方式来进行控制。差别在于Pro Micro是缩小版的 Leonardo ,内部集成了USB Slave控制器,我们不需要再花费精力模拟自己为USB Keyboard.
硬件连接示意图:
代码是直接修改自示例“KeyboardMessage”
/* Keyboard Button test For the Arduino Leonardo and Micro. Sends a text string when a button is pressed. The circuit: * pushbutton attached from pin 2 to +5V * 10-kilohm resistor attached from pin 4 to ground created 24 Oct 2011 modified 27 Mar 2012 by Tom Igoe This example code is in the public domain. http://www.arduino.cc/en/Tutorial/KeyboardButton */ const int buttonPinA = A1; // input pin for A const int buttonPinB = A2; // input pin for B int previousButtonStateA = HIGH; // for checking the state of a pushButton int previousButtonStateB = HIGH; // for checking the state of a pushButton void setup() { // make the pushButtonA pin an input: pinMode(buttonPinA, INPUT); // make the pushButtonB pin an input: pinMode(buttonPinB, INPUT); // initialize control over the keyboard: Keyboard.begin(); delay(10000); } void loop() { // read the pushbutton: int buttonStateA = digitalRead(buttonPinA); int buttonStateB = digitalRead(buttonPinB); // if the button state has changed, if ((buttonStateA != previousButtonStateA) // and it's currently pressed: && (buttonStateA == HIGH)) { // type out a message //Keyboard.print("A"); Keyboard.press(KEY_LEFT_ARROW); Keyboard.releaseAll(); } // if the button state has changed, if ((buttonStateB != previousButtonStateB) // and it's currently pressed: && (buttonStateB == HIGH)) { // type out a message //Keyboard.print("B"); Keyboard.press(KEY_RIGHT_ARROW); Keyboard.releaseAll(); } // save the current button state for comparison next time: previousButtonStateA = buttonStateA; previousButtonStateB = buttonStateB; }
实物图
工作的视频
http://www.tudou.com/listplay/mAjgE5Ac_Sg/Fz0piTvQ9Cs.html?resourceId=414535982_06_02_99
参考:
1. http://www.lab-z.com/%E7%94%A8-arduino-%E6%89%93%E9%80%A0ppt%E9%81%A5%E6%8E%A7%E5%99%A8/ 用 Arduino 打造PPT遥控器
2. http://arduino.cc/en/Reference/KeyboardModifiers 键值定义