Skip to content

Commit 43eb3b5

Browse files
author
Tom Igoe
committed
Moved Mouse and Keyboard examples for Leonardo into the core examples folder
1 parent 220a2cb commit 43eb3b5

File tree

3 files changed

+191
-0
lines changed

3 files changed

+191
-0
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/*
2+
JoystickMouseControl
3+
4+
Controls the mouse from a joystick on an Arduino Leonardo.
5+
Uses a pushbutton to turn on and off mouse control.
6+
7+
The mouse movement is always relative. This sketch reads
8+
two analog inputs that range from 0 to 1023 (or less on either end)
9+
and translates them into ranges of -60 to 60.
10+
The sketch assumes that the joystick resting values are around the
11+
middle of the range, but that they vary within a threshold.
12+
13+
WARNING: When you use the Mouse.move() command, the Arduino takes
14+
over your mouse! Make sure you have control before you use the command.
15+
This sketch includes a pushbutton to toggle the mouse control state, so
16+
you can turn on and off mouse control.
17+
18+
created 15 Sept 2011
19+
by Tom Igoe
20+
21+
this code is in the public domain
22+
23+
*/
24+
25+
// set pin numbers for switch, joystick axes, and LED:
26+
const int switchPin = 6; // switch to turn on and off mouse control
27+
const int xAxis = A1; // joystick X axis
28+
const int yAxis = A2; // joystick Y axis
29+
const int ledPin = 5; // Mouse control LED
30+
31+
// parameters for reading the joystick:
32+
int range = 12; // output range of X or Y movement
33+
int responseDelay = 2; // response delay of the mouse, in ms
34+
int threshold = range/4; // resting threshold
35+
int center = range/2; // resting position value
36+
int minima[] = {
37+
1023, 1023}; // actual analogRead minima for {x, y}
38+
int maxima[] = {
39+
0,0}; // actual analogRead maxima for {x, y}
40+
int axis[] = {
41+
xAxis, yAxis}; // pin numbers for {x, y}
42+
int mouseReading[2]; // final mouse readings for {x, y}
43+
44+
45+
boolean mouseIsActive = false; // whether or not to control the mouse
46+
int lastSwitchState = LOW; // previous switch state
47+
48+
void setup() {
49+
pinMode(switchPin, INPUT); // the switch pin
50+
pinMode(ledPin, OUTPUT); // the LED pin
51+
}
52+
53+
void loop() {
54+
// read the switch:
55+
int switchState = digitalRead(switchPin);
56+
// if it's changed and it's high, toggle the mouse state:
57+
if (switchState != lastSwitchState) {
58+
if (switchState == HIGH) {
59+
mouseIsActive = !mouseIsActive;
60+
// turn on LED to indicate mouse state:
61+
digitalWrite(ledPin, mouseIsActive);
62+
}
63+
}
64+
// save switch state for next comparison:
65+
lastSwitchState = switchState;
66+
67+
// read and scale the two axes:
68+
int xReading = readAxis(0);
69+
int yReading = readAxis(1);
70+
71+
// if the mouse control state is active, move the mouse:
72+
if (mouseIsActive) {
73+
Mouse.move(xReading, yReading, 0);
74+
}
75+
delay(responseDelay);
76+
}
77+
78+
/*
79+
reads an axis (0 or 1 for x or y) and scales the
80+
analog input range to a range from 0 to <range>
81+
*/
82+
83+
int readAxis(int axisNumber) {
84+
int distance = 0; // distance from center of the output range
85+
86+
// read the analog input:
87+
int reading = analogRead(axis[axisNumber]);
88+
89+
// of the current reading exceeds the max or min for this axis,
90+
// reset the max or min:
91+
if (reading < minima[axisNumber]) {
92+
minima[axisNumber] = reading;
93+
}
94+
if (reading > maxima[axisNumber]) {
95+
maxima[axisNumber] = reading;
96+
}
97+
98+
// map the reading from the analog input range to the output range:
99+
reading = map(reading, minima[axisNumber], maxima[axisNumber], 0, range);
100+
101+
// if the output reading is outside from the
102+
// rest position threshold, use it:
103+
if (abs(reading - center) > threshold) {
104+
distance = (reading - center);
105+
}
106+
107+
// the Y axis needs to be inverted in order to
108+
// map the movemment correctly:
109+
if (axisNumber == 1) {
110+
distance = -distance;
111+
}
112+
113+
// return the distance for this axis:
114+
return distance;
115+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
Keyboard Button test
3+
4+
Sends a text string when a button is pressed.
5+
6+
The circuit:
7+
* pushbutton attached from pin 4 to +5V
8+
* 10-kilohm resistor attached from pin 4 to ground
9+
10+
created 24 Oct 2011
11+
by Tom Igoe
12+
13+
This example code is in the public domain.
14+
15+
http://www.arduino.cc/en/Tutorial/KeyboardButton
16+
*/
17+
18+
const int buttonPin = 4; // input pin for pushbutton
19+
int previousButtonState = HIGH; // for checking the state of a pushButton
20+
int counter = 0; // button push counter
21+
22+
void setup() {
23+
// make the pushButton pin an input:
24+
pinMode(buttonPin, INPUT);
25+
}
26+
27+
void loop() {
28+
// read the pushbutton:
29+
int buttonState = digitalRead(buttonPin);
30+
// if the button state has changed,
31+
if ((buttonState != previousButtonState)
32+
// and it's currently pressed:
33+
&& (buttonState == HIGH)) {
34+
// increment the button counter
35+
counter++;
36+
// type out a message
37+
Keyboard.print("You pressed the button: ");
38+
Keyboard.print(counter);
39+
Keyboard.println(" times.");
40+
}
41+
// save the current button state for comparison next time:
42+
previousButtonState = buttonState;
43+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
Keyboard test
3+
4+
Reads a byte from the serial port, sends a keystroke back.
5+
The sent keystroke is one higher than what's received, e.g.
6+
if you send a, you get b, send A you get B, and so forth.
7+
8+
The circuit:
9+
* none
10+
11+
created 21 Oct 2011
12+
by Tom Igoe
13+
14+
This example code is in the public domain.
15+
16+
http://www.arduino.cc/en/Tutorial/KeyboardSerial
17+
*/
18+
19+
void setup() {
20+
// open the serial port:
21+
Serial.begin(9600);
22+
}
23+
24+
void loop() {
25+
// check for incoming serial data:
26+
if (Serial.available() > 0) {
27+
// read incoming serial data:
28+
char inChar = Serial.read();
29+
// Type the next ASCII value from what you received:
30+
Keyboard.write(inChar+1);
31+
}
32+
}
33+

0 commit comments

Comments
 (0)