From 772f4ffa5ffa1840c8d8f16209de57b64ef5da52 Mon Sep 17 00:00:00 2001 From: Tom Igoe Date: Tue, 27 Mar 2012 15:00:24 -0400 Subject: [PATCH] Updated USB examples to include Keyboard and mouse Begin. --- .../DigitalIputPullup/DigitalIputPullup.ino | 0 .../KeyboardLogout/KeyboardLogout.ino | 6 +- .../KeyboardMessage/KeyboardMessage.ino | 15 +-- .../KeyboardReprogram/KeyboardReprogram.ino | 3 + .../KeyboardSerial/KeyboardSerial.ino | 3 + .../ButtonMouseControl/ButtonMouseControl.ino | 4 + .../JoystickMouseControl.ino | 13 ++- .../KeyboardAndMouseControl.ino | 94 +++++++++++++++++++ 8 files changed, 125 insertions(+), 13 deletions(-) rename build/shared/examples/{2.Digital => 02.Digital}/DigitalIputPullup/DigitalIputPullup.ino (100%) rename build/shared/examples/{09.Keyboard => 09. USB (Leonardo only)/Keyboard}/KeyboardLogout/KeyboardLogout.ino (94%) rename build/shared/examples/{09.Keyboard => 09. USB (Leonardo only)/Keyboard}/KeyboardMessage/KeyboardMessage.ino (76%) rename build/shared/examples/{09.Keyboard => 09. USB (Leonardo only)/Keyboard}/KeyboardReprogram/KeyboardReprogram.ino (95%) rename build/shared/examples/{09.Keyboard => 09. USB (Leonardo only)/Keyboard}/KeyboardSerial/KeyboardSerial.ino (88%) rename build/shared/examples/{10.Mouse => 09. USB (Leonardo only)/Mouse}/ButtonMouseControl/ButtonMouseControl.ino (96%) rename build/shared/examples/{10.Mouse => 09. USB (Leonardo only)/Mouse}/JoystickMouseControl/JoystickMouseControl.ino (96%) create mode 100644 build/shared/examples/9. USB (Leonardo only)/KeyboardAndMouseControl/KeyboardAndMouseControl.ino diff --git a/build/shared/examples/2.Digital/DigitalIputPullup/DigitalIputPullup.ino b/build/shared/examples/02.Digital/DigitalIputPullup/DigitalIputPullup.ino similarity index 100% rename from build/shared/examples/2.Digital/DigitalIputPullup/DigitalIputPullup.ino rename to build/shared/examples/02.Digital/DigitalIputPullup/DigitalIputPullup.ino diff --git a/build/shared/examples/09.Keyboard/KeyboardLogout/KeyboardLogout.ino b/build/shared/examples/09. USB (Leonardo only)/Keyboard/KeyboardLogout/KeyboardLogout.ino similarity index 94% rename from build/shared/examples/09.Keyboard/KeyboardLogout/KeyboardLogout.ino rename to build/shared/examples/09. USB (Leonardo only)/Keyboard/KeyboardLogout/KeyboardLogout.ino index 6234f3bca..adb502a34 100644 --- a/build/shared/examples/09.Keyboard/KeyboardLogout/KeyboardLogout.ino +++ b/build/shared/examples/09. USB (Leonardo only)/Keyboard/KeyboardLogout/KeyboardLogout.ino @@ -17,6 +17,7 @@ * wire to connect D2 to ground. created 6 Mar 2012 + modified 27 Mar 2012 by Tom Igoe This example is in the public domain @@ -36,6 +37,7 @@ void setup() { // pullup resistor so it goes high unless // connected to ground: pinMode(2, INPUT_PULLUP); + Keyboard.begin(); } void loop() { @@ -54,7 +56,7 @@ void loop() { delay(100); Keyboard.releaseAll(); // enter: - Keyboard.type(KEY_RETURN); + Keyboard.write(KEY_RETURN); break; case WINDOWS: // CTRL-ALT-DEL: @@ -77,7 +79,7 @@ void loop() { delay(1000); Keyboard.releaseAll(); // Enter to confirm logout: - Keyboard.type(KEY_RETURN); + Keyboard.write(KEY_RETURN); break; } // do nothing: diff --git a/build/shared/examples/09.Keyboard/KeyboardMessage/KeyboardMessage.ino b/build/shared/examples/09. USB (Leonardo only)/Keyboard/KeyboardMessage/KeyboardMessage.ino similarity index 76% rename from build/shared/examples/09.Keyboard/KeyboardMessage/KeyboardMessage.ino rename to build/shared/examples/09. USB (Leonardo only)/Keyboard/KeyboardMessage/KeyboardMessage.ino index 503c78f5e..6b226b558 100644 --- a/build/shared/examples/09.Keyboard/KeyboardMessage/KeyboardMessage.ino +++ b/build/shared/examples/09. USB (Leonardo only)/Keyboard/KeyboardMessage/KeyboardMessage.ino @@ -4,11 +4,11 @@ Sends a text string when a button is pressed. The circuit: - * pushbutton attached from pin 4 to +5V + * pushbutton attached from pin 2 to +5V * 10-kilohm resistor attached from pin 4 to ground created 24 Oct 2011 - modified 19 Mar 2012 + modified 27 Mar 2012 by Tom Igoe This example code is in the public domain. @@ -16,13 +16,15 @@ http://www.arduino.cc/en/Tutorial/KeyboardButton */ -const int buttonPin = 4; // input pin for pushbutton +const int buttonPin = 2; // input pin for pushbutton int previousButtonState = HIGH; // for checking the state of a pushButton int counter = 0; // button push counter void setup() { // make the pushButton pin an input: pinMode(buttonPin, INPUT); + // initialize control over the keyboard: + Keyboard.begin(); } void loop() { @@ -30,9 +32,9 @@ void loop() { int buttonState = digitalRead(buttonPin); // if the button state has changed, if ((buttonState != previousButtonState) - // and it's currently pressed: - && (buttonState == HIGH)) { - // increment the button counter + // and it's currently pressed: + && (buttonState == HIGH)) { + // increment the button counter counter++; // type out a message Keyboard.print("You pressed the button "); @@ -42,3 +44,4 @@ void loop() { // save the current button state for comparison next time: previousButtonState = buttonState; } + diff --git a/build/shared/examples/09.Keyboard/KeyboardReprogram/KeyboardReprogram.ino b/build/shared/examples/09. USB (Leonardo only)/Keyboard/KeyboardReprogram/KeyboardReprogram.ino similarity index 95% rename from build/shared/examples/09.Keyboard/KeyboardReprogram/KeyboardReprogram.ino rename to build/shared/examples/09. USB (Leonardo only)/Keyboard/KeyboardReprogram/KeyboardReprogram.ino index 5c54b4cc1..9facdaba8 100644 --- a/build/shared/examples/09.Keyboard/KeyboardReprogram/KeyboardReprogram.ino +++ b/build/shared/examples/09. USB (Leonardo only)/Keyboard/KeyboardReprogram/KeyboardReprogram.ino @@ -15,6 +15,7 @@ * wire to connect D2 to ground. created 5 Mar 2012 + modified 27 Mar 2012 by Tom Igoe This example is in the public domain @@ -33,6 +34,8 @@ void setup() { // pullup resistor so it goes high unless // connected to ground: pinMode(2, INPUT_PULLUP); + // initialize control over the keyboard: + Keyboard.begin(); } void loop() { diff --git a/build/shared/examples/09.Keyboard/KeyboardSerial/KeyboardSerial.ino b/build/shared/examples/09. USB (Leonardo only)/Keyboard/KeyboardSerial/KeyboardSerial.ino similarity index 88% rename from build/shared/examples/09.Keyboard/KeyboardSerial/KeyboardSerial.ino rename to build/shared/examples/09. USB (Leonardo only)/Keyboard/KeyboardSerial/KeyboardSerial.ino index b64b6272b..7ee7f7ad7 100644 --- a/build/shared/examples/09.Keyboard/KeyboardSerial/KeyboardSerial.ino +++ b/build/shared/examples/09. USB (Leonardo only)/Keyboard/KeyboardSerial/KeyboardSerial.ino @@ -9,6 +9,7 @@ * none created 21 Oct 2011 + modified 27 Mar 2012 by Tom Igoe This example code is in the public domain. @@ -19,6 +20,8 @@ This example code is in the public domain. void setup() { // open the serial port: Serial.begin(9600); + // initialize control over the keyboard: + Keyboard.begin(); } void loop() { diff --git a/build/shared/examples/10.Mouse/ButtonMouseControl/ButtonMouseControl.ino b/build/shared/examples/09. USB (Leonardo only)/Mouse/ButtonMouseControl/ButtonMouseControl.ino similarity index 96% rename from build/shared/examples/10.Mouse/ButtonMouseControl/ButtonMouseControl.ino rename to build/shared/examples/09. USB (Leonardo only)/Mouse/ButtonMouseControl/ButtonMouseControl.ino index 2c3619f7a..6dcaf5cdd 100644 --- a/build/shared/examples/10.Mouse/ButtonMouseControl/ButtonMouseControl.ino +++ b/build/shared/examples/09. USB (Leonardo only)/Mouse/ButtonMouseControl/ButtonMouseControl.ino @@ -1,3 +1,4 @@ + /* ButtonMouseControl @@ -14,6 +15,7 @@ over your mouse! Make sure you have control before you use the mouse commands. created 15 Mar 2012 + modified 27 Mar 2012 by Tom Igoe this code is in the public domain @@ -38,6 +40,8 @@ void setup() { pinMode(leftButton, INPUT); pinMode(rightButton, INPUT); pinMode(mouseButton, INPUT); + // initialize mouse control: + Mouse.begin(); } void loop() { diff --git a/build/shared/examples/10.Mouse/JoystickMouseControl/JoystickMouseControl.ino b/build/shared/examples/09. USB (Leonardo only)/Mouse/JoystickMouseControl/JoystickMouseControl.ino similarity index 96% rename from build/shared/examples/10.Mouse/JoystickMouseControl/JoystickMouseControl.ino rename to build/shared/examples/09. USB (Leonardo only)/Mouse/JoystickMouseControl/JoystickMouseControl.ino index 16bb98414..64650d5dd 100644 --- a/build/shared/examples/10.Mouse/JoystickMouseControl/JoystickMouseControl.ino +++ b/build/shared/examples/09. USB (Leonardo only)/Mouse/JoystickMouseControl/JoystickMouseControl.ino @@ -21,7 +21,7 @@ you can turn on and off mouse control. created 15 Sept 2011 - updated 15 Mar 2012 + updated 27 Mar 2012 by Tom Igoe this code is in the public domain @@ -67,13 +67,15 @@ void loop() { int xReading = readAxis(A0); int yReading = readAxis(A1); + // take control of the mouse: + Mouse.begin(); // if the mouse control state is active, move the mouse: if (mouseIsActive) { Mouse.move(xReading, yReading, 0); } - + // read the mouse button and click or not click: - // if the mouse button is pressed: + // if the mouse button is pressed: if (digitalRead(mouseButton) == HIGH) { // if the mouse is not pressed, press it: if (!Mouse.isPressed(MOUSE_LEFT)) { @@ -87,7 +89,7 @@ void loop() { Mouse.release(MOUSE_LEFT); } } - + delay(responseDelay); } @@ -106,7 +108,7 @@ int readAxis(int thisAxis) { // if the output reading is outside from the // rest position threshold, use it: int distance = reading - center; - + if (abs(distance) < threshold) { distance = 0; } @@ -116,3 +118,4 @@ int readAxis(int thisAxis) { } + diff --git a/build/shared/examples/9. USB (Leonardo only)/KeyboardAndMouseControl/KeyboardAndMouseControl.ino b/build/shared/examples/9. USB (Leonardo only)/KeyboardAndMouseControl/KeyboardAndMouseControl.ino new file mode 100644 index 000000000..21019be45 --- /dev/null +++ b/build/shared/examples/9. USB (Leonardo only)/KeyboardAndMouseControl/KeyboardAndMouseControl.ino @@ -0,0 +1,94 @@ + +/* + ButtonMouseControl + + Controls the mouse from five pushbuttons on an Arduino Leonardo. + + Hardware: + * 5 pushbuttons attached to D2, D3, D4, D5, D6 + + + The mouse movement is always relative. This sketch reads + four pushbuttons, and uses them to set the movement of the mouse. + + WARNING: When you use the Mouse.move() command, the Arduino takes + over your mouse! Make sure you have control before you use the mouse commands. + + created 15 Mar 2012 + modified 27 Mar 2012 + by Tom Igoe + + this code is in the public domain + + */ + +// set pin numbers for the five buttons: + +// set pin numbers for the five buttons: +const int upButton = 2; +const int downButton = 3; +const int leftButton = 4; +const int rightButton = 5; +const int mouseButton = 6; + +void setup() { // initialize the buttons' inputs: + pinMode(upButton, INPUT); + pinMode(downButton, INPUT); + pinMode(leftButton, INPUT); + pinMode(rightButton, INPUT); + pinMode(mouseButton, INPUT); + + Serial.begin(9600); + // initialize mouse control: + Mouse.begin(); + Keyboard.begin(); +} + +void loop() { + // use serial input to control the mouse: + if (Serial.available() > 0) { + char inChar = Serial.read(); + + switch (inChar) { + case 'u': + // move mouse up + Mouse.move(0, -40); + break; + case 'd': + // move mouse down + Mouse.move(0, 40); + break; + case 'l': + // move mouse left + Mouse.move(-40, 0); + break; + case 'r': + // move mouse right + Mouse.move(40, 0); + break; + case 'm': + // move mouse right + Mouse.click(MOUSE_LEFT); + break; + } + } + + // use the pushbuttons to control the keyboard: + if (digitalRead(upButton) == HIGH) { + Keyboard.write('u'); + } + if (digitalRead(downButton) == HIGH) { + Keyboard.write('d'); + } + if (digitalRead(leftButton) == HIGH) { + Keyboard.write('l'); + } + if (digitalRead(rightButton) == HIGH) { + Keyboard.write('r'); + } + if (digitalRead(mouseButton) == HIGH) { + Keyboard.write('m'); + } + +} +