Using PS3 controller with ESC

So I’m using the PS3 BT library with an arduino to control 2 T200s. For some reason, the motors pulse when I’m not pressing any buttons, and they don’t have a smooth transition and don’t respond to my code well. It is very long so I’ll just post some parts of my code.

if (PS3.PS3Connected || PS3.PS3NavigationConnected) {
    down = PS3.getAnalogButton(L2);
    up = PS3.getAnalogButton(R2);
   
    pwr5 = map(up, 0, 225, 1500, 1900);//going up
    pwr6 = map(down, 0, 225, 1500, 1100);//going down

if (up > 20) {
      servoleft.writeMicroseconds(pwr5);
      servoright.writeMicroseconds(pwr5);
      Serial.print(F("\npwr6: "));
      Serial.print(pwr5);
    } else {
      servoleft.writeMicroseconds(1500);
      servoright.writeMicroseconds(1500);
    }
    
    if (down > 20) {
      servoleft.writeMicroseconds(pwr6);
      servoright.writeMicroseconds(pwr6);
      Serial.print(F("\npwr5: "));
      Serial.print(pwr6);
    } else {
      servoleft.writeMicroseconds(1500);
      servoright.writeMicroseconds(1500);
    }
}

This is the T200 part skipping other code for other motors, I’m not sure what I’m doing wrong.

Hi @Keonafielitz, welcome to the forum :slight_smile:

Without more context it’s difficult to say for sure what the issue is, but at a guess your issue may be similar to this one, where two libraries were being used in the Arduino that used the same internal timer, which meant there were conflicts and the ‘servo’ control wasn’t able to operate consistently while the other code was doing things.

If that is the case, you may need to make workarounds in your code, or if the Arduino type you’re using has multiple timers available you may be able to configure the relevant libraries to use separate timers.


By the way, I’ve edited your post to put your code into a code block. You can read about how to do that (and things like how to search our resources) in the How to Use the Blue Robotics Forums post :slight_smile:

Would it help if I posted my entire code? I’m very new to arduino so I don’t really know how to do any of those things, and any help would be appreciated.

The required information to determine the cause would be:

  • the hardware being used (the Arduino board type is probably the most important component)
  • a list of the libraries being used
  • the code (given it’s apparently very long, sharing effectively may be easiest via a GitHub repository or gist link so that changes can be tracked over time, and specific line numbers can be easily referred and linked to)

We can try to help you, but it’s not guaranteed that we can determine a solution for you, both because it may take a considerable amount of time, and if it’s not a relatively simple software issue and doesn’t have much information available online then testing may be required, which could require access to the hardware.

I’d suggest you do a few online searches for things like “using Arduino with PS3BT and servo”, but if that doesn’t yield useful results then you’re welcome to post the additional information about your system and we can try to guide you as to how best to determine the cause of the issue, and find potential solutions :slight_smile:

If the problem is what @EliotBR says it is, that

two libraries were being used in the Arduino that used the same internal timer, which meant there were conflicts

Then I might be able to help.

After making the post @EliotBR linked, I purchased an arduino mega, which has hardware serial support. Using the hardware serial on the mega seems to have solved my issue completely. What ever code I use the servos seem to run well alongside the ping; there is no stuttering or jerky motion what so ever.
I also came across this library: How to Use ServoTimer2 Library (Simple Explain) Servo Sweep | Arduino Project Hub
which I think is meant to avoid conflicts with other libraries, however I didn’t get it to work with the ‘nano every’ board I was using.

I hope that helps and that you get your problem sorted!

Hi! So I did try the ServoTimer2 library, and assuming I used it right, I still had issues of the servo motor jerking. I noticed, though, that it only jerked when I started the bluetooth connection to the PS3 controller. I put all my code in github at GitHub - keonafielitz/bt_motor_control: Controlling 4 motors using sabertooth motor controllers, 1 hobby servo, and 2 t200 through bluetooth so it would be easier to see the code. I’m also using an arduino mega 2560.

The Arduino Mega has 3 hardware serial ports beyond the default one used to talk to the computer - I’d suggest you try each of those instead of SoftwareSerial, noting the different pin requirements for each. I’m unsure whether it’s likely to work better with Servo or ServoTimer2.

Hopefully some combination of those options works well :slight_smile:


Just a note, you’re seemingly starting the same serial connection at both the start and end of your setup function, which doesn’t make much sense.