{
int wantedval = joystickButton[i];
int wantedpos;
for (int i=0; i<numButtons; i++) {
if (wantedval = joystickButton[i]) {
wantedpos = i;
}
}
Joystick.button(i + 1, allButtons[i]);
}
}
for (int i=0; i<numButtons; i++) {
Joystick.button(i + 1, allButtons[i]);
}
if (digitalRead(1))
{
Joystick.hat(1);
}
if (digitalRead(2))
{
Joystick.hat(90);
}
if (digitalRead(3))
{
Joystick.hat(180);
}
if (digitalRead(4))
{
Joystick.hat(270);
}
Thats actually pretty similar to what has been suggested DrVinny ...
One note ... this code ...seems to have some bracketing issues, and I think it is (supposed to be?) logically identical to this code..
// read digital pins and use them for the buttons
for (int i=0; i<numButtons; i++) {
if (digitalRead(joystickButton[i])) {
// when a pin reads high, the button is not pressed
// the pullup resistor creates the "on" signal
allButtons[i] = 0;
} else {
// when a pin reads low, the button is connecting to ground.
allButtons[i] = 1;
}
{
int wantedval = (joystickButton[i]);
int wantedpos;
for (int i=0; i<numButtons; i++) {
if (wantedval = joystickButton[i]) {
wantedpos = i;
}
}
Joystick.button(wantedpos + 1, allButtons[i]);
}
}
const int numButtons = 22; // 16 for Teensy, 32 for Teensy++
int joystickButton [] = {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 31, 16, 17, 18, 19, 20, 21, 22, 23 };
void setup() {
// you can print to the serial monitor while the joystick is active!
Serial.begin(9600);
// configure the joystick to manual send mode. This gives precise
// control over when the computer receives updates, but it does
// require you to manually call Joystick.send_now().
Joystick.useManualSend(true);
for (int i=0; i<numButtons; i++) {
pinMode(joystickButton[i], INPUT_PULLUP);
}
Serial.println("Begin Complete Joystick Test");
}
byte allButtons[numButtons];
byte prevButtons[numButtons];
int angle=0;
void loop() {
// read analog inputs and use them for the joystick axis
analogReadResolution(16);
Joystick.X(analogRead(A0));
Joystick.Y(analogRead(A1));
// read digital pins and use them for the buttons
for (int i=0; i<numButtons; i++) {
if (digitalRead(joystickButton[i])) {
// when a pin reads high, the button is not pressed
// the pullup resistor creates the "on" signal
allButtons[i] = 0;
} else {
// when a pin reads low, the button is connecting to ground.
allButtons[i] = 1;
}
{
int wantedval = (joystickButton[i]);
int wantedpos;
for (int i=0; i<numButtons; i++) {
if (wantedval = joystickButton[i]) {
wantedpos = i;
}
}
Joystick.button(wantedpos + 1, allButtons[i]);
}
}
// Because setup configured the Joystick manual send,
// the computer does not see any of the changes yet.
// This send_now() transmits everything all at once.
Joystick.send_now();
// check to see if any button changed since last time
boolean anyChange = false;
for (int i=0; i<numButtons; i++) {
if (allButtons[i] != prevButtons[i]) anyChange = true;
prevButtons[i] = allButtons[i];
}
// if any button changed, print them to the serial monitor
if (anyChange) {
Serial.print("Buttons: ");
for (int i=0; i<numButtons; i++) {
Serial.print(allButtons[i], DEC);
}
Serial.println();
}
// a brief delay, so this runs "only" 200 times per second
delay(5);
}
const int numButtons = 10;
const uint8_t buttons[numButtons] = { 0, 1, 2, 3, 16, 17, 18, 19, 20, 21};
byte allButtons[numButtons];
byte prevButtons[numButtons];
//int angle=0;
void setup() {
Serial.begin(9600);
// configure the joystick to manual send mode. This gives precise
// control over when the computer receives updates, but it does
// require you to manually call Joystick.send_now().
Joystick.useManualSend(true);
for (int i=0; i<numButtons; i++) {
pinMode(buttons[i], INPUT_PULLUP);
}
pinMode(12, INPUT_PULLUP);
pinMode(13, INPUT_PULLUP);
pinMode(14, INPUT_PULLUP);
pinMode(15, INPUT_PULLUP);
Serial.println("Begin joystick test excluding hat");
}
void loop() {
// read digital pins and use them for the buttons
for (int i=0; i<numButtons; i++) {
if (digitalRead(buttons[i])) {
// when a pin reads high, the button is not pressed
allButtons[i] = 0;
} else {
// when a pin reads low, the button is connecting to ground and is pressed.
allButtons[i] = 1;
}
Joystick.button(i + 1, allButtons[i]);
}
// make the hat switch automatically move in a circle
//angle = angle + 1;
//if (angle >= 360) angle = 0;
//Joystick.hat(angle);
if (!(digitalRead(12))) {Joystick.X(1023); } // !active low
else if (!(digitalRead(13))) {Joystick.X(0);}
else {Joystick.X(512); }
if (!(digitalRead(14))) {Joystick.Y(0); } // !active low
else if (!(digitalRead(15))) {Joystick.Y(1023);}
else {Joystick.Y(512); }
Joystick.send_now();
// check to see if any button changed since last time
boolean anyChange = false;
for (int i=0; i<numButtons; i++) {
if (allButtons[i] != prevButtons[i]) anyChange = true;
prevButtons[i] = allButtons[i];
}
// if any button changed, print them to the serial monitor
if (anyChange) {
Serial.print("Buttons: ");
for (int i=0; i<numButtons; i++) {
Serial.print(allButtons[i], DEC);
}
Serial.println();
}
delay(2);
}
I'm going to be selling the thing to my friend so I need it to work as generically as possible. I know the standard does exist because I can take one of my other arcade sticks, they show up as an arcade stick in Devices and Printers, all the buttons and the joystick register properly in joy.cpl, and it works with every game I've tried as well as GGPO and MAME, my goal is to recreate a similar level of compatibility with the Teensy. I'll try your code to see if I can get the joystick working in the serial monitor but ultimately it is not productive trying to get that working if there is something preventing the stick from showing up in joy.cpl. I asked the people in the big thread about the 128 button code but haven't got a response yet.
Could well be, I don't understand the code though.
I just tested the code I gave you on my mame arcade setup ... I compiled the code (no mistakes!!!) for my teensy 3.1 ...put in the pins I have connected my digital joystick to, and .... it works fine...indeed on joy.cpl I can move the plus sign around with my joystick ....
View attachment 6348View attachment 6349
diagonals work pretty well!
Im on windows 7.... perhaps you need to tell us more about your setup ... are u using a teensy 3.x ... are you connecting through a hub (shouldn't make any difference...) ... can u upload blink, are you getting serialprint data on the arduino ide serial monitor? are you getting hid devices appear in windows device manager
Perhaps spend a little time to go through these tutorials. They'll get you up to speed on some important basic concepts of Arduino.
http://www.pjrc.com/teensy/tutorial2.html
The Keyboard.print() and Joystick.button() and similar functions are similar to Serial.print(), except they send USB communication as the keyboard and joystick.
Even if you still have to ask question about the arcane programming syntax (a common issue when getting started), at least understanding this material about how the basic functions work will really help you to achieve better projects.
const int numButtons = 10;
// Changes numButtons to be the specific pins that are wired rather than being incremental 0-16
const uint8_t buttons[numButtons] = { 21, 20, 19, 18, 0, 1, 2, 3, 17, 16 };
void setup() {
// you can print to the serial monitor while the joystick is active!
Serial.begin(9600);
// configure the joystick to manual send mode. This gives precise
// control over when the computer receives updates, but it does
// require you to manually call Joystick.send_now().
Joystick.useManualSend(true);
for (int i=0; i<numButtons; i++) {
pinMode(buttons[i], INPUT_PULLUP);
}
Serial.println("Begin Complete Joystick Test");
}
byte allButtons[numButtons];
byte prevButtons[numButtons];
int angle=0;
void loop() {
// read digital pins and use them for the buttons
for (int i=0; i<numButtons; i++) {
if (digitalRead(buttons[i])) {
// when a pin reads high, the button is not pressed
// the pullup resistor creates the "on" signal
allButtons[i] = 0;
} else {
// when a pin reads low, the button is connecting to ground.
allButtons[i] = 1;
}
Joystick.button(i + 1, allButtons[i]);
}
// make the hat switch automatically move in a circle
angle = angle + 1;
if (angle >= 360) angle = 0;
Joystick.hat(angle);
// Because setup configured the Joystick manual send,
// the computer does not see any of the changes yet.
// This send_now() transmits everything all at once.
Joystick.send_now();
// check to see if any button changed since last time
boolean anyChange = false;
for (int i=0; i<numButtons; i++) {
if (allButtons[i] != prevButtons[i]) anyChange = true;
prevButtons[i] = allButtons[i];
}
// if any button changed, print them to the serial monitor
if (anyChange) {
Serial.print("Buttons: ");
for (int i=0; i<numButtons; i++) {
Serial.print(allButtons[i], DEC);
}
Serial.println();
}
// a brief delay, so this runs 200 times per second
delay(5);
}
const int numButtons = 10;
// Changes numButtons to be the specific pins that are wired rather than being incremental 0-16
const uint8_t buttons[numButtons] = { 21, 0, 1, 20, 18, 19, 3, 2, 17, 16 };
void setup() {
// you can print to the serial monitor while the joystick is active!
Serial.begin(9600);
// configure the joystick to manual send mode. This gives precise
// control over when the computer receives updates, but it does
// require you to manually call Joystick.send_now().
Joystick.useManualSend(true);
for (int i=0; i<numButtons; i++) {
pinMode(buttons[i], INPUT_PULLUP);
}
pinMode(12, INPUT_PULLUP);
pinMode(13, INPUT_PULLUP);
pinMode(14, INPUT_PULLUP);
pinMode(15, INPUT_PULLUP);
Serial.println("Begin Complete Joystick Test");
}
byte allButtons[numButtons];
byte prevButtons[numButtons];
int angle=0;
void loop() {
// read digital pins and use them for the buttons
for (int i=0; i<numButtons; i++) {
if (digitalRead(buttons[i])) {
// when a pin reads high, the button is not pressed
// the pullup resistor creates the "on" signal
allButtons[i] = 0;
} else {
// when a pin reads low, the button is connecting to ground.
allButtons[i] = 1;
}
Joystick.button(i + 1, allButtons[i]);
}
if (!(digitalRead(12))) {Joystick.X(1023); } // !active low
else if (!(digitalRead(13))) {Joystick.X(0);}
else {Joystick.X(512); }
if (!(digitalRead(15))) {Joystick.Y(0); } // !active low
else if (!(digitalRead(14))) {Joystick.Y(1023);}
else {Joystick.Y(512); }
// Because setup configured the Joystick manual send,
// the computer does not see any of the changes yet.
// This send_now() transmits everything all at once.
Joystick.send_now();
// check to see if any button changed since last time
boolean anyChange = false;
for (int i=0; i<numButtons; i++) {
if (allButtons[i] != prevButtons[i]) anyChange = true;
prevButtons[i] = allButtons[i];
}
// if any button changed, print them to the serial monitor
if (anyChange) {
Serial.print("Buttons: ");
for (int i=0; i<numButtons; i++) {
Serial.print(allButtons[i], DEC);
}
Serial.println();
}
// a brief delay, so this runs 200 times per second
delay(5);
}
const int numButtons = 10;
// Changes numButtons to be the specific pins that are wired rather than being incremental 0-16
const uint8_t buttons[numButtons] = { 21, 0, 1, 20, 18, 19, 3, 2, 17, 16 };
const uint8_t joy[] = {12,13,14,15};
bool joyread [4];
void joystick(bool*);
void setup() {
Serial.begin(9600);
Joystick.useManualSend(true);
for (int i=0; i<numButtons; i++) {
pinMode(buttons[i], INPUT_PULLUP);
}
for (int i=0; i<4; i++) {
pinMode(joy[i], INPUT_PULLUP);
}
Serial.println("Begin Complete Joystick Test");
}
byte allButtons[numButtons];
byte prevButtons[numButtons];
int angle=0;
void loop() {
// read digital pins and use them for the buttons
for (int i=0; i<numButtons; i++) {
if (digitalRead(buttons[i])) {
// when a pin reads high, the button is not pressed
// the pullup resistor creates the "on" signal
allButtons[i] = 0;
} else {
// when a pin reads low, the button is connecting to ground.
allButtons[i] = 1;
}
Joystick.button(i + 1, allButtons[i]);
}
/* if (!(digitalRead(12))) {Joystick.X(1023); } // !active low
else if (!(digitalRead(13))) {Joystick.X(0);}
else {Joystick.X(512); }
if (!(digitalRead(15))) {Joystick.Y(0); } // !active low
else if (!(digitalRead(14))) {Joystick.Y(1023);}
else {Joystick.Y(512); } */
for (int i=0; i<4; i++) {
joyread[i] = digitalRead(joy[i]);
}
joystick(joyread);
// Because setup configured the Joystick manual send,
// the computer does not see any of the changes yet.
// This send_now() transmits everything all at once.
Joystick.send_now();
/* check to see if any button changed since last time
boolean anyChange = false;
for (int i=0; i<numButtons; i++) {
if (allButtons[i] != prevButtons[i]) anyChange = true;
prevButtons[i] = allButtons[i];
}
// if any button changed, print them to the serial monitor
if (anyChange) {
Serial.print("Buttons: ");
for (int i=0; i<numButtons; i++) {
Serial.print(allButtons[i], DEC);
}
Serial.println();
}
// a brief delay, so this runs 200 times per second*/
delay(1);
}
void joystick(bool* read)
{
bool rest=true;
if (read[0]) {rest=false;if (read[2]) {Joystick.hat(45);} else if (read[3]) {Joystick.hat(135);} else {Joystick.hat(90);}}
if (read[1]) {rest=false;if (read[2]) {Joystick.hat(315);} else if (read[3]) {Joystick.hat(225);} else {Joystick.hat(270);}}
if (read[2] && (!read[0]) && (!read[1]) ) {rest=false;Joystick.hat(0);}
if (read[3] && (!read[0]) && (!read[1]) ) {rest=false;Joystick.hat(180);}
if (rest) {Joystick.hat(-1);}
}