Skip to content
switch.ino 1.5 KiB
Newer Older
//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();

  }

}