Newbie; trying to learn basic programming and Bluepad32

Ive used the Bluepad32 example 'controller' to write a custom code. ive followed a few youtubers for examples.
my code compiled succesfully but the esp continues to loop:

rst:0x8 (TG1WDT_SYS_RESET) ,boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00, q_drv:0x00, d_drv:0x00, cs0_drv:0x00, hd_drv:0x00, wp_drv0:00
mode:DIO, clock div:1
load:0x3fff0030, len:1184
load:0x40078000, len:13260
load:0x40080400, len:3028
entry 0x400805e4
BluePad32 (C) 2016-2024 Ricardo Quesada and contributors.
Version: v4.1.0
BTstack: Copyright (c) 2017 BlueKitchen GmbH.
BTstack up and running at 00:48:12:EC:38:7A

Your help in finding my mistake in the code would be greatly appreciated.
Thanks, David
MY current code is below:

#include <Bluepad32.h>

int builtInLed = 2;

//Boom Extention Motor
int BoomExtPin1 = 7;
int BoomExtPin2 = 8;
int BoomExtEnPin = 9;

//Boom Lift Motor
int BoomLiftPin1 = 10;
int BoomLiftPin2 = 11;
int BoomLiftEnPin = 12;

//Carrier Motor
int CarrierPin1 = 13;
int CarrierPin2 = 14;
int CarrierEnPin = 15;

//Winch 1 Motor
int Winch1Pin1 = 23;
int Winch1Pin2 = 24;
int Winch1EnPin = 26;

//Winch 2 Motor
int Winch2Pin1 = 29;
int Winch2Pin2 = 30;
int Winch2EnPin = 31;



ControllerPtr myControllers[BP32_MAX_GAMEPADS];

// This callback gets called any time a new gamepad is connected.
// Up to 4 gamepads can be connected at the same time.
void onConnectedController(ControllerPtr ctl) {
  bool foundEmptySlot = false;
  for (int i = 0; i < BP32_MAX_GAMEPADS; i++) {
    if (myControllers[i] == nullptr) {
      Serial.printf("CALLBACK: Controller is connected, index=%d\n", i);
      // Additionally, you can get certain gamepad properties like:
      // Model, VID, PID, BTAddr, flags, etc.
      ControllerProperties properties = ctl->getProperties();
      Serial.printf("Controller model: %s, VID=0x%04x, PID=0x%04x\n", ctl->getModelName().c_str(), properties.vendor_id,
                    properties.product_id);
      myControllers[i] = ctl;
      foundEmptySlot = true;
      break;
    }
  }
  if (!foundEmptySlot) {
    Serial.println("CALLBACK: Controller connected, but could not found empty slot");
  }
}

void onDisconnectedController(ControllerPtr ctl) {
  bool foundController = false;

  for (int i = 0; i < BP32_MAX_GAMEPADS; i++) {
    if (myControllers[i] == ctl) {
      Serial.printf("CALLBACK: Controller disconnected from index=%d\n", i);
      myControllers[i] = nullptr;
      foundController = true;
      break;
    }
  }

  if (!foundController) {
    Serial.println("CALLBACK: Controller disconnected, but not found in myControllers");
  }
}

void dumpGamepad(ControllerPtr ctl) {
  Serial.printf(
    "idx=%d, dpad: 0x%02x, buttons: 0x%04x, axis L: %4d, %4d, axis R: %4d, %4d, brake: %4d, throttle: %4d, "
    "misc: 0x%02x, gyro x:%6d y:%6d z:%6d, accel x:%6d y:%6d z:%6d\n",
    ctl->index(),        // Controller Index
    ctl->dpad(),         // D-pad
    ctl->buttons(),      // bitmask of pressed buttons
    ctl->axisX(),        // (-511 - 512) left X Axis
    ctl->axisY(),        // (-511 - 512) left Y axis
    ctl->axisRX(),       // (-511 - 512) right X axis
    ctl->axisRY(),       // (-511 - 512) right Y axis
    ctl->brake(),        // (0 - 1023): brake button
    ctl->throttle(),     // (0 - 1023): throttle (AKA gas) button
    ctl->miscButtons(),  // bitmask of pressed "misc" buttons
    ctl->gyroX(),        // Gyro X
    ctl->gyroY(),        // Gyro Y
    ctl->gyroZ(),        // Gyro Z
    ctl->accelX(),       // Accelerometer X
    ctl->accelY(),       // Accelerometer Y
    ctl->accelZ()        // Accelerometer Z
  );
}


