Skip to content

Commit 772f4ff

Browse files
author
Tom Igoe
committed
Updated USB examples to include Keyboard and mouse Begin.
1 parent 259a2f1 commit 772f4ff

File tree

8 files changed

+125
-13
lines changed

8 files changed

+125
-13
lines changed

build/shared/examples/09.Keyboard/KeyboardLogout/KeyboardLogout.ino renamed to build/shared/examples/09. USB (Leonardo only)/Keyboard/KeyboardLogout/KeyboardLogout.ino

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
* wire to connect D2 to ground.
1818
1919
created 6 Mar 2012
20+
modified 27 Mar 2012
2021
by Tom Igoe
2122
2223
This example is in the public domain
@@ -36,6 +37,7 @@ void setup() {
3637
// pullup resistor so it goes high unless
3738
// connected to ground:
3839
pinMode(2, INPUT_PULLUP);
40+
Keyboard.begin();
3941
}
4042

4143
void loop() {
@@ -54,7 +56,7 @@ void loop() {
5456
delay(100);
5557
Keyboard.releaseAll();
5658
// enter:
57-
Keyboard.type(KEY_RETURN);
59+
Keyboard.write(KEY_RETURN);
5860
break;
5961
case WINDOWS:
6062
// CTRL-ALT-DEL:
@@ -77,7 +79,7 @@ void loop() {
7779
delay(1000);
7880
Keyboard.releaseAll();
7981
// Enter to confirm logout:
80-
Keyboard.type(KEY_RETURN);
82+
Keyboard.write(KEY_RETURN);
8183
break;
8284
}
8385
// do nothing:

build/shared/examples/09.Keyboard/KeyboardMessage/KeyboardMessage.ino renamed to build/shared/examples/09. USB (Leonardo only)/Keyboard/KeyboardMessage/KeyboardMessage.ino

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,37 @@
44
Sends a text string when a button is pressed.
55
66
The circuit:
7-
* pushbutton attached from pin 4 to +5V
7+
* pushbutton attached from pin 2 to +5V
88
* 10-kilohm resistor attached from pin 4 to ground
99
1010
created 24 Oct 2011
11-
modified 19 Mar 2012
11+
modified 27 Mar 2012
1212
by Tom Igoe
1313
1414
This example code is in the public domain.
1515
1616
http://www.arduino.cc/en/Tutorial/KeyboardButton
1717
*/
1818

19-
const int buttonPin = 4; // input pin for pushbutton
19+
const int buttonPin = 2; // input pin for pushbutton
2020
int previousButtonState = HIGH; // for checking the state of a pushButton
2121
int counter = 0; // button push counter
2222

2323
void setup() {
2424
// make the pushButton pin an input:
2525
pinMode(buttonPin, INPUT);
26+
// initialize control over the keyboard:
27+
Keyboard.begin();
2628
}
2729

2830
void loop() {
2931
// read the pushbutton:
3032
int buttonState = digitalRead(buttonPin);
3133
// if the button state has changed,
3234
if ((buttonState != previousButtonState)
33-
// and it's currently pressed:
34-
&& (buttonState == HIGH)) {
35-
// increment the button counter
35+
// and it's currently pressed:
36+
&& (buttonState == HIGH)) {
37+
// increment the button counter
3638
counter++;
3739
// type out a message
3840
Keyboard.print("You pressed the button ");
@@ -42,3 +44,4 @@ void loop() {
4244
// save the current button state for comparison next time:
4345
previousButtonState = buttonState;
4446
}
47+

build/shared/examples/09.Keyboard/KeyboardReprogram/KeyboardReprogram.ino renamed to build/shared/examples/09. USB (Leonardo only)/Keyboard/KeyboardReprogram/KeyboardReprogram.ino

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* wire to connect D2 to ground.
1616
1717
created 5 Mar 2012
18+
modified 27 Mar 2012
1819
by Tom Igoe
1920
2021
This example is in the public domain
@@ -33,6 +34,8 @@ void setup() {
3334
// pullup resistor so it goes high unless
3435
// connected to ground:
3536
pinMode(2, INPUT_PULLUP);
37+
// initialize control over the keyboard:
38+
Keyboard.begin();
3639
}
3740

3841
void loop() {

build/shared/examples/09.Keyboard/KeyboardSerial/KeyboardSerial.ino renamed to build/shared/examples/09. USB (Leonardo only)/Keyboard/KeyboardSerial/KeyboardSerial.ino

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* none
1010
1111
created 21 Oct 2011
12+
modified 27 Mar 2012
1213
by Tom Igoe
1314
1415
This example code is in the public domain.
@@ -19,6 +20,8 @@ This example code is in the public domain.
1920
void setup() {
2021
// open the serial port:
2122
Serial.begin(9600);
23+
// initialize control over the keyboard:
24+
Keyboard.begin();
2225
}
2326

2427
void loop() {

build/shared/examples/10.Mouse/ButtonMouseControl/ButtonMouseControl.ino renamed to build/shared/examples/09. USB (Leonardo only)/Mouse/ButtonMouseControl/ButtonMouseControl.ino

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
/*
23
ButtonMouseControl
34
@@ -14,6 +15,7 @@
1415
over your mouse! Make sure you have control before you use the mouse commands.
1516
1617
created 15 Mar 2012
18+
modified 27 Mar 2012
1719
by Tom Igoe
1820
1921
this code is in the public domain
@@ -38,6 +40,8 @@ void setup() {
3840
pinMode(leftButton, INPUT);
3941
pinMode(rightButton, INPUT);
4042
pinMode(mouseButton, INPUT);
43+
// initialize mouse control:
44+
Mouse.begin();
4145
}
4246

4347
void loop() {

build/shared/examples/10.Mouse/JoystickMouseControl/JoystickMouseControl.ino renamed to build/shared/examples/09. USB (Leonardo only)/Mouse/JoystickMouseControl/JoystickMouseControl.ino

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
you can turn on and off mouse control.
2222
2323
created 15 Sept 2011
24-
updated 15 Mar 2012
24+
updated 27 Mar 2012
2525
by Tom Igoe
2626
2727
this code is in the public domain
@@ -67,13 +67,15 @@ void loop() {
6767
int xReading = readAxis(A0);
6868
int yReading = readAxis(A1);
6969

70+
// take control of the mouse:
71+
Mouse.begin();
7072
// if the mouse control state is active, move the mouse:
7173
if (mouseIsActive) {
7274
Mouse.move(xReading, yReading, 0);
7375
}
74-
76+
7577
// read the mouse button and click or not click:
76-
// if the mouse button is pressed:
78+
// if the mouse button is pressed:
7779
if (digitalRead(mouseButton) == HIGH) {
7880
// if the mouse is not pressed, press it:
7981
if (!Mouse.isPressed(MOUSE_LEFT)) {
@@ -87,7 +89,7 @@ void loop() {
8789
Mouse.release(MOUSE_LEFT);
8890
}
8991
}
90-
92+
9193
delay(responseDelay);
9294
}
9395

@@ -106,7 +108,7 @@ int readAxis(int thisAxis) {
106108
// if the output reading is outside from the
107109
// rest position threshold, use it:
108110
int distance = reading - center;
109-
111+
110112
if (abs(distance) < threshold) {
111113
distance = 0;
112114
}
@@ -116,3 +118,4 @@ int readAxis(int thisAxis) {
116118
}
117119

118120

121+
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
2+
/*
3+
ButtonMouseControl
4+
5+
Controls the mouse from five pushbuttons on an Arduino Leonardo.
6+
7+
Hardware:
8+
* 5 pushbuttons attached to D2, D3, D4, D5, D6
9+
10+
11+
The mouse movement is always relative. This sketch reads
12+
four pushbuttons, and uses them to set the movement of the mouse.
13+
14+
WARNING: When you use the Mouse.move() command, the Arduino takes
15+
over your mouse! Make sure you have control before you use the mouse commands.
16+
17+
created 15 Mar 2012
18+
modified 27 Mar 2012
19+
by Tom Igoe
20+
21+
this code is in the public domain
22+
23+
*/
24+
25+
// set pin numbers for the five buttons:
26+
27+
// set pin numbers for the five buttons:
28+
const int upButton = 2;
29+
const int downButton = 3;
30+
const int leftButton = 4;
31+
const int rightButton = 5;
32+
const int mouseButton = 6;
33+
34+
void setup() { // initialize the buttons' inputs:
35+
pinMode(upButton, INPUT);
36+
pinMode(downButton, INPUT);
37+
pinMode(leftButton, INPUT);
38+
pinMode(rightButton, INPUT);
39+
pinMode(mouseButton, INPUT);
40+
41+
Serial.begin(9600);
42+
// initialize mouse control:
43+
Mouse.begin();
44+
Keyboard.begin();
45+
}
46+
47+
void loop() {
48+
// use serial input to control the mouse:
49+
if (Serial.available() > 0) {
50+
char inChar = Serial.read();
51+
52+
switch (inChar) {
53+
case 'u':
54+
// move mouse up
55+
Mouse.move(0, -40);
56+
break;
57+
case 'd':
58+
// move mouse down
59+
Mouse.move(0, 40);
60+
break;
61+
case 'l':
62+
// move mouse left
63+
Mouse.move(-40, 0);
64+
break;
65+
case 'r':
66+
// move mouse right
67+
Mouse.move(40, 0);
68+
break;
69+
case 'm':
70+
// move mouse right
71+
Mouse.click(MOUSE_LEFT);
72+
break;
73+
}
74+
}
75+
76+
// use the pushbuttons to control the keyboard:
77+
if (digitalRead(upButton) == HIGH) {
78+
Keyboard.write('u');
79+
}
80+
if (digitalRead(downButton) == HIGH) {
81+
Keyboard.write('d');
82+
}
83+
if (digitalRead(leftButton) == HIGH) {
84+
Keyboard.write('l');
85+
}
86+
if (digitalRead(rightButton) == HIGH) {
87+
Keyboard.write('r');
88+
}
89+
if (digitalRead(mouseButton) == HIGH) {
90+
Keyboard.write('m');
91+
}
92+
93+
}
94+

0 commit comments

Comments
 (0)