I have two sets of code.
One set reads a bunch of push button switches and rotary encoders and uses the Tiny USB library to pass this along to the Windows game controller. This works fine.
The second set of code reads a pushbutton on a MCP-23017 (I need more input pins than the ESP-32 S3 board has). This code works fine.
When I combine the two sets of code however, it will not seem to read the MCP-23017 at all. There are two settings that I have to change in the Arduino IDE to get the Tiny USB part of the code to work. USB CDC on Boot > Enabled and USB Mode: USB-OTG (Tiny USB). I am not sure of one or both of these settings causes the MCP-23017 to not work but as I said, independently both sets of code work fine but combined the MCP-23017 is not working.
Any help you can give would be greatly appreciated.
COMBINED SETS OF CODE:
#include <Wire.h>
#include <Adafruit_MCP23X17.h>
#ifndef ARDUINO_USB_MODE
#elif ARDUINO_USB_MODE == 1
#else
#include "USB.h"
#include "USBHIDGamepad.h"
USBHIDGamepad Gamepad;
// --------------------- MCP23017 SETUP ---------------------
Adafruit_MCP23X17 mcp;
#define I2C_SDA 20
#define I2C_SCL 19
#define MCP_BUTTON_PIN 0 // MCP23017 pin PA0
const uint8_t MCP_GAMEPAD_BUTTON = 29; // Custom gamepad button for MCP button (adjust as needed)
bool lastMCPButtonState = true;
// --------------------- BUTTONS SETUP ----------------------
const int numButtons = 17;
const int buttonPins[numButtons] = {6, 16, 46, 11, 14, 1, 2, 42, 41, 40, 39, 38, 37, 36, 35, 0, 45};
int buttonLastState[numButtons] = {HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH};
const uint8_t buttonGamepadNumbers[numButtons] = {2, 5, 10, 13, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28};
// ------------------- ENCODERS SETUP -----------------------
const int numEncoders = 6;
const int encoderPins[numEncoders][2] = {
{4, 5}, {7, 15}, {17, 18}, {8, 3}, {9, 10}, {12, 13}
};
const uint8_t encoderCWButtons[numEncoders] = {0, 3, 6, 8, 11, 14};
const uint8_t encoderCCWButtons[numEncoders] = {1, 4, 7, 9, 12, 15};
int lastEncoded[numEncoders] = {0};
int encoderSteps[numEncoders] = {0};
const int debounceSteps = 4;
void setup() {
Serial.begin(115200);
delay(1000);
// ---------- Initialize USB Gamepad ----------
Gamepad.begin();
USB.begin();
// ---------- Set up physical buttons ----------
for (int i = 0; i < numButtons; i++) {
pinMode(buttonPins, INPUT_PULLUP);
}
// ---------- Set up rotary encoders ----------
for (int i = 0; i < numEncoders; i++) {
pinMode(encoderPins[0], INPUT_PULLUP);
pinMode(encoderPins[1], INPUT_PULLUP);
int MSB = digitalRead(encoderPins[0]);
int LSB = digitalRead(encoderPins[1]);
lastEncoded = (MSB << 1) | LSB;
}
// ---------- Set up MCP23017 ----------
Wire.begin(I2C_SDA, I2C_SCL);
if (!mcp.begin_I2C(0x27, &Wire)) {
Serial.println("MCP23017 not found. Check wiring.");
while (1);
}
Serial.println("MCP23017 initialized.");
mcp.pinMode(MCP_BUTTON_PIN, INPUT_PULLUP);
Serial.println("Setup complete.");
}
void loop() {
// ---------- Handle physical buttons ----------
for (int i = 0; i < numButtons; i++) {
int currentState = digitalRead(buttonPins);
if (currentState != buttonLastState) {
if (currentState == LOW) {
Gamepad.pressButton(buttonGamepadNumbers);
} else {
Gamepad.releaseButton(buttonGamepadNumbers);
}
buttonLastState[i] = currentState;
}
}
// ---------- Handle rotary encoders ----------
for (int i = 0; i < numEncoders; i++) {
int MSB = digitalRead(encoderPins[i][0]);
int LSB = digitalRead(encoderPins[i][1]);
int encoded = (MSB << 1) | LSB;
int sum = (lastEncoded[i] << 2) | encoded;
switch (sum) {
case 0b1101: case 0b0100: case 0b0010: case 0b1011:
encoderSteps[i]++;
break;
case 0b1110: case 0b0111: case 0b0001: case 0b1000:
encoderSteps[i]--;
break;
}
lastEncoded[i] = encoded;
if (encoderSteps[i] >= debounceSteps) {
Gamepad.pressButton(encoderCWButtons[i]);
delay(100);
Gamepad.releaseButton(encoderCWButtons[i]);
encoderSteps[i] = 0;
} else if (encoderSteps[i] <= -debounceSteps) {
Gamepad.pressButton(encoderCCWButtons[i]);
delay(100);
Gamepad.releaseButton(encoderCCWButtons[i]);
encoderSteps[i] = 0;
}
}
// ---------- Handle MCP23017 button ----------
bool currentMCPButtonState = mcp.digitalRead(MCP_BUTTON_PIN);
if (currentMCPButtonState != lastMCPButtonState) {
if (currentMCPButtonState == LOW) {
Serial.println("MCP23017 button pressed.");
Gamepad.pressButton(MCP_GAMEPAD_BUTTON);
} else {
Gamepad.releaseButton(MCP_GAMEPAD_BUTTON);
}
lastMCPButtonState = currentMCPButtonState;
delay(50); // Simple debounce
}
}
#endif /* ARDUINO_USB_MODE */
Help a newbie with some code?
-
pilotguy9853
- Posts: 6
- Joined: Sun May 11, 2025 2:23 pm
Return to “General Discussion”
Jump to
- English Forum
- Explore
- News
- General Discussion
- FAQ
- Documentation
- Documentation
- Sample Code
- Discussion Forum
- ESP32-S31
- Hardware
- ESP-IDF
- ESP-BOX
- ESP-ADF
- ESP-MDF
- ESP-WHO
- ESP-SkaiNet
- ESP32 Arduino
- IDEs for ESP-IDF
- ESP-AT
- ESP IoT Solution
- ESP RainMaker
- Rust
- ESP8266
- Report Bugs
- Showcase
- Chinese Forum 中文社区
- 活动区
- 乐鑫活动专区
- 讨论区
- ESP32-S31 中文讨论版
- 喵伴 中文讨论版
- ESP-IDF 中文讨论版
- 《ESP32-C3 物联网工程开发实战》书籍讨论版
- 中文文档讨论版
- ESP-AT 中文讨论版
- ESP-BOX 中文讨论版
- ESP IoT Solution 中文讨论版
- ESP-ADF 中文讨论版
- ESP Mesh 中文讨论版
- ESP Cloud 中文讨论版
- ESP-WHO 中文讨论版
- ESP-SkaiNet 中文讨论版
- ESP 生产支持讨论版
- 硬件问题讨论
- 项目展示
Who is online
Users browsing this forum: Bytespider, PerplexityBot, Qwantbot, YisouSpider and 6 guests
- All times are UTC
- Top
- Delete cookies
About Us
Espressif Systems is a fabless semiconductor company providing cutting-edge low power WiFi SoCs and wireless solutions for wireless communications and Internet of Things applications. ESP8266EX and ESP32 are some of our products.
Information
Espressif ESP32 ... Available now!