Created
December 28, 2024 02:22
-
-
Save dragoncoder047/72a2495b037967b1de96c2aca19aca8c to your computer and use it in GitHub Desktop.
Source code used in https://youtu.be/mR6eZbIb-GU
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #ifndef ARDUINO_ARCH_ESP32 | |
| #error "only works on ESP32" | |
| #endif | |
| #if !__has_include("Bluepad32.h") | |
| #error "Need to use esp32_bluepad32 core, installation instructions here: https://bluepad32.readthedocs.io/en/latest/plat_arduino/" | |
| #endif | |
| #include <Bluepad32.h> | |
| ControllerPtr myController; | |
| void onConnect(ControllerPtr ctl) { | |
| Serial.printf("Controller connected! %s\n", ctl->getModelName().c_str()); | |
| if (ctl->isGamepad()) myController = ctl; | |
| } | |
| void onDisconnect(ControllerPtr ctl) { | |
| (void)ctl; | |
| Serial.println("Controller disconnected!"); | |
| if (ctl == myController) myController = NULL; | |
| } | |
| #define RIGHT_PIN 14 | |
| #define LEFT_PIN 32 | |
| #define FORWARD_PIN 15 | |
| #define BACK_PIN 33 | |
| #define high(pin) digitalWrite(pin, HIGH) | |
| #define low(pin) digitalWrite(pin, LOW) | |
| #define init(pin) \ | |
| do { \ | |
| pinMode(pin, OUTPUT); \ | |
| high(pin); \ | |
| } while (0) | |
| void setup() { | |
| init(RIGHT_PIN); | |
| init(LEFT_PIN); | |
| init(FORWARD_PIN); | |
| init(BACK_PIN); | |
| Serial.begin(115200); | |
| BP32.setup(&onConnect, &onDisconnect); | |
| BP32.forgetBluetoothKeys(); | |
| BP32.enableVirtualDevice(false); | |
| Serial.printf("Using Bluepad32: %s\n", BP32.firmwareVersion()); | |
| } | |
| void loop() { | |
| BP32.update(); | |
| if (myController != NULL && myController->isConnected()) { | |
| if (myController->hasData()) { | |
| int32_t steering = myController->axisRX(); | |
| int32_t throttle = myController->axisY(); | |
| Serial.printf("steering=%i throttle=%i\n", steering, throttle); | |
| if (abs(steering) > 256) { | |
| if (steering > 0) { | |
| low(RIGHT_PIN); | |
| high(LEFT_PIN); | |
| } else { | |
| high(RIGHT_PIN); | |
| low(LEFT_PIN); | |
| } | |
| } else { | |
| high(RIGHT_PIN); | |
| high(LEFT_PIN); | |
| } | |
| if (abs(throttle) > 256) { | |
| if (throttle < 0) { // up is negative for some reason | |
| low(FORWARD_PIN); | |
| high(BACK_PIN); | |
| } else { | |
| high(FORWARD_PIN); | |
| low(BACK_PIN); | |
| } | |
| } else { | |
| high(FORWARD_PIN); | |
| high(BACK_PIN); | |
| } | |
| } | |
| } | |
| vTaskDelay(10); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment