#include <Bounce.h>
/// Define Pins
const int startEmulation = 2; // switch to turn on and off mouse emulation
/////////////////////////////////////////////////////////////////////////////////
const int joystickX = A1; // joystick X axis
const int joystickY = A0; // joystick Y axis
// parameters for reading the joystick:
int cursorSpeed = 8; // output speed of X or Y movement
// halving 7 (center, below) makes 3.5 which will only be stored as 3 in an integer, doesn't seem 'good' so I have changed above to 8 - Rob.
int responseDelay = 20; // response delay of the mouse, in ms
// I made responseDelay bigger, maybe I should not have, try other values till you like movement of mouse for thumbstick - Rob.
int threshold = cursorSpeed / 4; // resting threshold
int center = cursorSpeed / 2; // resting position value
boolean mouseIsActive = false; // whether or not to control the mouse
int lastSwitchState = LOW; // previous switch stat
//////////////////////////////////////////////////////////////////////////////////////
// Create Bounce objects for each button. The Bounce object
// automatically deals with contact chatter or "bounce", and
// it makes detecting changes very simple.
Bounce button3 = Bounce(4, 10); // if a button is too "sensitive"
Bounce button4 = Bounce(5, 10); // to rapid touch, you can
Bounce button5 = Bounce(3, 10); // increase this time.
byte left = 0;
byte middle = 0;
byte right = 0;
/////////////////////////////////////////////////////////////////////////////////
void setup() {
pinMode(startEmulation, INPUT_PULLUP); // the switch pin
///////////////////////////////////////////////////////////////////////////////////
// Configure the pins for input mode with pullup resistors.
// The pushbuttons connect from each pin to ground. When
// the button is pressed, the pin reads LOW because the button
// shorts it to ground. When released, the pin reads HIGH
// because the pullup resistor connects to +5 volts inside
// the chip. LOW for "on", and HIGH for "off" may seem
// backwards, but using the on-chip pullup resistors is very
// convenient. The scheme is called "active low", and it's
// very commonly used in electronics... so much that the chip
// has built-in pullup resistors!
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
//////////////////////////////////////////////////////////////////////////////////
Mouse.begin(); // take control of the mouse
}
elapsedMillis moveDelay;
void loop() {
// read the switch:
int switchState = digitalRead(startEmulation);
// if it's changed and it's high, toggle the mouse state:
if (switchState != lastSwitchState) {
if (switchState == LOW) {
mouseIsActive = !mouseIsActive;
}
}
// save switch state for next loop:
lastSwitchState = switchState;
// read and scale the two axes:
if(moveDelay>responseDelay-1)
{
moveDelay=0;
int xReading = readAxis(A1);
int yReading = readAxis(A0);
// if the mouse control state is active, move the mouse:
if (mouseIsActive) {
Mouse.move(xReading, yReading, 0); // (x, y, scroll mouse wheel)
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// Update all the buttons. There should not be any long
// delays in loop(), so this runs repetitively at a rate
// faster than the buttons could be pressed and released.
button3.update();
button4.update();
button5.update();
// this variable will let us know if any button changes
byte anyChange = 0;
// Check each button for "falling" edge.
// Change a mouse button when each button presses
// Update the Joystick buttons only upon changes.
// falling = high (not pressed - voltage from pullup resistor)
// to low (pressed - button connects pin to ground)
if (button3.fallingEdge()) {
left = 1;
anyChange = 1;
}
if (button4.fallingEdge()) {
middle = 1;
anyChange = 1;
}
if (button5.fallingEdge()) {
right = 1;
anyChange = 1;
}
// Check each button for "rising" edge
// Change a mouse button when each button releases.
// For many types of projects, you only care when the button
// is pressed and the release isn't needed.
// rising = low (pressed - button connects pin to ground)
// to high (not pressed - voltage from pullup resistor)
if (button3.risingEdge()) {
left = 0;
anyChange = 1;
}
if (button4.risingEdge()) {
middle = 0;
anyChange = 1;
}
if (button5.risingEdge()) {
right = 0;
anyChange = 1;
}
// if any changes were made, update the Mouse buttons
if (anyChange) {
Mouse.set_buttons(left, middle, right);
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////
/*
reads an axis (0 or 1 for x or y) and scales the
analog input range to a range from 0 to <range>
*/
int readAxis(int thisAxis) {
// read the analog input:
int reading = analogRead(thisAxis);
// map the reading from the analog input range to the output range:
reading = map(reading, 0, 1023, 0, cursorSpeed);
// if the output reading is outside from the
// rest position threshold, use it:
int distance = reading - center;
if (abs(distance) < threshold) {
distance = 0;
}
// return the distance for this axis:
return distance;
}