How to stop a continuous servo from moving until input is given

Hello all,

I am using a PS3 controller with a usb host shield to communicate with an Uno R3. I have two continuous servos connected to the uno that are controlled with the joysticks on the PS3 controller.

These two servos will be used to move a small car.

The issue is the servo continuously running while the the joystick input is at the "90" position. The joysticks are using the map function in the PS3BT library, and are continuously outputting a position while there is power to the Uno.

What I am trying to do is create a dead zone that is 10 percent above and below the 90 position to prevent the servos from slowly moving while there is no joystick input.

I tried to set up an if statement that used the servo.detach() function but it stopped the joystick input all together

Thanks in advance, I hope my question makes sense. I'm new, go easy.

#include <SPI.h>

#include <PS3BT.h> //Three libraries must be included for a PS3 controller with
// an arduino UNO board, SPI, PS3BT, and Servo.
#include <Servo.h>

USB Usb;

BTD Btd(&Usb);

PS3BT PS3(&Btd);

const int buttonInterval = 8000; // number of millisecs between button readings
unsigned long previousButtonMillis = 0; // time when button press last checked
unsigned long currentMillis = 0;

Servo Drservo1; //5 instances must be created for the 5 servos used.

Servo Drservo2; //Each Servo will be used independently except for the two
//drive servos (Drservo1, and Drservo2).
Servo Gripservo;

Servo Craneservo;

Servo Extenderservo;

int DrservoPin1 = 5;
int DrservoPin2 = 3;

void setup() {

Serial.begin(115200);

if (Usb.Init() == -1) {

Serial.print(F("\r\nOSC did not start")); //This line lets the user know a connection was not made to the USB
//host shield
while (1); //This pauses the code so the problem can be solved

}

Serial.print(F("\r\nPS3 Connection Made")); //This line lets the user know a connection was made to the controller.

Drservo1.attach(5); //Assigning each servo to it's respective pin
Drservo2.attach(3);
Gripservo.attach(6);
Craneservo.attach(7);
Extenderservo.attach(8);
int DrservoPin1 = 5;
int DrservoPin2 = 3;

}
void loop()
{
Usb.Task();

if (PS3.PS3Connected || PS3.PS3NavigationConnected) {

Drservo1.writeMicroseconds(map(PS3.getAnalogHat(RightHatY), 0, 255, 1410, 2210)); // These map functions changes the joystick position from the controller

// to a speed and direction for the servo.
Drservo2.writeMicroseconds(map(PS3.getAnalogHat(LeftHatY), 0, 255, 1410, 2210));

Serial.println(map(PS3.getAnalogHat(RightHatY), 0, 255, 1410, 2210));

Serial.println(map(PS3.getAnalogHat(LeftHatY), 0, 255, 1410, 2210));

if (PS3.getAnalogHat(RightHatY) >=20 && PS3.getAnalogHat(RightHatY) <= 235){
//Drservo1.detach();
digitalWrite(DrservoPin1, LOW);

// if (PS3.getAnalogHat(LeftHatY) >=20 && PS3.getAnalogHat(LeftHatY) <= 235){
// Drservo2.detach();
// digitalWrite(DrservoPin2, LOW);
// }
}

MEGR_PS3_code.ino (4.21 KB)

To make it easy for people to help you please modify your post and use the code button </>
codeButton.png

so your code looks like this and is easy to copy to a text editor. See How to use the Forum

That way it won't have any smileys - the compiler doesn't like them.

Also please use the AutoFormat tool to indent your code for easier reading.

Don't create complicated lines of code like this because they make debugging and modification difficult

Drservo1.writeMicroseconds(map(PS3.getAnalogHat(RightHatY), 0, 255, 1410, 2210));

Do it in separate stages - something like

joystickValue = PS3.getAnalogHat(RightHatY);
servoValue = map(joystickValue, 0, 255, 1410, 2210);
Drservo.writeMicroseconds(servoValue);

Now you can add print statements so you can see the intermediate values. Or you can add code to change the intermediate values.

For example if you want a dead band you could have something like this

if (joystickValue > 250 and joystickValue < 260) {
   joystickValue = 255
}

...R