1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-08-05 13:16:13 +03:00
Files
.settings
app
arduino-builder
arduino-core
build
cmd
javadoc
linux
macosx
shared
examples
01.Basics
02.Digital
03.Analog
04.Communication
05.Control
06.Sensors
07.Display
08.Strings
09.USB
Keyboard
KeyboardAndMouseControl
Mouse
ButtonMouseControl
ButtonMouseControl.ino
ButtonMouseControl.txt
layout.png
schematic.png
JoystickMouseControl
10.StarterKit
ArduinoISP
icons
lib
tools
Edison_help_files-1.6.2.zip.sha
Galileo_help_files-1.6.2.zip.sha
manpage.adoc
reference-1.6.0.zip.sha
revisions.txt
windows
.gitignore
build.xml
build_all_dist.bash
build_board_manager_package.sh
build_pull_request.bash
create_reference.pl
fetch.sh
howto.txt
libastylej-2.05.zip.sha
docs
hardware
libraries
.classpath
.gitignore
.project
.travis.yml
README.Arduino.md
README.md
appveyor.yml
format.every.sketch.sh
lib_sync
license.txt
esp8266/build/shared/examples/09.USB/Mouse/ButtonMouseControl/ButtonMouseControl.ino
2013-10-21 09:58:40 +02:00

84 lines
2.1 KiB
C++

/*
ButtonMouseControl
For Leonardo and Due boards only.
Controls the mouse from five pushbuttons on an Arduino Leonardo, Micro or Due.
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:
const int upButton = 2;
const int downButton = 3;
const int leftButton = 4;
const int rightButton = 5;
const int mouseButton = 6;
int range = 5; // output range of X or Y movement; affects movement speed
int responseDelay = 10; // response delay of the mouse, in ms
void setup() {
// initialize the buttons' inputs:
pinMode(upButton, INPUT);
pinMode(downButton, INPUT);
pinMode(leftButton, INPUT);
pinMode(rightButton, INPUT);
pinMode(mouseButton, INPUT);
// initialize mouse control:
Mouse.begin();
}
void loop() {
// read the buttons:
int upState = digitalRead(upButton);
int downState = digitalRead(downButton);
int rightState = digitalRead(rightButton);
int leftState = digitalRead(leftButton);
int clickState = digitalRead(mouseButton);
// calculate the movement distance based on the button states:
int xDistance = (leftState - rightState) * range;
int yDistance = (upState - downState) * range;
// if X or Y is non-zero, move:
if ((xDistance != 0) || (yDistance != 0)) {
Mouse.move(xDistance, yDistance, 0);
}
// if the mouse button is pressed:
if (clickState == HIGH) {
// if the mouse is not pressed, press it:
if (!Mouse.isPressed(MOUSE_LEFT)) {
Mouse.press(MOUSE_LEFT);
}
}
// else the mouse button is not pressed:
else {
// if the mouse is pressed, release it:
if (Mouse.isPressed(MOUSE_LEFT)) {
Mouse.release(MOUSE_LEFT);
}
}
// a delay so the mouse doesn't move too fast:
delay(responseDelay);
}