Hello,
I trying to control my robotic arm using a PS4 controller. It has worked once (controlling steppers with the joysticks) but now the controller wont stay connected or doesn't connect at all. So when pressing the ps logo button in the middle the lamp blinks and remains on, but only for maybe a second. I have changed the controllers Mac address using SixaxisPairTool and adjusted the code.
Does anyone recognize my problem and can help?
Thanks
Here''s the code to find the esp32's Mac address (, which seems to work as it print a mac address):
#include <PS4Controller.h>
#include <esp_bt_main.h>
#include <esp_bt_device.h>
void setup()
{
Serial.begin(115200);
PS4.begin();
const uint8_t* address = esp_bt_dev_get_address();
char str[100];
sprintf(str, "ESP32's Bluetooth MAC address is - %02x:%02x:%02x:%02x:%02x:%02x", address[0],address[1],address[2],address[3],address[4],address[5]);
Serial.println(str);
}
void loop()
{
}
Here's the control code for the steppers:
#include <AccelStepper.h>
#include <PS4Controller.h>
// Define stepper motor connections and motor interface type
#define dirPin1 16
#define stepPin1 4
#define dirPin2 5
#define stepPin2 17
#define motorInterfaceType 1
// Create an instance of the stepper motor
AccelStepper stepper1(motorInterfaceType, stepPin1, dirPin1);
AccelStepper stepper2(motorInterfaceType, stepPin2, dirPin2);
const int stepsPerRevolution1 = 2800;
const int stepsPerRevolution2 = 2900;
// Current angle of the stepper motor
float currentAngle1 = 0.0;
float currentAngle2 = 0.0;
// Define stick drift threshold
const int stickDriftThreshold = 10;
// Function to map joystick values to stepper speed
int mapJoystickToSpeed(int value, int minSpeed, int maxSpeed) {
if (abs(value) < stickDriftThreshold) {
return 0; // Ignore small stick movements (stick drift)
}
return map(value, -128, 127, -maxSpeed, maxSpeed);
}
void updateAngle() {
currentAngle1 = (stepper1.currentPosition() % stepsPerRevolution1) * (360.0 / stepsPerRevolution1);
currentAngle2 = (stepper2.currentPosition() % stepsPerRevolution2) * (360.0 / stepsPerRevolution2);
}
void setup() {
// Initialize serial communication
Serial.begin(115200);
// Initialize stepper motor
stepper1.setMaxSpeed(500);
stepper1.setAcceleration(500);
stepper2.setMaxSpeed(750);
stepper2.setAcceleration(750);
// Initialize PS4 controller
if (!PS4.begin("08:d1:f9:cb:5b:6e")) { // Replace with your ESP32's MAC address
Serial.println("Failed to connect PS4 controller!");
while (1); // Stay here forever if the connection fails
} else {
Serial.println("PS4 Controller connected!");
}
}
void loop() {
// Check if the PS4 controller is connected
if (PS4.isConnected()) {
// Read joystick values
int joyX = PS4.RStickX();
int joyY = PS4.RStickY();
// Map joystick values to stepper motor speed
int speedX = mapJoystickToSpeed(joyX, 0, 500);
int speedY = mapJoystickToSpeed(joyY, 0, 750);
// Set speed and direction of the stepper motor based on joystick values
stepper1.setSpeed(speedX);
stepper2.setSpeed(speedY);
// Move the stepper motor
stepper1.runSpeed();
stepper2.runSpeed();
updateAngle();
Serial.print("Current Angles: ");
Serial.print(currentAngle1);
Serial.print(" ");
Serial.println(currentAngle2);
}
}