Tutorial: How to Use Arduino Uno as HID | Part 2: Arduino Mouse Emulation
Introduction
In this tutorial, we will explore the mouse emulation in Arduino Uno. This should be the part 2 in using the Arduino Uno as Human Interface Device.
You may also want to visit the part 1 here:
Tutorial: How to use Arduino Uno as HID | Arduino Keyboard Emulation
Circuit Diagram
Video Demonstration
Firmware Links:
Arduino Mouse firmware
Arduino Original firmware
Source Code
1
2/* Arduino USB Mouse HID demo */
3/* Author: Darran Hunt
4 * Release into the public domain.
5 *
6 * Modified: George Bantique
7 * TechToTinker
8 */
9
10//#define ORIGINAL_CODE // to display the original code
11#define MODIFIED_CODE // to display the modified code
12//#define SERIAL_DEBUG // to overwrite the mouse hid character with printable character
13
14struct {
15 uint8_t buttons;
16 int8_t x;
17 int8_t y;
18 int8_t wheel; /* Not yet implemented */
19} mouseReport;
20
21uint8_t nullReport[4] = { 0, 0, 0, 0 };
22
23void setup();
24void loop();
25
26
27#ifdef MODIFIED_CODE
28
29#define ONBOARD_LED 13
30#define SR_PIN A2
31#define XR_PIN A1
32#define YR_PIN A0
33
34int xr_rest, yr_rest;
35
36void setup()
37{
38 Serial.begin(9600);
39
40 pinMode(ONBOARD_LED, OUTPUT);
41 pinMode(SR_PIN, INPUT_PULLUP);
42 pinMode(XR_PIN, INPUT);
43 pinMode(YR_PIN, INPUT);
44
45 mouseReport.buttons = 0;
46 mouseReport.x = 0;
47 mouseReport.y = 0;
48 mouseReport.wheel = 0;
49
50 // save the rest value
51 xr_rest = analogRead(XR_PIN);
52 yr_rest = analogRead(YR_PIN);
53}
54
55void loop()
56{
57 uint8_t s_temp;
58 int x_temp, y_temp, ind;
59
60 if (digitalRead(SR_PIN)) {
61 s_temp = 0;
62 } else {
63 s_temp = 1;
64 }
65
66 int xr_val = analogRead(XR_PIN);
67 int yr_val = analogRead(YR_PIN);
68
69 if (xr_val == xr_rest) {
70 // joystick is not move
71 x_temp = 0;
72 } else if (xr_val < xr_rest) {
73 // joystick to the left
74 x_temp = - map(xr_val, 0, xr_rest, 10, 0);
75 } else {
76 // joystick to the right
77 x_temp = map(xr_val, xr_rest, 1023, 0, 10);
78 }
79
80 if (yr_val == yr_rest) {
81 // joystick is not move
82 y_temp = 0;
83 } else if (yr_val < yr_rest) {
84 // joystick to the left
85 y_temp = - map(yr_val, 0,yr_rest, 10, 0);
86 } else {
87 // joystick to the right
88 y_temp = map(yr_val, yr_rest,1023, 0, 10);
89 }
90
91#ifdef SERIAL_DEBUG
92 Serial.print("X: ");
93 Serial.println(x_temp);
94 Serial.print("Y: ");
95 Serial.println(y_temp);
96#endif // SERIAL_DEBUG
97
98 mouseReport.buttons = s_temp;
99 mouseReport.x = x_temp;
100 mouseReport.y = y_temp;
101 mouseReport.wheel = 0;
102
103 for (int ind = 0; ind < 20; ind++) {
104 Serial.write((uint8_t *)&mouseReport, 4);
105 Serial.write((uint8_t *)&nullReport, 4);
106 }
107}
108#endif // MODIFIED_CODE
109
110#ifdef ORIGINAL_CODE
111void setup()
112{
113 Serial.begin(9600);
114 delay(200);
115}
116
117/* Move the mouse in a clockwise square every 5 seconds */
118void loop()
119{
120 int ind;
121 delay(1000);
122
123 mouseReport.buttons = 0;
124 mouseReport.x = 0;
125 mouseReport.y = 0;
126 mouseReport.wheel = 0;
127
128 // Going left
129 mouseReport.x = -2;
130 for (ind = 0; ind < 20; ind++) {
131 Serial.write((uint8_t *) &mouseReport, 4);
132 Serial.write((uint8_t *) &nullReport, 4);
133 }
134
135 // Going up
136 mouseReport.x = 0;
137 mouseReport.y = -2;
138 for (ind = 0; ind < 20; ind++) {
139 Serial.write((uint8_t *)&mouseReport, 4);
140 Serial.write((uint8_t *)&nullReport, 4);
141 }
142
143 // Going right
144 mouseReport.x = 2;
145 mouseReport.y = 0;
146 for (ind = 0; ind < 20; ind++) {
147 Serial.write((uint8_t *)&mouseReport, 4);
148 Serial.write((uint8_t *)&nullReport, 4);
149 }
150
151 // Going down
152 mouseReport.x = 0;
153 mouseReport.y = 2;
154 for (ind = 0; ind < 20; ind++) {
155 Serial.write((uint8_t *)&mouseReport, 4);
156 Serial.write((uint8_t *)&nullReport, 4);
157 }
158}
159#endif // ORIGINAL_CODE
Call To Action
If you found this tutorial, please help me spread this tutorial to as many as possible so that it could help and inspire others.
Please don’t forget to Subscribe to TechToTinker Youtube channel:
Click this to SUBSCRIBE
Thank you and have a good day.
Posts in this series
- How to Get Started with ATTiny85 in Arduino IDE
- Tutorial: How to use MFRC522 RFID module using Arduino
- SOS Flasher Using Millis Function with Enable Switch
- Tutorial: How to use DS3231 RTC in Arduino
- Tutorial: How to use 0.96 OLED - a small and cute display
- Tutorial: Getting Started with the NRF24L01 | How to use | Arduino
- Tutorial: How to use SIM800L GSM Module for Controlling Anything | Arduino
- Tutorial: How to use Keypad | Text Entry Mode | Arduino
- Tutorial: How to use 4x4 Keypad | Arduino
- Project Idea: Arduino Voltmeter
- Project Idea: Door Lock Security | Arduino
- Multitasking with Arduino | Relay Timer Controller | using millis
- Tutorial Understanding Blink Without Delay | How to millis
- Arduino Simple LCD Menu
- How to use SIM800L GSM Module using Arduino | Make or Answer Voice Calls
- Tutorial: How to Use Arduino Uno as HID | Part 1: Arduino Keyboard Emulation
- Tutorial: How to use SIM800L DTMF to Control Anything | Arduino
- Tutorial: Arduino EEPROM
- How to use SIM800L GSM Module | Arduino | Send and Receive SMS
- 16x2 LCD Menu for Arduino
- Tutorial: Arduino GPIO | How to use Arduino Pins
- MIT App Inventor for Arduino
- RC Car using L298N, HC-06, and Arduino Uno
- How to Use LCD Keypad Shield for Arduino
- How to Use Arduino Interrupts
- Project: Automatic Alcohol Dispenser
- TUTORIAL: How to use HC-SR04 Ultrasonic Sensor with Arduino
- Source Code: Astronomia Meme and Funeral Dance | melodies the Arduino way
- How to Get Started with L293D Motor Driver Shield with Arduino
- How to Get Started with L298N Motor Driver module using Arduino
- Part 2: Wav Music Player with Lyrics Using Arduino and SD Card
- Interfacing Infrared to Arduino Uno
- Part 1: Wav Music Player Using Arduino Uno and SD Card
- How to Interface Stepper Motor to Arduino Uno
- How To Play MP3 Files on Arduino from SD Card
- What is Arduino Software Serial
- How to Interface SD card to Arduino (without SD card shield)?
- Playing Melodies Using Arduino
- 8 Degrees Of Freedom (DOF) Robot Using Arduino Uno
- How to Interface PS2 Controller to Arduino Uno
- Part 3: DF Player Mini Tinkering with Arduino Nano and LCD
- How to Interface HC-06 to Arduino
- How to make a Remote Control RC car using Arduino and HC-06 bluetooth module
- Part 2: DF Player Mini Tinkering with Arduino Nano
- Part 1: DF Player Mini - a mini cheap mp3 player
No comments yet!