Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <DynamicHID.h>
- #include <Joystick.h>
- const int UpPin = 2;
- const int DownPin = 3;
- const int RightPin = 4;
- const int LeftPin = 5;
- int buttonPins[] = {6,7,8,9,10,11};
- const int buttonsCount = sizeof(buttonPins)/sizeof(int);
- Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,JOYSTICK_TYPE_GAMEPAD,
- buttonsCount, 0, // Button Count, Hat Switch Count
- true, true, false, // X and Y, but no Z Axis
- false, false, false, // No Rx, Ry, or Rz
- false, false, // No rudder or throttle
- false, false, false); // No accelerator, brake, or steering
- // Last state of the buttons
- int lastButtonState[buttonsCount];
- int lastUpState = 0;
- int lastDownState = 0;
- int lastLeftState = 0;
- int lastRightState = 0;
- //тут может гнать, если что комменть эту строку
- //memset(lastButtonState, 0, sizeof(buttonPins));
- void setup() {
- // Initialize Button Pins
- pinMode(UpPin, INPUT_PULLUP);
- pinMode(DownPin, INPUT_PULLUP);
- pinMode(RightPin, INPUT_PULLUP);
- pinMode(LeftPin, INPUT_PULLUP);
- for (int i=0; i<buttonsCount; i++) {
- pinMode(buttonPins[i], INPUT_PULLUP);
- }
- // Initialize Joystick Library
- Joystick.begin();
- Joystick.setXAxisRange(-1, 1);
- Joystick.setYAxisRange(-1, 1);
- }
- void loop() {
- // Read Up
- int currentUpState = !digitalRead(UpPin);
- int currentDownState = !digitalRead(DownPin);
- if(currentUpState !=lastUpState or currentDownState != lastDownState){
- if (currentUpState == 1) {
- Joystick.setYAxis(-1);
- }
- else
- if (currentDownState == 1) {
- Joystick.setYAxis(1);
- }
- else
- Joystick.setYAxis(0);
- }
- // Read Down
- // int currentDownState = !digitalRead(DownPin);
- // if (currentDownState != lastDownState){
- // if (currentDownState == 1) {
- // Joystick.setYAxis(1);
- // } else
- // if (currentUpState == 1){
- // Joystick.setYAxis(-1);
- // }else {
- // Joystick.setYAxis(0);
- // }
- // }
- // }
- // Read Left
- int currentLeftState = !digitalRead(LeftPin);
- if (currentLeftState != lastLeftState){
- if (currentLeftState == 1) {
- Joystick.setXAxis(-1);
- } else {
- Joystick.setXAxis(0);
- }
- }
- // Read Right
- int currentRightState = !digitalRead(RightPin);
- if (currentRightState != lastRightState){
- if (currentRightState == 1) {
- Joystick.setXAxis(1);
- } else {
- Joystick.setXAxis(0);
- }
- }
- // Read buttons values
- for (int index = 0; index < buttonsCount; index++){
- int currentButtonState = !digitalRead(buttonPins[index]);
- if (currentButtonState != lastButtonState[index]){
- Joystick.setButton(index, currentButtonState);
- }
- lastButtonState[index] = currentButtonState;
- }
- delay(10);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement