diff --git a/3way_button/3way_button.ino b/3way_button/3way_button.ino new file mode 100644 index 0000000000000000000000000000000000000000..da27ea9919a7bcd7bc95132121b7e73a2737cb30 --- /dev/null +++ b/3way_button/3way_button.ino @@ -0,0 +1,68 @@ +//include the third party library to manage HID +#include "HID-Project.h" + +//declare understandable var to recognise button in the code, +//the values are the pin number of the Arduino on which the 2 sides of the 3 way buttons are plugged in +const int Button1 = 4; +const int Button2 = 7; + +//manage the last button status +int lastButtonStatus[32]; + +void setup() { + //define the pin 4 and 7 as inputs + pinMode(Button1, INPUT_PULLUP); + pinMode(Button2, INPUT_PULLUP); + + //initialise the last status for 32 possible button (no use for now but will be usefull later + // in the final button box code + for (int i = 0 ; i < 31 ; i++) + { + lastButtonStatus[i] = HIGH; + } + + //initialise the gamepad + Gamepad.begin(); +} + + +//function that will be executed again and again and again... +void loop() { + //to keep the loop clean and understandable + //Button management externalised to another function + ButtonCheck(Button1, 1); + ButtonCheck(Button2, 2); +} + + +// function that check the button state +// the parameters are +// inputNB => the pin nb of the button of the push button +// gamepadButtonNb => which gamepad number will be pressed when the push button is pressed +void ButtonCheck(int inputNb, int gamepadButtonNb) { + //we check and store the button status + int ButtonVal = digitalRead(inputNb); + + // if the button state have changed, it means the user pressed or released the physical button + if (ButtonVal != lastButtonStatus[inputNb]) { + + // if the status is LOW, it means the user pressed the physical push button + // remember, the INPUT_PULLUP invert the value + if (ButtonVal == LOW) { + //don't forget to update the last status of the button + lastButtonStatus[inputNb] = LOW; + + //and then we "press" the gamepad button + Gamepad.press(gamepadButtonNb); + Gamepad.write(); + } + else { // if the input is HIGH, it means the user released the physical push button + //don't forget to update the last status of the button + lastButtonStatus[inputNb] = HIGH; + + //and then we "release" the gamepad button + Gamepad.release(gamepadButtonNb); + Gamepad.write(); + } + } +} diff --git a/analog_joystick/analog_joystick.ino b/analog_joystick/analog_joystick.ino new file mode 100644 index 0000000000000000000000000000000000000000..d4946e2e2714695cf3b6b40c7e4d24aa34869436 --- /dev/null +++ b/analog_joystick/analog_joystick.ino @@ -0,0 +1,85 @@ +//include the third party library to manage HID +#include "HID-Project.h" + +//declare understandable var to recognise button in the code, the X and Y axis of the analog joystick +const int analogJoyButton = 2; +const int joystickX = A0; +const int joystickY = A1; + +int lastButtonStatus[32]; + +void setup() { + //define the diffent pin of the arduino as input + pinMode(analogJoyButton, INPUT_PULLUP); + pinMode(joystickX, INPUT); + pinMode(joystickY, INPUT); + + //initialise the gamepad + Gamepad.begin(); +} + +//function that will be executed again and again and again... +void loop() { + //to keep the loop clean and understandable + //joystick management externalised to another function + //Button management externalised to another function + ButtonCheck(analogJoyButton, 1); + analogJoyAxisCheck(joystickX , joystickY); +} + +void analogJoyAxisCheck(int joyX , int joyY) { + //initialise variable that will be needed + int xMap, yMap, xValue, yValue; + + //read the input value of the analog joystick x and y axis + xValue = analogRead(joyX); + yValue = analogRead(joyY); + + // after playing with the joystick and sending + // value in the serial bus Serial.println(xValue); and Serial.println(yValue); + // it appears that center of joystick is x:517 and y:531 + // max/top y: 1023 min/bottom y :0 + // max/right x: 1023 min/left x :0 + + //we map the scale to transform value from 0 to 1023 into something from -32765 to 32766 + xMap = map(xValue, 0, 1023, -32765, 32766); + yMap = map(yValue, 0, 1023, -32765, 32766); + + //once we have the matching coordinates, we can send it to the gamepad + Gamepad.xAxis(xMap); + Gamepad.yAxis(yMap); + Gamepad.write(); +} + + +// function that check the button state +// the parameters are +// inputNB => the pin nb of the button of the analog joystick +// gamepadButtonNb => which gamepad number will be pressed when the analog joystick is pressed +void ButtonCheck(int inputNb, int gamepadButtonNb) { + //we check and store the button status + int ButtonVal = digitalRead(inputNb); + + // if the button state have changed, it means the user pressed or released the physical button + if (ButtonVal != lastButtonStatus[inputNb]) { + + // if the status is LOW, it means the user pressed the physical push button + // remember, the INPUT_PULLUP invert the value + if (ButtonVal == LOW) { + //don't forget to update the last status of the button + lastButtonStatus[inputNb] = LOW; + + //and then we "press" the gamepad button + Gamepad.press(gamepadButtonNb); + Gamepad.write(); + } + else { // if the input is HIGH, it means the user released the physical push button + //don't forget to update the last status of the button + lastButtonStatus[inputNb] = HIGH; + + //and then we "release" the gamepad button + Gamepad.release(gamepadButtonNb); + Gamepad.write(); + } + } +} diff --git a/rotaryencoder/rotaryencoder.ino b/rotaryencoder/rotaryencoder.ino new file mode 100644 index 0000000000000000000000000000000000000000..ff69895570a2be530fb331a9ab476f700e69dbc4 --- /dev/null +++ b/rotaryencoder/rotaryencoder.ino @@ -0,0 +1,119 @@ +//include the third party library to manage HID +#include "HID-Project.h" + +//declare understandable var to recognise button in the code, the A pin and B pin +const int rotaryEncoderA = 4; +const int rotaryEncoderB = 3; +const int rotaryEncoderButton = 2; + +//we also need to init other values +int counter = 0; +int lastCounter = 0; +int aState; +int aLastState; + +int lastButtonStatus[32]; + +void setup() { + //declare the A and B pin as input pullup + pinMode (rotaryEncoderA, INPUT_PULLUP); + pinMode (rotaryEncoderB, INPUT_PULLUP); + + //initialise the A pin last state + aLastState = digitalRead(rotaryEncoderA); + + //declare the button as an INPUT_PULLUP + pinMode(rotaryEncoderButton, INPUT_PULLUP); + + //initialise the gamepad + Gamepad.begin(); +} + +//function that will be executed again and again and again... +void loop() { + //to keep the loop clean and understandable + //rotary encoder management externalised to another function + rotaryEncoder( rotaryEncoderA, rotaryEncoderB, 2 , 3); + //Button management externalised to another function + ButtonCheck(rotaryEncoderButton , 1 ); +} + +//this function manage the rotary encoders +//the parameters are +// inputA => the pin number of the A side of the rotary +// inputB => the pin number of the B side of the rotary +// gamepadButtonUpNb => which gamepad number will be pressed when rotary going up +// gamepadButtonDownNb => which gamepad number will be pressed when rotary going down +void rotaryEncoder(int inputA, int inputB , int gamepadButtonUpNb , int gamepadButtonDownNb) { + + aState = digitalRead(inputA); // Reads the "current" state of the outputA + // If the previous and the current state of the outputA are different, that means a Pulse has occured + if (aState != aLastState) { + // If the outputB state is different to the outputA state, that means the encoder is rotating clockwise + if (digitalRead(inputB) != aState) { + counter ++; + + //my rotary encoder have a +2 value each step so button will be press only when even number + if (counter % 2 == 0) { + //since I don't want to hold the press + // I just "press" the button, wait 300ms and release the button + Gamepad.press(gamepadButtonUpNb); + Gamepad.write(); + delay(300); + Gamepad.release(gamepadButtonUpNb); + Gamepad.write(); + } + + } else { // else, it means that the outputB state is the same as A that means the encoder is rotating counter clockwise + + counter --; + + //my rotary encoder have a +2 value each step so button will be press only when even number + if (counter % 2 == 0) { + //since I don't want to hold the press + // I just "press" the button, wait 300ms and release the button + Gamepad.press(gamepadButtonDownNb); + Gamepad.write(); + delay(300); + Gamepad.release(gamepadButtonDownNb); + Gamepad.write(); + } + } + Serial.print("Position: "); + Serial.println(counter); + } + aLastState = aState; // Updates the previous state of the outputA with the current state + +} + +// function that check the button state +// the parameters are +// inputNB => the pin nb of the button of the rotary encoder +// gamepadButtonNb => which gamepad number will be pressed when rotary button is pressed +void ButtonCheck(int inputNb, int gamepadButtonNb) { + //we check and store the button status + int ButtonVal = digitalRead(inputNb); + + // if the button state have changed, it means the user pressed or released the physical button + if (ButtonVal != lastButtonStatus[inputNb]) { + + // if the status is LOW, it means the user pressed the physical push button + // remember, the INPUT_PULLUP invert the value + if (ButtonVal == LOW) { + //don't forget to update the last status of the button + lastButtonStatus[inputNb] = LOW; + + //and then we "press" the gamepad button + Gamepad.press(gamepadButtonNb); + Gamepad.write(); + } + else { // if the input is HIGH, it means the user released the physical push button + //don't forget to update the last status of the button + lastButtonStatus[inputNb] = HIGH; + + //and then we "release" the gamepad button + Gamepad.release(gamepadButtonNb); + Gamepad.write(); + } + } +} diff --git a/simple_push_button/simple_push_button.ino b/simple_push_button/simple_push_button.ino new file mode 100644 index 0000000000000000000000000000000000000000..abacddcaef25df7fb83a053839901f373a6ee91c --- /dev/null +++ b/simple_push_button/simple_push_button.ino @@ -0,0 +1,56 @@ +//include the third party library to manage HID +#include "HID-Project.h" + +//declare understandable var to recognise button in the code, +//the value is the pin number of the Arduino on which the push button is plugged +const int Button = 4; + +int lastButtonStatus[32]; + +void setup() { + //define the diffent pin of the arduino as input pullup + pinMode(Button, INPUT_PULLUP); + + //initialise the gamepad + Gamepad.begin(); +} + +//function that will be executed again and again and again... +void loop() { + //to keep the loop clean and understandable + //Button management externalised to another function + ButtonCheck(Button, 1); +} + + +// function that check the button state +// the parameters are +// inputNB => the pin nb of the button of the push button +// gamepadButtonNb => which gamepad number will be pressed when the push button is pressed +void ButtonCheck(int inputNb, int gamepadButtonNb) { + //we check and store the button status + int ButtonVal = digitalRead(inputNb); + + // if the button state have changed, it means the user pressed or released the physical button + if (ButtonVal != lastButtonStatus[inputNb]) { + + // if the status is LOW, it means the user pressed the physical push button + // remember, the INPUT_PULLUP invert the value + if (ButtonVal == LOW) { + //don't forget to update the last status of the button + lastButtonStatus[inputNb] = LOW; + + //and then we "press" the gamepad button + Gamepad.press(gamepadButtonNb); + Gamepad.write(); + } + else { // if the input is HIGH, it means the user released the physical push button + //don't forget to update the last status of the button + lastButtonStatus[inputNb] = HIGH; + + //and then we "release" the gamepad button + Gamepad.release(gamepadButtonNb); + Gamepad.write(); + } + } +} diff --git a/switch/switch.ino b/switch/switch.ino new file mode 100644 index 0000000000000000000000000000000000000000..6e813366c2ad9d5869f79f496ba3aa55cfa5ac66 --- /dev/null +++ b/switch/switch.ino @@ -0,0 +1,57 @@ +//include the third party library to manage HID +#include "HID-Project.h" + +//declare understandable var to recognise button in the code +const int Button = 4; + +//manage the last button status +int lastButtonStatus[32]; + +//initialisation function +void setup() { + //define the pin 4 as an input + pinMode(Button, INPUT); + + //initialise the last status for 32 possible button (no use for now but will be usefull later + // in the final button box code + for (int i = 0 ; i < 31 ; i++) + { + lastButtonStatus[i] = HIGH; + } + + //initialise the gamepad + Gamepad.begin(); +} + + +//function that will be executed again and again and again... +void loop() { + //to keep the loop clean and understandable + //switch management was externalise to another function + switchCheck(Button, 1); +} + +//external function to manage switch +//it takes 2 parameters +//inputNb => the pin number on which the switch is plug +//gamepadButtonNb => the button number of the gamepad +void switchCheck(int inputNb, int gamepadButtonNb) { + //read the digital input of the switch + int ButtonVal = digitalRead(inputNb); + + //if the status is not the same as last time + //it means that the user switches "off" or "on" + if (ButtonVal != lastButtonStatus[inputNb]) { + //don't forget to update the last status + lastButtonStatus[inputNb] = ButtonVal; + + //then we press the button , wait a little and release the button + Gamepad.press(gamepadButtonNb); + Gamepad.write(); + delay(300); + Gamepad.release(gamepadButtonNb); + Gamepad.write(); + + } + +}