Hi, I'm trying to move the mouse cursor with an arcade stick wired to a Teensy LC. In my original code (borrowed from another project) I can't get the mouse cursor to stop moving. I'm just trying to get pin0 to work first and then I'll figure out the rest later. With code borrowed from the official Arduino page the cursor moves erratically and only pin 3 even works remotely correctly. (could be my wiring, I don't have resistors on the buttons currently.)
I just accidentally deleted my code, here's what I started with and I'm rewriting it. I will update the post in a few here.
And the Ardunio code I just tried:
I just accidentally deleted my code, here's what I started with and I'm rewriting it. I will update the post in a few here.
Code:
/* USB Arcade Stick
* All Buttons assigned as Keyboard keys for easy use with Emulation
*
* Original code from the Teensy Examples.
*/
#include
// Create Bounce objects for each button. The Bounce object
// automatically deals with contact chatter or "bounce", and
// it makes detecting changes very simple.
Bounce button0 = Bounce(0, 10);
Bounce button1 = Bounce(1, 10); // 10 = 10 ms debounce time
Bounce button2 = Bounce(2, 10); // which is appropriate for
Bounce button3 = Bounce(3, 10); // most mechanical pushbuttons
Bounce button4 = Bounce(4, 10);
Bounce button5 = Bounce(5, 10); // if a button is too "sensitive"
Bounce button6 = Bounce(6, 10); // to rapid touch, you can
Bounce button7 = Bounce(7, 10); // increase this time.
Bounce button8 = Bounce(8, 10);
Bounce button9 = Bounce(9, 10);
Bounce button10 = Bounce(10, 10);
Bounce button11 = Bounce(11, 10);
Bounce button12 = Bounce(12, 10);
Bounce button13 = Bounce(13, 10);
void setup() {
// 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(0, INPUT_PULLUP);
pinMode(1, INPUT_PULLUP);
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
pinMode(6, INPUT_PULLUP); // Teensy++ LED, may need 1k resistor pullup
pinMode(7, INPUT_PULLUP);
pinMode(8, INPUT_PULLUP);
pinMode(9, INPUT_PULLUP);
pinMode(10, INPUT_PULLUP);
pinMode(11, INPUT_PULLUP);
pinMode(12, INPUT_PULLUP);
pinMode(13, INPUT_PULLUP);
}
void loop() {
// 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.
button0.update();
button1.update();
button2.update();
button3.update();
button4.update();
button5.update();
button6.update();
button7.update();
button8.update();
button9.update();
button10.update();
button11.update();
button12.update();
button13.update();
// Check each button for "falling" edge.
// Type a message on the Keyboard 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 (button0.fallingEdge()) {
Keyboard.press(KEY_LEFT);
}
if (button1.fallingEdge()) {
Keyboard.press(KEY_UP);
}
if (button2.fallingEdge()) {
Keyboard.press(KEY_DOWN);
}
if (button3.fallingEdge()) {
Keyboard.press(KEY_RIGHT);
}
if (button4.fallingEdge()) {
Keyboard.press(KEY_5);
}
if (button5.fallingEdge()) {
Keyboard.press(KEY_1);
}
if (button6.fallingEdge()) {
Keyboard.press(KEY_Z);
}
if (button7.fallingEdge()) {
Keyboard.press(KEY_X);
}
if (button8.fallingEdge()) {
Keyboard.press(KEY_C);
}
if (button9.fallingEdge()) {
Keyboard.press(KEY_A);
}
if (button10.fallingEdge()) {
Keyboard.press(KEY_S);
}
if (button11.fallingEdge()) {
Keyboard.press(KEY_D);
}
if (button12.fallingEdge()) {
Keyboard.press(KEY_ESC);
}
if (button13.fallingEdge()) {
Keyboard.press(KEY_F);
}
// Check each button for "rising" edge
// Type a message on the Keyboard 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 (button0.risingEdge()) {
Keyboard.release(KEY_LEFT);
}
if (button1.risingEdge()) {
Keyboard.release(KEY_UP);
}
if (button2.risingEdge()) {
Keyboard.release(KEY_DOWN);
}
if (button3.risingEdge()) {
Keyboard.release(KEY_RIGHT);
}
if (button4.risingEdge()) {
Keyboard.release(KEY_5);
}
if (button5.risingEdge()) {
Keyboard.release(KEY_1);
}
if (button6.risingEdge()) {
Keyboard.release(KEY_Z);
}
if (button7.risingEdge()) {
Keyboard.release(KEY_X);
}
if (button8.risingEdge()) {
Keyboard.release(KEY_C);
}
if (button9.risingEdge()) {
Keyboard.release(KEY_A);
}
if (button10.risingEdge()) {
Keyboard.release(KEY_S);
}
if (button11.risingEdge()) {
Keyboard.release(KEY_D);
}
if (button12.risingEdge()) {
Keyboard.release(KEY_ESC);
}
if (button13.risingEdge()) {
Keyboard.release(KEY_F);
}
}
And the Ardunio code I just tried:
Code:
/*
Button Mouse Control
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.
*/
#include "Mouse.h"
// set pin numbers for the five buttons:
const int upButton = 0;
const int downButton = 1;
const int leftButton = 2;
const int rightButton = 3;
const int mouseButton = 4;
int range = 5; // output range of X or Y movement; affects movement speed
int responseDelay = 100; // 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 { // else the mouse button is not pressed:
// if the mouse is pressed, release it:
if (Mouse.isPressed(MOUSE_LEFT)) {
Mouse.release(MOUSE_LEFT);
}
}
// a delay so the mouse does not move too fast:
delay(responseDelay);
}