I have built a gaming controller out of the arduino esplora and a 5 position switch, using the five position switch to control the mouse(switch mouse style), however when plugged in it just moves the mouse cursor down for some weird reason can somebody please help
here is my code with explanations included
#include <Esplora.h>
#include <Mouse.h>
#include <Keyboard.h>
const int upButton = 20; // do I really need to take notes on this one for myself
const int downButton = 1; // Dude if I cant understand this it's hopeless
const int leftButton = 28; //
const int rightButton = 10; //
const int mouseButton = 21;//
int range = 5;
int responseDelay = 10;
boolean buttonStates[8];
const byte buttons[] = {
JOYSTICK_DOWN,
JOYSTICK_LEFT,
JOYSTICK_UP,
JOYSTICK_RIGHT,
SWITCH_RIGHT,
SWITCH_LEFT,
SWITCH_UP,
SWITCH_DOWN,
};
const char keystrokes[] = {
'S',
'A',
'W',
'D',
'Q',
0x09,
'E',
'v'
};
//You can change the keys to whatever you want.
void setup()
{
pinMode(upButton, INPUT); // tells arduino that this constant is input
pinMode(downButton, INPUT); // tells arduino that this constant is input
pinMode(leftButton, INPUT); // tells arduino that this constant is input
pinMode(rightButton, INPUT); // tells arduino that this constant is input
pinMode(mouseButton, INPUT); // tells arduino that this constant is input
Mouse.begin(); // take control of the mouse
Keyboard.begin(); // takes control of keyboard
}
void loop(){{
int upState = digitalRead(upButton);
int downState = digitalRead(downButton);
int rightState = digitalRead(rightButton);
int leftState = digitalRead(leftButton);
int clickState = digitalRead(mouseButton);
int xDistance = (leftState - rightState) * range;
int yDistance = (upState - downState) * range;
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);
}
int valueSlider = Esplora.readSlider();
if(digitalRead(valueSlider) > 800){
Keyboard.press('R');
Keyboard.release('R');
}
//BELOW THIS LINE IS AKASH'S STUFF DO NOT TWEAK
for(byte thisButton = 0; thisButton < 8; thisButton++) {
boolean lastState = buttonStates[thisButton];
boolean newState = Esplora.readButton(buttons[thisButton]);
if (lastState != newState) {
if (newState == PRESSED) {
Keyboard.press(keystrokes[thisButton]);
}
else if (newState == RELEASED) {
Keyboard.release(keystrokes[thisButton]);
}
}
buttonStates[thisButton] = newState;
}
delay(50);
int yAxis = Esplora.readAccelerometer(Y_AXIS);
if (yAxis >= -15) { Keyboard.press(KEY_LEFT_SHIFT); delay(50);
Keyboard.release(KEY_LEFT_SHIFT);
};
if (yAxis <= 15) {
Keyboard.press(KEY_LEFT_CTRL);
delay(50);
Keyboard.release(KEY_LEFT_CTRL);
};
delay(50);
}