Advertisement
Guest User

arcadoid code v1 (makey makey)

a guest
Jan 18th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.85 KB | None | 0 0
  1. #include <Keyboard.h>
  2. #include <Mouse.h>
  3. #define DEBUG 0
  4. //--- JOYSTICK ---
  5. //pin
  6. #define JOY_VY 5
  7. #define JOY_VX 4
  8. #define JOY_SW 15
  9. //constants
  10. #define JOY_THRESHOLD 255
  11. #define JOY_MIDDLE 512
  12.  
  13. //--- BUTTONS ---
  14. //pin
  15. #define BTN_A 0
  16. #define BTN_B 1
  17. #define BTN_L 2
  18. #define BTN_R 3
  19. #define BTN_START 4
  20. #define BTN_SELECT 5
  21. //constants
  22. #define BTN_PRESSED 0
  23. #define BTN_NUMBER 6
  24.  
  25. int BTN_PIN_LIST[BTN_NUMBER]={BTN_A,BTN_B,BTN_L,BTN_R,BTN_START,BTN_SELECT};
  26. String BTN_DEBUG_LIST[BTN_NUMBER]={"A","B","L","R","START","SELECT"};
  27. char BTN_KEY_LIST[BTN_NUMBER]={'a','b','l','r','p','s'};
  28.  
  29. void setup() {
  30.   // put your setup code here, to run once:
  31.   Serial.begin(9600);
  32.   Keyboard.begin();
  33.   pinMode(0,INPUT);
  34. }
  35.  
  36. void loop() {
  37.   // joystick
  38.   int joy_x = analogRead(JOY_VX);
  39.   int joy_y = analogRead(JOY_VY);
  40.   if(joy_x<JOY_MIDDLE-JOY_THRESHOLD){
  41.     if(DEBUG)Serial.println("left");
  42.     else{
  43.       Keyboard.press(KEY_LEFT_ARROW);
  44.     }
  45.   }else{
  46.     if(!DEBUG)Keyboard.release(KEY_LEFT_ARROW);
  47.   }
  48.   if(joy_x>JOY_MIDDLE+JOY_THRESHOLD){
  49.     if(DEBUG)Serial.println("right");
  50.     else{
  51.       Keyboard.press(KEY_RIGHT_ARROW);
  52.     }
  53.   }else{
  54.     if(!DEBUG)Keyboard.release(KEY_RIGHT_ARROW);
  55.   }
  56.   if(joy_y<JOY_MIDDLE-JOY_THRESHOLD){
  57.     if(DEBUG)Serial.println("up");
  58.     else{
  59.       Keyboard.press(KEY_UP_ARROW);
  60.     }
  61.   }else{
  62.     if(!DEBUG)Keyboard.release(KEY_UP_ARROW);
  63.   }
  64.   if(joy_y>JOY_MIDDLE+JOY_THRESHOLD){
  65.     if(DEBUG)Serial.println("down");
  66.     else{
  67.       Keyboard.press(KEY_DOWN_ARROW);
  68.     }
  69.   }else{
  70.     if(!DEBUG)Keyboard.release(KEY_DOWN_ARROW);
  71.   }
  72.   //buttons
  73.   for(int i=0;i<BTN_NUMBER;i++){
  74.     if(digitalRead(BTN_PIN_LIST[i])==BTN_PRESSED){
  75.       if(DEBUG)Serial.println(BTN_DEBUG_LIST[i]);
  76.       else Keyboard.press(BTN_KEY_LIST[i]);
  77.     }else if(!DEBUG)Keyboard.release(BTN_KEY_LIST[i]);
  78.   }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement