Vorpal Robotics Wiki
Log in

DPAD Button Module

From Vorpal Robotics Wiki
Revision as of 16:33, 2 August 2018 by Vorpalwiki (talk | contribs) (Sample Code)

[FILE:DPAD-Button-Module.jpg|right|400px]

Description

This is a module with five push buttons arranged in a DPAD style layout, with four directional buttons and one additional button. It's typically used for directional controls (forward, backward, left, right) and the extra button can be used to trigger an actuator or special function.

Wiring

There are three pins on the DPAD:

  • GND (ground) should be connected to a ground wire (usually black).
  • VCC (positive voltage) should be connected to a positive voltage source of the same voltage as your microprocessor, for example +5V.
  • OUT (signal) this pin will output a voltage in the range of GND to VCC and would typically be connected to an analog input port.

Each button will output a different voltage level, and so you can distinguish the five buttons using only a single analog port.

However, if multiple buttons are pressed, you will only get back one of the two (or more) buttons. Thus, it is not possible with this module to detect simutaneous multiple clicks.

Sample Code

The following code is a function to decode the DPAD button. It assumes you've connected the DPAD OUT pin to analog 4. It returns a character representing the directional button pressed, one of 'f' (forward), 'b' (backward), 'l' (left), 'r' (right), or 'a' (actuator). If no button is pressed, it returns 's' (stop)

char readDPAD() {
  // read the analog port
  int b = analogRead(A4);
 // These values will reliably distinguish the buttons        
  if (b < 100) {
     return 'b';  // backward (bottom button)
  } else if (b < 200) {
     return 'l';  // left 
  } else if (b < 400) {
     return 'r';   // right
  } else if (b < 600) {
     return 'f';  // forward (top of diamond)
  } else if (b < 850) {
     return 'a';  // actuator (very top button)
  } else {
     return 's';  // stop (no button is pressed)
  }
}