Skip to content

Instantly share code, notes, and snippets.

@ProjectionistFM
Created August 26, 2022 03:00
Arduino Switch Test
#include "HID-Project.h"
const int pinSwitch_1 = 2;
const int pinSwitch_2 = 5;
// declaring boolean variables to hold the new and old switch states
boolean oldSwitchState_1 = LOW;
boolean newSwitchState_1 = LOW;
void setup() {
pinMode(pinSwitch_1, INPUT_PULLUP);
pinMode(pinSwitch_2, INPUT_PULLUP);
Serial.begin( 9600 );
// Sends a clean report to the host. This is important on any Arduino type.
Gamepad.begin();
}
void loop() {
// SWITCH 1
// read the status from the switch
newSwitchState_1 = digitalRead(pinSwitch_1);
// if switched off
if (newSwitchState_1 == HIGH && oldSwitchState_1 == LOW) {
oldSwitchState_1 = HIGH;
Gamepad.release(1);
}
// if switched on
if (newSwitchState_1 == LOW && oldSwitchState_1 == HIGH) {
oldSwitchState_1 = LOW;
Gamepad.press(1);
}
// SWITCH 2
if (digitalRead(pinSwitch_2)) {
Gamepad.release(2);
}
else {
Gamepad.press(2);
}
// Functions above only set the values.
// This writes the report to the host.
Gamepad.write();
// Simple debounce
delay(50);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment