Skip to content

Instantly share code, notes, and snippets.

@tecteun
Created December 5, 2025 08:58
Show Gist options
  • Select an option

  • Save tecteun/16c2da06dd3c357562a50fd60c104a1c to your computer and use it in GitHub Desktop.

Select an option

Save tecteun/16c2da06dd3c357562a50fd60c104a1c to your computer and use it in GitHub Desktop.
NES pico ble
#include <JoystickBLE.h>
volatile bool updateRequired = false; // Flag to track updates
void handleButtonPress() {
updateRequired = true; // Set the flag when any button changes state
}
const char *localName = "NES BLE pico";
void setup(){}
void setup1() {
JoystickBLE.begin(localName);
// Configure pins as inputs with pull-down resistors
pinMode(2, INPUT_PULLDOWN); // UP
pinMode(3, INPUT_PULLDOWN); // DOWN
pinMode(4, INPUT_PULLDOWN); // LEFT
pinMode(5, INPUT_PULLDOWN); // RIGHT
pinMode(6, INPUT_PULLDOWN); // START
pinMode(7, INPUT_PULLDOWN); // SELECT
pinMode(8, INPUT_PULLDOWN); // A BUTTON
pinMode(9, INPUT_PULLDOWN); // B BUTTON
// Configure onboard LED pin as output
pinMode(LED_BUILTIN, OUTPUT);
// Attach interrupts for all buttons
attachInterrupt(digitalPinToInterrupt(2), handleButtonPress, CHANGE); // UP
attachInterrupt(digitalPinToInterrupt(3), handleButtonPress, CHANGE); // DOWN
attachInterrupt(digitalPinToInterrupt(4), handleButtonPress, CHANGE); // LEFT
attachInterrupt(digitalPinToInterrupt(5), handleButtonPress, CHANGE); // RIGHT
attachInterrupt(digitalPinToInterrupt(6), handleButtonPress, CHANGE); // START
attachInterrupt(digitalPinToInterrupt(7), handleButtonPress, CHANGE); // SELECT
attachInterrupt(digitalPinToInterrupt(8), handleButtonPress, CHANGE); // A BUTTON
attachInterrupt(digitalPinToInterrupt(9), handleButtonPress, CHANGE); // B BUTTON
// Enable manual sending mode
JoystickBLE.useManualSend(true);
}
bool toggle = false;
void loop() {
if (updateRequired) {
updateRequired = false; // Clear the update flag
// Handle MENU combination
if (digitalRead(8) && digitalRead(9)) {
JoystickBLE.button(1, false); // START
JoystickBLE.button(2, false); // SELECT
JoystickBLE.button(9, true); // MENU
} else {
JoystickBLE.button(1, digitalRead(8)); // START
JoystickBLE.button(2, digitalRead(9)); // SELECT
JoystickBLE.button(9, false); // MENU
}
// Handle other buttons
JoystickBLE.button(3, digitalRead(5)); // A BUTTON
JoystickBLE.button(4, digitalRead(7)); // B BUTTON
JoystickBLE.button(5, digitalRead(2)); // UP
JoystickBLE.button(6, digitalRead(3)); // DOWN
JoystickBLE.button(7, digitalRead(4)); // LEFT
JoystickBLE.button(8, digitalRead(6)); // RIGHT
// Send all updates
JoystickBLE.send_now();
// Blink onboard LED if any button is pressed
if (digitalRead(8) || digitalRead(9) || digitalRead(2) || digitalRead(3) ||
digitalRead(4) || digitalRead(5) || digitalRead(6) || digitalRead(7)) {
toggle = !toggle;
digitalWrite(LED_BUILTIN, toggle);
}
//delay(4); // Short delay to prevent flooding
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment