Hi,
i have build a jostick with an arduino micro for a disabled person, that he can play small games.
It emulates the arrow keys and i also added buttons for the most common keys like return, space and ctrl.
Everything works fine until i want to use it for a flash-game (in browser). it doesn't recognize any of the inputs.
Can somebody explain me why and how to fix this.
also the ctrl-key doesn't work at all.
Here is the code. Please don't judge me for it. It's really quick and dirty copy'n'paste.
/*
ButtonMouseControl
Controls the mouse from five pushbuttons on an Arduino Leonardo or Micro.
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 = 7;
const int downButton = 4;
const int leftButton = 5;
const int rightButton = 6;
const int mouseButtonleft = 2;
const int mouseButtonright = 8;
const int Button3 = 3;
const int Button4 = 9;
int range = 1; // output range of X or Y movement; affects movement speed
int responseDelay = 12; // response delay of the mouse, in ms
int zeit = 1;
float range2 = 1 ;
int state = HIGH; // the current state of the output pin
int reading; // the current reading from the input pin
int previous = LOW; // the previous reading from the input pin
// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0; // the last time the output pin was toggled
long debounce = 200; // the debounce time, increase if the output flickers
void setup() {
// initialize the buttons' inputs:
pinMode(upButton, INPUT_PULLUP);
pinMode(downButton, INPUT_PULLUP);
pinMode(leftButton, INPUT_PULLUP);
pinMode(rightButton, INPUT_PULLUP);
pinMode(mouseButtonleft, INPUT_PULLUP);
pinMode(mouseButtonright, INPUT_PULLUP);
pinMode(Button3, INPUT_PULLUP);
pinMode(Button4, INPUT_PULLUP);
Mouse.begin();
delay(3000);
}
void loop() {
reading = digitalRead(Button4);
// read the buttons:
int upState = digitalRead(upButton);
int downState = digitalRead(downButton);
int rightState = digitalRead(rightButton);
int leftState = digitalRead(leftButton);
int clickStateleft = digitalRead(mouseButtonleft);
int clickStateright = digitalRead(mouseButtonright);
int clickState3 = digitalRead(Button3);
// int clickState4 = digitalRead(Button4);
// calculate the movement distance based on the button states:
int xDistance = (leftState - rightState) * range;
int yDistance = (upState - downState) * range;
// if the input just went from LOW and HIGH and we've waited long enough
// to ignore any noise on the circuit, toggle the output pin and remember
// the time
if (reading == HIGH && previous == LOW && millis() - time > debounce) {
if (state == HIGH)
state = LOW;
else
state = HIGH;
time = millis();
}
if (state == LOW) {
// if X or Y is non-zero, move:
if (((xDistance != 0) || (yDistance != 0)) && (zeit > 50)) {
Mouse.move(((leftState - rightState)*range2), ((upState - downState)*range2), 0);
range2 += 0.01;
}
else {
Mouse.move(xDistance, yDistance, 0);
zeit ++;
}
if (digitalRead(upButton) && digitalRead(downButton) && digitalRead(rightButton) && digitalRead(leftButton)) {
zeit = 1;
range2 = 1;
}
if (range2 > 5) {
range2 -= 0.01;
}
//-----------------------------------------------------------------
// if the left mouse button is pressed:
if (clickStateleft == LOW) {
Mouse.press(MOUSE_LEFT);
}
// else the mouse button is not pressed:
else {
// if the mouse is pressed, release it:
Mouse.release(MOUSE_LEFT);
}
//-----------------------------------------------------------------
// if the right mouse button is pressed:
if (clickStateright == LOW) {
// if the mouse is not pressed, press it:
Mouse.press(MOUSE_RIGHT);
}
// else the mouse button is not pressed:
else {
//if the mouse is pressed, release it:
Mouse.release(MOUSE_RIGHT);
}
//-----------------------------------------------------------------
//Down
if (clickState3 == LOW) {
Keyboard.write(KEY_DOWN_ARROW);
delay(100);
}
// a delay so the mouse doesn't move too fast:
delay(responseDelay);
//delay (range2);
}
else {
//-----------------------------------------------------------------
//Up
if (upState == LOW) {
Keyboard.write(KEY_UP_ARROW);
delay(300);
}
//-----------------------------------------------------------------
//Down
if (downState == LOW) {
Keyboard.write(KEY_DOWN_ARROW);
delay(300);
}
//-----------------------------------------------------------------
//Right
if (rightState == LOW) {
Keyboard.write(KEY_RIGHT_ARROW);
delay(300);
}
//-----------------------------------------------------------------
//Left
if (leftState == LOW) {
Keyboard.write(KEY_LEFT_ARROW);
delay(300);
}
//-----------------------------------------------------------------
//RedButton
if (clickStateleft == LOW) {
Keyboard.write(KEY_RETURN);
delay(500);
}
//-----------------------------------------------------------------
//GreenButton
if (clickStateright == LOW) {
Keyboard.write((char) 32);
delay(500);
}
//-----------------------------------------------------------------
//YellowButton
if (clickState3 == LOW) {
Keyboard.write(KEY_LEFT_CTRL);
delay(300);
}
}
previous = reading;
}