i am absolutely useless at programming, i can't even figure out what should be something simple:
But i am determined to learn at least part of what i am doing with the arduino's.
I want to turn my uno (R3) into a usb hid joystick, i have done so using Darran Hunts 'UNO big joystick' http://hunt.net.nz/users/darran/
I like this particular version of the HID usb joysticks as it has the number of buttons i need, and it 'looks' simple,
So i have the following script on my UNO:
/* Arduino USB Joystick HID demo */
/* Author: Darran Hunt
* Released into the public domain.
*/
#define NUM_BUTTONS 40
#define NUM_AXES 8 // 8 axes, X, Y, Z, etc
typedef struct joyReport_t {
int16_t axis[NUM_AXES];
uint8_t button[(NUM_BUTTONS+7)/8]; // 8 buttons per byte
} joyReport_t;
joyReport_t joyReport;
void setup()
{
Serial.begin(115200);
delay(200);
for (uint8_t ind=0; ind<8; ind++) {
joyReport.axis[ind] = ind*1000;
}
for (uint8_t ind=0; ind<sizeof(joyReport.button); ind++) {
joyReport.button[ind] = 0;
}
}
// Send an HID report to the USB interface
void sendJoyReport(struct joyReport_t *report)
{
Serial.write((uint8_t *)report, sizeof(joyReport_t));
}
// turn a button on
void setButton(joyReport_t *joy, uint8_t button)
{
uint8_t index = button/8;
uint8_t bit = button - 8*index;
joy->button[index] |= 1 << bit;
}
// turn a button off
void clearButton(joyReport_t *joy, uint8_t button)
{
uint8_t index = button/8;
uint8_t bit = button - 8*index;
joy->button[index] &= ~(1 << bit);
}
uint8_t button=0; // current button
bool press = true; // turn buttons on?
/* Turn each button on in sequence 1 - 40, then off 1 - 40
* add values to each axis each loop
*/
void loop()
{
// Turn on a different button each time
if (press) {
setButton(&joyReport, button);
} else {
clearButton(&joyReport, button);
}
/* Move all of the axes */
for (uint8_t ind=0; ind<8; ind++) {
joyReport.axis[ind] += 10 * (ind+1);
}
sendJoyReport(&joyReport);
button++;
if (button >= 40) {
button = 0;
press = !press;
}
delay(100);
}
And when i use 'flip' to load the 'big joystick.hex' to the 8u2, it works, upon unplugging and re-pluggig the UNP, it shows up as a 40 button, 8 axis joystick,
Of course it is only a demo script, so the buttons are turning on one at a time, then off one at a time, and the axis moving by them selves,
I need to replace the demo code with actual code that will read inputs from the digital pins of the UNO, and send out the relevant joystick button presses.
i have no use for the axis, but i will leave it until much later before i see about removing those and changing the USB descriptors.
Can someone tell me what i need to change to get a single button press recognised, i.e. pin 2 connected to ground shows up as button 1 pressed on the joystick, and released when the connection is removed,
then i can hopefully work out how to get a few buttons working and move on from there,
Eventually i want to combine a 36 button keypad matrix script with the joystick script, so the buttons pressed on the keypad send joystick buttons to the game,
The button matrix script is:
#include <Keypad.h>
#include <LiquidCrystal.h>
const byte ROWS = 4; //four rows
const byte COLS = 9; //nine columns
char keys[ROWS][COLS] = {
{'0','1','2','3','4','5','6','7','8'},
{'9','A','B','C','D','E','F','G','H'},
{'I','J','K','L','M','N','O','P','Q'},
{'R','S','T','U','V','W','X','Y','Z'}
};
byte rowPins[ROWS] = {13, 12, 11, 10}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {9, 8, 7, 6, 5, 4, 3, 2, 1}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
LiquidCrystal lcd(A5, A4, A3, A2, A1, A0);
void setup()
{
lcd.begin(20, 1); // Specify how many columns and rows in the LCD unit
lcd.clear();
}
void loop()
{
char key = keypad.getKey();
switch (key)
{
case '0':
lcd.clear();
lcd.print ("xxx");
break;
case '1':
lcd.clear();
lcd.print ("xxx");
break;
case 'A':
lcd.clear();
lcd.print ("xxx");
break;
case 'B':
lcd.clear();
lcd.print ("xxx");
break;
case 'C':
lcd.clear();
lcd.print ("xxx");
break; //and so on for 36 buttons
}
}
It is one i used to display different sentences on an LCD according to which button was pressed, so i guess that instead of the LCD bit, i need to 'print' the button press letter to the joystick??