Hi. I am making a project. I am making a gamepad that works with bluetooth with this library GitHub - lemmingDev/ESP32-BLE-Gamepad: Bluetooth LE Gamepad library for the ESP32. But esp32 does not have enough pins for buttons. So I am reading the datas of buttons and analog joystick modules with Arduino uno and sending it thorough UART. My UNO code is like that. #define X1 A1
#define Y1 A2
#define X2 A3
#define Y2 A4
#define b0 0
#define b1 1
#define b2 2
#define b3 3
#define b4 4
#define b5 5
#define b6 6
#define b7 7
#define b8 8
#define b9 9
#define b10 10
#define b11 11
#define b12 12
#define b13 13
#define b14 A0
`struct joystick` `{` ` int Xaxis;` ` int Yaxis;` ` int Xaxis2;` ` int Yaxis2;` ` int buttons[15];` `};`
joystick state;
`void setup()` `{` ` Serial.begin(9600);` ` for (int i=0; i<15; i++) {` ` pinMode(i, INPUT_PULLUP);` ` }` `}`
void readButton(int button, int index)
{
if(digitalRead(button)==0){
state.button[index]=1;
}
else{
state.button[index]=0;
}
}
`void loop()` `{` ` state.Xaxis=analogRead(X1);` ` state.Yaxis=analogRead(Y1);` ` state.Xaxis2=analogRead(X2);` ` state.Yaxis2=analogRead(Y2);`
readButton(b1, 1);
readButton(b2, 2);
readButton(b3, 3);
readButton(b4, 4);
readButton(b5, 5);
readButton(b6, 6);
readButton(b7, 7);
readButton(b8, 8);
readButton(b9, 9);
readButton(b10, 10);
readButton(b11, 11);
readButton(b12, 12);
readButton(b13, 13);
readButton(b14, 14);
``
byte *ptr;
ptr = (byte*)&state;
byte counter = sizeof(state);
Serial.print('<');
do
{
byte m = (byte*)*ptr;
Serial.write(m);
ptr++;
counter--;
}
while(counter != 0);
Serial.write('>'); //end of struct data
Serial.println();
}
And this is my ESP32 code
#include <BleConnectionStatus.h>
#include <BleGamepad.h>
#include <BleGamepadConfiguration.h>
#include <NimBLEDevice.h>
#include <Arduino.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ST7735.h>
#include <SPI.h>
`#define Rxpin 16` `#define Txpin 17`
#define TFT_CS 15
#define TFT_DC 2
#define TFT_RST 4
#define Charge 24
`BleGamepad bleGamepad("EAGLE TECH Gamepad", "Eagle Tech", 100);`
struct joystick
{
int Xaxis;
int Yaxis;
int Xaxis2;
int Yaxis2;
int buttons[15];
};
`joystick state;`
byte myData[38];
bool flag = false;
`unsigned long time_now = 0;`
const int delayBetweenSamples = 2, delayBetweenHIDReports = 5, debounceDelay = 10;
`int previousButtonStates[15] = {0};` `int currentButtonStates[15] = {0};`
unsigned long previousBattery, previousTime = 0;
`Adafruit_ST7735 Eagle = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);`
`void setup() {` ` Serial.begin(115200);` ` Serial2.begin(9600, SERIAL_8N1, Rxpin, Txpin);` ` bleGamepad.begin();` ` settingscreen();` `}`
void settingscreen() {
Eagle.initR(INITR_BLACKTAB);
Eagle.fillScreen(ST7735_BLACK);
Eagle.setTextColor(ST7735_YELLOW);
Eagle.fillRect(10, 35, 80, 16, ST7735_BLACK);
Eagle.fillRect(10, 105, 80, 16, ST7735_BLACK);
Eagle.setTextSize(1);
Eagle.setCursor(10, 15);
Eagle.println("Time:");
Eagle.setCursor(10, 85);
Eagle.println("Battery charge:");
Eagle.setTextSize(2);
Eagle.setTextColor(ST7735_WHITE);
float battery = analogRead(Charge) * (5.00 / 1023.00) * 2;
Eagle.fillRect(10, 105, 80, 16, ST7735_BLACK);
Eagle.setCursor(10, 105);
Eagle.print(battery);
}
void readingbytes() {
if (Serial2.available()) {
static bool flag = false;
char myData[38];
Serial2.readBytesUntil('>', myData, sizeof(myData));
if (myData[0] == '<') {
flag = true;
state.Xaxis = (myData[1] << 8) | myData[2];
state.Yaxis = (myData[3] << 8) | myData[4];
state.Xaxis2 = (myData[5] << 8) | myData[6];
state.Yaxis2 = (myData[7] << 8) | myData[8];
for (int i = 0; i < 15; i++) {
int previousState = buttonState[i];
buttonState[i] = (myData[9 + i*2] << 8) | myData[8 + i*2];
if (bleGamepad.isConnected() && (buttonState[i] != previousState)) {
if (buttonState[i] == 1) {
bleGamepad.press(buttons[i]);
} else {
bleGamepad.release(buttons[i]);
}
}
}
int adjustedValue1 = map(state.Xaxis, 0, 1023, 0, 32767);
int adjustedValue2 = map(state.Yaxis, 0, 1023, 0, 32767);
int adjustedValue4 = map(state.Xaxis2, 0, 1023, 0, 32767);
int adjustedValue3 = map(state.Yaxis2, 0, 1023, 0, 32767);
bleGamepad.setAxes(adjustedValue1, adjustedValue2, 0, 0, adjustedValue3, adjustedValue4, DPAD_CENTERED);
delay(delayBetweenHIDReports);
flag = false;
}
}}
`void tme() {` ` unsigned long currentTime = millis();` ` unsigned long seconds = currentTime / 1000;` ` unsigned int hours = seconds / 3600;` ` unsigned int mins = (seconds % 3600) / 60;` ` unsigned int secs = seconds % 60;` ` ` ` if (seconds != (previousTime / 1000)) {` ` // Time value has changed, update the display` ` String strHours = (hours < 10) ? "0" + String(hours) : String(hours);` ` String strMins = (mins < 10) ? "0" + String(mins) : String(mins);` ` String strSecs = (secs < 10) ? "0" + String(secs) : String(secs);` ` String timeString = strHours + "." + strMins + "." + strSecs;` ` ` ` Eagle.fillRect(10, 35, 200, 16, ST7735_BLACK);` ` Eagle.setCursor(10, 35);` ` Eagle.print(timeString);` ` previousTime = currentTime;` ` }` ` }`
void battery(){
float battery = analogRead(Charge)*(5.00 / 1023.00) * 2;
if (battery != previousBattery) {
// Battery value has changed, update the display
Eagle.fillRect(10, 105, 80, 16, ST7735_BLACK);
Eagle.setCursor(10, 105);
Eagle.print(battery);
previousBattery = battery;
}
}
``
void loop() {
readingbytes();
tme();
battery();
}
Only problem is that it presses buttons until 8. button. After that it does not detect or show in the computer. I do not know why. Is there any one who can help? pleasee