void processGamepad(ControllerPtr ctl) {
  // There are different ways to query whether a button is pressed.
  // By query each button individually:
  //  a(), b(), x(), y(), l1(), etc...
  
  //== PS4 X button = 0x0001 ==//
  if (ctl->buttons() == 0x0001) {
    digitalWrite(builtInLed, HIGH);
  }
  if (ctl->buttons() != 0x0001) {
    digitalWrite(builtInLed, LOW);
  }

  //== PS4 LEFT JOYSTICK - UP ==//
  if (ctl->axisY() <= -25) {

    int motorSpeed = map(ctl->axisY(), -25, -508, 70, 255);

    digitalWrite(BoomExtPin1, HIGH);
    digitalWrite(BoomExtPin2, LOW);
    digitalWrite(Winch1Pin1, HIGH);
    digitalWrite(Winch1Pin2, LOW);
    digitalWrite(Winch2Pin1, HIGH);
    digitalWrite(Winch2Pin2, LOW);
    analogWrite(BoomExtEnPin, motorSpeed);
    analogWrite(Winch1EnPin, motorSpeed);
    analogWrite(Winch2EnPin, motorSpeed);
  }

  //== PS4 LEFT JOYSTICK - DOWN ==//
  if (ctl->axisY() >= 25) {

    int motorSpeed = map(ctl->axisY(), 25, 512, 70, 255);

    digitalWrite(BoomExtPin1, LOW);
    digitalWrite(BoomExtPin2, HIGH);
    digitalWrite(Winch1Pin1, LOW);
    digitalWrite(Winch1Pin2, HIGH);
    digitalWrite(Winch2Pin1, LOW);
    digitalWrite(Winch2Pin2, HIGH);
    analogWrite(BoomExtEnPin, motorSpeed);
    analogWrite(Winch1EnPin, motorSpeed);
    analogWrite(Winch2EnPin, motorSpeed);
  }

  //== PS4 LEFT JOYSTICK - LEFT ==//
  if (ctl->axisX() <= -25) {

    int motorSpeed = map(ctl->axisX(), -25, -508, 70, 255);

    digitalWrite(BoomLiftPin1, HIGH);
    digitalWrite(BoomLiftPin2, LOW);
    analogWrite(BoomLiftEnPin, motorSpeed);
  }

  //== PS4 LEFT JOYSTICK - RIGHT ==//
  if (ctl->axisX() <= 25) {

    int motorSpeed = map(ctl->axisX(), 25, 512, 70, 255);

    digitalWrite(BoomLiftPin1, LOW);
    digitalWrite(BoomLiftPin2, HIGH);
    analogWrite(BoomLiftEnPin, motorSpeed);
  }

  //== PS4 LEFT JOYSTICK DEADZONE ==//
  if (ctl->axisY() > -25 && ctl->axisY() < 25 && ctl->axisX() > -25 && ctl->axisX() < 25) {
    // code for when left joystick is at idle
  }

  //== PS4 RIGHT JOYSTICK - UP ==//
  if (ctl->axisRY() <= -25) {

    int motorSpeed = map(ctl->axisRY(), -25, -508, 70, 255);

    digitalWrite(CarrierPin1, HIGH);
    digitalWrite(CarrierPin2, LOW);
    analogWrite(CarrierEnPin, motorSpeed);
  }

  //== PS4 RIGHT JOYSTICK - DOWN ==//
  if (ctl->axisRY() >= 25) {

    int motorSpeed = map(ctl->axisRY(), 25, 512, 70, 255);

    digitalWrite(CarrierPin1, LOW);
    digitalWrite(CarrierPin2, HIGH);
    analogWrite(CarrierEnPin, motorSpeed);
  }

  //== PS4 RIGHT JOYSTICK - LEFT ==//
  if (ctl->axisRX() <= -25) {

    int motorSpeed = map(ctl->axisRX(), -25, -508, 70, 255);

    digitalWrite(Winch1Pin1, HIGH);
    digitalWrite(Winch1Pin2, LOW);
    digitalWrite(Winch2Pin1, HIGH);
    digitalWrite(Winch2Pin2, LOW);
    analogWrite(Winch1EnPin, motorSpeed);
    analogWrite(Winch2EnPin, motorSpeed);
  }

  //== PS4 RIGHT JOYSTICK - RIGHT ==//
  if (ctl->axisRX() >= 25) {

    int motorSpeed = map(ctl->axisRX(), 25, 512, 70, 255);

    digitalWrite(Winch1Pin1, LOW);
    digitalWrite(Winch1Pin2, HIGH);
    digitalWrite(Winch2Pin1, LOW);
    digitalWrite(Winch2Pin2, HIGH);
    analogWrite(Winch1EnPin, motorSpeed);
    analogWrite(Winch2EnPin, motorSpeed);
  }

  //== PS4 RIGHT JOYSTICK DEADZONE ==//
  if (ctl->axisRY() > -25 && ctl->axisRY() < 25 && ctl->axisRX() > -100 && ctl->axisRX() < 100) {
    // code for when left joystick is at idle
  }

  // Another way to query controller data is by getting the buttons() function.
  // See how the different "dump*" functions dump the Controller info.
  dumpGamepad(ctl);
}

void processControllers() {
  for (auto myController : myControllers) {
    if (myController && myController->isConnected() && myController->hasData()) {
      if (myController->isGamepad()) {
        processGamepad(myController);

      } else {
        Serial.println("Unsupported controller");
      }
    }
  }
}

// Arduino setup function. Runs in CPU 1
void setup() {

  //sets the pins as outputs
  pinMode(BoomExtPin1, OUTPUT);
  pinMode(BoomExtPin2, OUTPUT);
  pinMode(BoomExtEnPin, OUTPUT);
  pinMode(BoomLiftPin1, OUTPUT);
  pinMode(BoomLiftPin2, OUTPUT);
  pinMode(BoomLiftEnPin, OUTPUT);
  pinMode(CarrierPin1, OUTPUT);
  pinMode(CarrierPin2, OUTPUT);
  pinMode(CarrierEnPin, OUTPUT);
  pinMode(Winch1Pin1, OUTPUT);
  pinMode(Winch1Pin2, OUTPUT);
  pinMode(Winch1EnPin, OUTPUT);
  pinMode(Winch2Pin1, OUTPUT);
  pinMode(Winch2Pin2, OUTPUT);
  pinMode(Winch2EnPin, OUTPUT);


  Serial.begin(115200);
  Serial.printf("Firmware: %s\n", BP32.firmwareVersion());
  const uint8_t* addr = BP32.localBdAddress();
  Serial.printf("BD Addr: %2X:%2X:%2X:%2X:%2X:%2X\n", addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);

  // Setup the Bluepad32 callbacks
  BP32.setup(&onConnectedController, &onDisconnectedController);

  // "forgetBluetoothKeys()" should be called when the user performs
  // a "device factory reset", or similar.
  // Calling "forgetBluetoothKeys" in setup() just as an example.
  // Forgetting Bluetooth keys prevents "paired" gamepads to reconnect.
  // But it might also fix some connection / re-connection issues.
  BP32.forgetBluetoothKeys();

  // Enables mouse / touchpad support for gamepads that support them.
  // When enabled, controllers like DualSense and DualShock4 generate two connected devices:
  // - First one: the gamepad
  // - Second one, which is a "virtual device", is a mouse.
  // By default, it is disabled.
  BP32.enableVirtualDevice(false);
}

// Arduino loop function. Runs in CPU 1.
void loop() {
  // This call fetches all the controllers' data.
  // Call this function in your main loop.
  bool dataUpdated = BP32.update();
  if (dataUpdated)
    processControllers();

  // The main loop must have some kind of "yield to lower priority task" event.
  // Otherwise, the watchdog will get triggered.
  // If your main loop doesn't have one, just add a simple `vTaskDelay(1)`.
  // Detailed info here:
  // https://stackoverflow.com/questions/66278271/task-watchdog-got-triggered-the-tasks-did-not-reset-the-watchdog-in-time

  //     vTaskDelay(1);
  delay(3000);

}

I don't know that firmware, but since the BD Addr does not get printed, could it be that the BP32 is not fully setup when you try to print the BD address ?

try

const uint8_t* addr = BP32.localBdAddress();
if (addr != nullptr) {
  Serial.printf("BD Addr: %2X:%2X:%2X:%2X:%2X:%2X\n", addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
} else {
  Serial.println("BD Addr not available yet");
}

or should you call

  BP32.setup(&onConnectedController, &onDisconnectedController);

before ?

1 Like

Thank You for your help. I tried your suggestion it had no impact. I had the same result. I went back and brought up a new copy of the example and modified it everywhere I did before except add my lines of code. The serial monitor output didnt loop and finished out it added the lines,

Firmware: Bluepad32 for Arduino v4.1.0
BD Addr: 0:4B:12:EC:38:7A

Which I believe it means it made a successful boot. So im going to go with it being an issue in the code I added (no surprise). Again thanks for your input.

MY additions are the
int lines at the beginning

Under void processGamepad
digitalWrite lines
analogWrite Lines

Under void setup
pinMode lines

This post can be closed. I figured out that I was using the wrong pin numbers off the pinout diagram.

Mark your post as the solution then

Glad you figured it out

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.