Updated USB examples to include Keyboard and mouse Begin.

This commit is contained in:
Tom Igoe 2012-03-27 15:00:24 -04:00
parent 259a2f18b3
commit 772f4ffa5f
8 changed files with 125 additions and 13 deletions

View File

@ -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:

View File

@ -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;
}

View File

@ -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() {

View File

@ -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() {

View File

@ -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() {

View File

@ -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) {
}

View File

@ -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');
}
}