Skip to content

Instantly share code, notes, and snippets.

@furious
Created September 19, 2019 19:51
Show Gist options
  • Save furious/03d8ca3e0e7da1e257de97006dff26c1 to your computer and use it in GitHub Desktop.
Save furious/03d8ca3e0e7da1e257de97006dff26c1 to your computer and use it in GitHub Desktop.
Simple Switches Gamepad using ATMEGA32U4
#include <Gamepad.h>
unsigned char button_normal[32] = {2,3,4,5,6};
unsigned char button_switch[32] = {7,8,9,10,14,15,16,A0,A1,A2,A3};
bool button_state[32];
void setup_pins(unsigned char *buttons, uint8_t total){
for(uint8_t i=0; i<total; i++){
pinMode(buttons[i], INPUT_PULLUP);
}
}
void setup() {
Gamepad.begin();
delay(1000);
setup_pins(button_normal, 6);
setup_pins(button_switch, 11);
}
void loop() {
delay(2);
uint8_t button = 0;
bool state = false;
for(uint8_t i=0; i<5; i++){
state = digitalRead(button_normal[i]) == 0;
Gamepad.setButton(button, state);
button_state[button] = state;
button++;
}
for(uint8_t i=0; i<11; i++){
state = digitalRead(button_switch[i]) == 1;
if(state && !button_state[button]){
if(i > 2){
Gamepad.setButton(button, true);
delay(100);
Gamepad.setButton(button, false);
delay(100);
}
button_state[button] = true;
} else if(!state && button_state[button]) {
Gamepad.setButton(button, true);
delay(100);
Gamepad.setButton(button, false);
delay(100);
button_state[button] = false;
}
button++;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment