A simple dance pad/rhythm game controller implementation for Arduino devices using modern C++
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
1.7 KiB

  1. /*@ file DDRunino.ino
  2. *
  3. * Simple Arduino sketch to run a DDR pad as a keyboard
  4. *
  5. * https://git.beesbuzz.biz/fluffy/DDRuino/
  6. */
  7. #include <Keyboard.h>
  8. //! Uncomment this to enable the serial debugger to determine the pin mappings
  9. //#define DEBUG
  10. //! Debounce time in milliseconds
  11. constexpr int debounceTime = 10;
  12. //! Input button
  13. struct Input {
  14. int pin; //!< Input pin
  15. int keyCode; //!< Mapped key
  16. bool pressed; //!< Last read state
  17. unsigned long lastChange; //!< Last update time
  18. };
  19. /*! Map the input pins to a button on the pad; use debug mode to determine
  20. * your button layout if you're not sure
  21. */
  22. Input mapping[] = {
  23. { 4, KEY_RETURN },
  24. { 5, KEY_ESC },
  25. { 6, KEY_UP_ARROW },
  26. { 7, KEY_DOWN_ARROW },
  27. { 8, KEY_RIGHT_ARROW },
  28. { 9, KEY_LEFT_ARROW },
  29. };
  30. void setup()
  31. {
  32. #ifdef DEBUG
  33. Serial.begin(9600);
  34. for (int i = 0; i < 12; i++) {
  35. pinMode(i, INPUT_PULLUP);
  36. }
  37. #endif
  38. unsigned long now = millis();
  39. for (auto i : mapping) {
  40. pinMode(i.pin, INPUT_PULLUP);
  41. i.pressed = false;
  42. i.lastChange = now;
  43. }
  44. Keyboard.begin();
  45. }
  46. void loop()
  47. {
  48. #ifdef DEBUG
  49. for (int i = 0; i < 12; i++) {
  50. Serial.print(!digitalRead(i) ? "0123456789abcdef"[i] : ' ');
  51. }
  52. Serial.print('\n');
  53. #endif
  54. unsigned long now = millis();
  55. for (auto& i : mapping) {
  56. bool val = !digitalRead(i.pin);
  57. if (val != i.pressed && (i.lastChange + debounceTime) < now) {
  58. if (val) {
  59. Keyboard.press(i.keyCode);
  60. } else {
  61. Keyboard.release(i.keyCode);
  62. }
  63. i.pressed = val;
  64. i.lastChange = now;
  65. }
  66. }
  67. }