Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Keyboard.h>
- #include <Mouse.h>
- #define DEBUG 0
- //--- JOYSTICK ---
- //pin
- #define JOY_VY 5
- #define JOY_VX 4
- #define JOY_SW 15
- //constants
- #define JOY_THRESHOLD 255
- #define JOY_MIDDLE 512
- //--- BUTTONS ---
- //pin
- #define BTN_A 0
- #define BTN_B 1
- #define BTN_L 2
- #define BTN_R 3
- #define BTN_START 4
- #define BTN_SELECT 5
- //constants
- #define BTN_PRESSED 0
- #define BTN_NUMBER 6
- int BTN_PIN_LIST[BTN_NUMBER]={BTN_A,BTN_B,BTN_L,BTN_R,BTN_START,BTN_SELECT};
- String BTN_DEBUG_LIST[BTN_NUMBER]={"A","B","L","R","START","SELECT"};
- char BTN_KEY_LIST[BTN_NUMBER]={'a','b','l','r','p','s'};
- void setup() {
- // put your setup code here, to run once:
- Serial.begin(9600);
- Keyboard.begin();
- pinMode(0,INPUT);
- }
- void loop() {
- // joystick
- int joy_x = analogRead(JOY_VX);
- int joy_y = analogRead(JOY_VY);
- if(joy_x<JOY_MIDDLE-JOY_THRESHOLD){
- if(DEBUG)Serial.println("left");
- else{
- Keyboard.press(KEY_LEFT_ARROW);
- }
- }else{
- if(!DEBUG)Keyboard.release(KEY_LEFT_ARROW);
- }
- if(joy_x>JOY_MIDDLE+JOY_THRESHOLD){
- if(DEBUG)Serial.println("right");
- else{
- Keyboard.press(KEY_RIGHT_ARROW);
- }
- }else{
- if(!DEBUG)Keyboard.release(KEY_RIGHT_ARROW);
- }
- if(joy_y<JOY_MIDDLE-JOY_THRESHOLD){
- if(DEBUG)Serial.println("up");
- else{
- Keyboard.press(KEY_UP_ARROW);
- }
- }else{
- if(!DEBUG)Keyboard.release(KEY_UP_ARROW);
- }
- if(joy_y>JOY_MIDDLE+JOY_THRESHOLD){
- if(DEBUG)Serial.println("down");
- else{
- Keyboard.press(KEY_DOWN_ARROW);
- }
- }else{
- if(!DEBUG)Keyboard.release(KEY_DOWN_ARROW);
- }
- //buttons
- for(int i=0;i<BTN_NUMBER;i++){
- if(digitalRead(BTN_PIN_LIST[i])==BTN_PRESSED){
- if(DEBUG)Serial.println(BTN_DEBUG_LIST[i]);
- else Keyboard.press(BTN_KEY_LIST[i]);
- }else if(!DEBUG)Keyboard.release(BTN_KEY_LIST[i]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement