Advertisement
Guest User

arduino joystick button matrix

a guest
Jul 31st, 2013
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.73 KB | None | 0 0
  1. /* @file CustomKeypad.pde
  2.  || @version 1.0
  3.  || @author Alexander Brevig
  4.  || @contact alexanderbrevig@gmail.com
  5.  ||
  6.  || @description
  7.  || | Demonstrates changing the keypad size and key values.
  8.  || #
  9.  */
  10. #include <Keypad.h>
  11. JoyState_t joySt;
  12. const byte ROWS = 4; //four rows
  13. const byte COLS = 6; //four columns
  14. //define the cymbols on the buttons of the keypads
  15. uint32_t values[] = {
  16.   0x1,0x2,0x4,0x8,0x10,0x20,0x40,0x80,0x100,0x200,0x400,0x800,0x1000,0x2000,0x4000,0x8000,0x10000,0x20000,0x40000,0x80000,0x100000,0x200000,0x400000,0x800000  };
  17. char hexaKeys[ROWS][COLS] = {
  18.   {1,2,3,4,5,6},
  19.   {7,8,9,10,11,12},
  20.   {13,14,15,16,17,18},
  21.   {19,20,21,22,23,24}};
  22. byte rowPins[ROWS] = {
  23.   8, 5, 3, 4}; //connect to the row pinouts of the keypad
  24. byte colPins[COLS] = {
  25.   6, 7, 13, 11,10,9}; //connect to the column pinouts of the keypad
  26.  
  27. //initialize an instance of class NewKeypad
  28. Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
  29.  
  30. void setup(){
  31.   //Serial.begin(9600);
  32.   joySt.xAxis = 0;
  33.   joySt.yAxis = 0;
  34.   joySt.zAxis = 0;
  35.   joySt.xRotAxis = 0;
  36.   joySt.yRotAxis = 0;
  37.   joySt.zRotAxis = 0;
  38.   joySt.throttle = 0;
  39.   joySt.rudder = 0;
  40.   joySt.hatSw1 = 0;
  41.   joySt.hatSw2 = 0;
  42.   joySt.buttons = 0;
  43. }
  44.  
  45. void loop(){
  46.   joySt.xAxis = analogRead(A0)/4;
  47.   joySt.yAxis = analogRead(A1)/4;
  48.   joySt.zAxis = analogRead(A2)/4;
  49.   joySt.xRotAxis = analogRead(A3)/4;
  50.   joySt.yRotAxis = analogRead(A4)/4;
  51.   char customKey = customKeypad.getKey();
  52.   joySt.buttons = 0;
  53.   if (customKey){
  54.     joySt.buttons = values[customKey-1];
  55.     //Serial.println(joySt.buttons,BIN);
  56.     //Serial.println(joySt.buttons,HEX);
  57.     //joySt.buttons = 0x100;
  58.   }
  59.   Joystick.setState(&joySt);
  60.  
  61.   delay(10);
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement