I have been working on a project to help learn programming/microcontrollers and am not sure how to get past this one last hurdle on what I would like to do.
I am taking a SNES controller and having it output to a PC as a joystick. I am using a Leonardo with modified HID and USBAPI files that include joystick support. I am also using a library written to gather to data from the SNES button presses. The way it works is it has a double 8 bit shift register, the first 12 bits are the states of the 12 buttons and the last 4 are always high.
The state of the buttons are stored as an unsigned integer.
What I am trying to add in are some Keyboard presses when certain buttons are pressed like follows.
The key presses do indeed get sent out, however it sends out 16 of them. I am guessing this has to do with the state of the buttons being store as an integer? What would be the best method to implement to have this only output a single keypress when they actions are done?
Sorry if I did not explain as well, any further questions please just let me know.
have you got the arduino board working as a usb master device? or are you using the pc as an interface repeating the input to the arduino? I ask as im using a usbhost shield to talk to a usb slave device?
have you checked out the usb coms data here https://www.circuitsathome.com/
i think you might find the code you need in the usb host shield libraries?
Need to see the code of how your reading the switches..
Quick guess is that you need debounce code added. It's probably reading the button switch 16 times when your tapping it..
#include "Arduino.h"
#include "SNESpaduino.h"
// Constructor: Init pins
SNESpaduino::SNESpaduino(byte latch, byte clock, byte data)
{
// Store the latch, clock and data pin for later use
PIN_LATCH = latch;
PIN_CLOCK = clock;
PIN_DATA = data;
// Set correct modes for the communication pins
pinMode(PIN_LATCH, OUTPUT);
pinMode(PIN_CLOCK, OUTPUT);
pinMode(PIN_DATA, INPUT);
}
// Return the state of all buttons. 12 of the uint16_t's bits are used, the 4 MSBs must be ignored.
uint16_t SNESpaduino::getButtons(boolean return_inverted)
{
// Init the button-state variable
state = 0;
// Latch the current buttons' state into the pad's register
digitalWrite(PIN_LATCH, HIGH);
digitalWrite(PIN_LATCH, LOW);
// Loop to receive 12 bits from the pad
for(i = 0; i < 12; i++)
{
// Read a button's state, shift it into the variable
state |= digitalRead(PIN_DATA) << i;
// Send a clock pulse to shift out the next bit
digitalWrite(PIN_CLOCK, HIGH);
digitalWrite(PIN_CLOCK, LOW);
}
// Return the bits
if(return_inverted)
return ~state;
else
return state;
}