Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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();
}
}