Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* DanceForce pad code based on code provided by Promit Roy under Creative Commons CC-BY License
- * Modifications by Charles Scheidecker for Sparkfun Pro Micro board with XInput support
- * See https://github.com/dmadison/ArduinoXInput for XInput installation instructions
- */
- #include <XInput.h>
- //The analog threshold value for triggering a button
- const int TriggerThreshold = 25;
- //pin mappings for where things got soldered
- const int Pin_Up = A0;
- const int Pin_Right = A1;
- const int Pin_Down = A2;
- const int Pin_Left = A3;
- const int Pin_Back = 9;
- const int Pin_Start = 8;
- const int p[6] = {Pin_Up, Pin_Right, Pin_Down, Pin_Left, Pin_Start, Pin_Back};
- // debounce vars
- unsigned long lastDebounceTime[6] = {0};
- bool lastState[6] = {false};
- const int debounceDelayPad = 5; // debounce time in ms, adjust as needed
- const int debounceDelayButton = 125; // debounce time in ms, adjust as needed
- // state vars
- //analog read values
- int a[6] = {0};
- // Button states
- bool s[6] = {0};
- void setup() {
- Serial.begin(38400);
- //The analog pins are configured with internal pull-up resistors, which makes for a very simple circuit
- //However this method does not support useful pressure sensitivity adjustments
- //By soldering 1K resistors as pull-ups on the board, you can make the buttons require more pressure
- //The first version did that, but making the buttons more difficult didn't seem very desirable
- pinMode(Pin_Start, INPUT_PULLUP);
- pinMode(Pin_Back, INPUT_PULLUP);
- pinMode(Pin_Up, INPUT_PULLUP);
- pinMode(Pin_Right, INPUT_PULLUP);
- pinMode(Pin_Down, INPUT_PULLUP);
- pinMode(Pin_Left, INPUT_PULLUP);
- XInput.setAutoSend(false);
- XInput.begin();
- }
- void loop() {
- //read each pin, and set that Joystick button appropriately
- for(int i = 0; i < 6; ++i)
- {
- a[i] = analogRead(p[i]);
- // normalize values to on or off
- if (a[i] <= TriggerThreshold) {
- s[i] = true;
- } else {
- s[i] = false;
- }
- switch(p[i]) {
- case Pin_Up:
- if (debounced(i, debounceDelayPad)) {
- XInput.setButton(BUTTON_Y, s[i]);
- }
- break;
- case Pin_Right:
- if (debounced(i, debounceDelayPad)) {
- XInput.setButton(BUTTON_B, s[i]);
- }
- break;
- case Pin_Down:
- if (debounced(i, debounceDelayPad)) {
- XInput.setButton(BUTTON_A, s[i]);
- }
- break;
- case Pin_Left:
- if (debounced(i, debounceDelayPad)) {
- XInput.setButton(BUTTON_X, s[i]);
- }
- break;
- case Pin_Start:
- if (debounced(i, debounceDelayButton)) {
- XInput.setButton(BUTTON_START, s[i]);
- }
- break;
- case Pin_Back:
- if (debounced(i, debounceDelayButton)) {
- XInput.setButton(BUTTON_BACK, s[i]);
- }
- break;
- }
- lastState[i] = s[i];
- }
- XInput.send();
- //Enable this block if you need to debug the electricals of the pad
- //XInput library automatically prints debug information if a non-XInput board
- //is selected. If you want raw debug data, use a standard board type and set
- //if(0) to if(1)
- #ifndef USB_XINPUT
- if(1)
- {
- char buffer[80];
- sprintf(buffer, "Pins: %d %d %d %d (%d %d)\n", a[0], a[1], a[2], a[3], a[4], a[5]);
- sprintf(buffer, " Deb: %lu %lu %lu %lu (%lu %lu) %lu\n", lastDebounceTime[0], lastDebounceTime[1], lastDebounceTime[2], lastDebounceTime[3], lastDebounceTime[4], lastDebounceTime[5], millis());
- Serial.println(buffer);
- //delay(250);
- }
- #endif
- //This limits the pad to run at 200 Hz.
- //delay(5);
- }
- //returns true if a button doesn't need to be debounced
- bool debounced(int buttonIndex, int debounceDelay) {
- // If the switch changed, due to noise or pressing:
- if (s[buttonIndex] != lastState[buttonIndex]) {
- // reset debounce timer
- lastDebounceTime[buttonIndex] = millis();
- }
- if ((millis() - lastDebounceTime[buttonIndex]) > debounceDelay) {
- // whatever the reading is at, it's been there for longer than the debounce
- // delay, so take it as the actual current state:
- // if the button state has changed:
- if (s[buttonIndex] != lastState[buttonIndex]) {
- lastState[buttonIndex] = s[buttonIndex];
- }
- return true;
- }
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement