Vorpal Robotics Wiki
Log in

Difference between revisions of "DPAD Button Module"

From Vorpal Robotics Wiki
(Sample Code)
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
[FILE:DPAD-Button-Module.jpg|right|400px]
+
[[FILE:DPAD-Button-Module.jpg|right|400px]]
 +
== To Purchase ==
 +
To purchase this DPAD module, please [https://vorpal-robotics-store.myshopify.com/products/vorpal-dpad-button-module CLICK HERE] to visit the product page in our online store.
 
==Description==
 
==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.
 
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.
Line 15: Line 17:
 
== Sample Code ==
 
== 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).
+
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)
 
<code>
 
<code>
char readDPAD() {
+
char readDPAD() {
     
+
  // read the analog port
 
   int b = analogRead(A4);
 
   int b = analogRead(A4);
         
+
  // The comparison values used in this compound if statement will reliably distinguish the buttons       
 
   if (b < 100) {
 
   if (b < 100) {
 
       return 'b';  // backward (bottom button)
 
       return 'b';  // backward (bottom button)
Line 34: Line 36:
 
       return 's';  // stop (no button is pressed)
 
       return 's';  // stop (no button is pressed)
 
   }
 
   }
}
+
}
 
</code>
 
</code>

Latest revision as of 16:48, 2 August 2018

DPAD-Button-Module.jpg

To Purchase

To purchase this DPAD module, please CLICK HERE to visit the product page in our online store.

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);
  // The comparison values used in this compound if statement 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)
  }
}