PS3 gamepad and servo

Hey guys,

So iv'e been using a ps3 controller to control servos, I connected everything via A USB host shield and A dongle for wireless feasibility. It all works great and I was Able to control 2 servos at once with the 2 joysticks, but then I encountered a problem.

I need to control 6 different servos in my project, and since the ps3 controller doesn't have 6 joysticks i need to use buttons (X,O,square...) to control the servos.

I then relised I have no Idea how to do that

my question to you guys is how do I make it so When I press a certain button on the PS3 cont, the allocated servo moves in consideration of long the button is being pressed down. So like for example every 1 second the button is pressed the servo moves 10 degrees.

Thanks guys.

I'm not a proficient coder... but I would do something like this.. Code below is just an example...

If(ButtonState==high){ //if button is pushed
if(ButtonStart==0){ // see if it's the first loop it was on high
ButtonStart = Millis(); // set the time it was first pushed
}
}
else{ // button not pushed anymore
if(ButtonStart > 0){ // see if we have a time for when it was pushed
ButtonTime = Millis() - ButtonStart; // use time from when button was pushed until now
ServoMove = ButtonTime /100; // 1 second = 1000 ms so 1000/100 = 10 (may want to round ServoMove to nearest whole number)
ButtonStart=0; // reset our start time..
}
}

The code above wont move the servo until the button is released

This is my code,

#include <PS3BT.h>                    //Include the necessary libraries. 
#include <Servo.h> 

USB Usb; 
BTD Btd(&Usb); 
PS3BT PS3(&Btd);   

Servo servo1;                    //Create instances of type Servo. servo1 is the steering servo and servo2 is the ESC. 
Servo servo2; 

void setup() { 
    Serial.begin(115200);                     
    if (Usb.Init() == -1) {                     
      Serial.print(F("\r\nOSC did not start")); 
      while(1); //halt 
    } 
    Serial.print(F("\r\nPS3 Bluetooth Library Started"));                
    
    servo1.attach(7);                    //Steering servo on digital pin 5 
    servo2.attach(6);                   //ESC on sigital pin 3 
} 
void loop()   
{ 
    Usb.Task(); 

    if(PS3.PS3Connected || PS3.PS3NavigationConnected) { 
        
      servo1.write(map(PS3.getAnalogHat(RightHatX), 0, 255, 0, 180)); 
      servo2.write(map(PS3.getAnalogHat(LeftHatY), 0, 255, 180, 0)); 
    } 
    else   
     { 
      servo1.write(90); 
      servo2.write(90); 
     } 
        
      if(PS3.getButtonClick(PS)) { 
        PS3.disconnect(); 
     } 
}

How would I incorporate that in?

How does a button start? Meaning variable names ARE important. switchPressTime conveys a whole lot more information than buttonStart.

IS this How I would make my CODE?

If(TRIANGLEState==high){     //if button is pushed
    if(TRIANGLEStart==0){     // see if it's the first loop it was on high
         TRIANGLEStart = Millis(); // set the time it was first pushed
     }
}
else{    // button not pushed anymore
       if(TRIANGLEStart > 0){   // see if we have a time for when it was pushed
           TRIANGLETime = Millis() - TRIANGLEStart;  // use time from when button was pushed until now
           ServoMove = TRIANGLETime /100;       // 1 second = 1000 ms so 1000/100 = 10 (may want to round ServoMove to nearest whole number)
           TRIANGLEStart=0; // reset our start time..
        }
}

Never mind that last reply,

I was able to make the servo move 90 degrees up by pressing triangle than move 90 degrees down by pressing X.

How do I implement the "Button" function as I can't attach it to a pin as it's on the ps3 controller?

How do I implement the "Button" function as I can't attach it to a pin as it's on the ps3 controller?

The PS3 class has (or should have) methods for reading all the controls on the PS3 thing.

The PS3 class has (or should have) methods for reading all the controls on the PS3 thing.

It does as it knows "TRIANGLE" is the button triangle but in order to get more complicated with the Buttons and make them move by how long im holding the buttons down I need to define the "ButtonState" and "ButtonTime".

I just don't know how to make the arduino software know im talking about the Triangle button when defining the state and Time

Here is my current code in which I was able to move the servo 180 degrees with the Button "TRIANGLE" and back 180 degrees with the button "CROSS"

#include <PS3BT.h>                    //Include the necessary libraries. 
#include <Servo.h> 

USB Usb; 
BTD Btd(&Usb); 
PS3BT PS3(&Btd);   

Servo servo1;                    //Create instances of type Servo. servo1 is the steering servo and servo2 is the ESC. 
Servo servo2; 

int pos = 0; 



void setup() { 
    Serial.begin(115200);                     
    if (Usb.Init() == -1) {                     
      Serial.print(F("\r\nOSC did not start")); 
      while(1); //halt 
    } 
    Serial.print(F("\r\nPS3 Bluetooth Library Started"));                
    
    servo1.attach(7);                    
    servo2.attach(6);   

pinMode(TRIANGLE,INPUT);

} 
void loop()   
{ 
    Usb.Task(); 
    
for(pos = 0; pos < 180; pos += 1)

    if(PS3.PS3Connected || PS3.PS3NavigationConnected){
       if (PS3.getButtonClick(TRIANGLE)==HIGH)
       servo1.write(pos);
    }
   for(pos = 180; pos>=1; pos-=1)
    if(PS3.getButtonClick(CROSS)==HIGH)
    servo1.write(pos);


     if (PS3.getButtonClick(PS)) {
      Serial.print(F("\r\nPS"));
      PS3.disconnect();
}

}
for(pos = 0; pos < 180; pos += 1)

    if(PS3.PS3Connected || PS3.PS3NavigationConnected){
       if (PS3.getButtonClick(TRIANGLE)==HIGH)
       servo1.write(pos);
    }
   for(pos = 180; pos>=1; pos-=1)
    if(PS3.getButtonClick(CROSS)==HIGH)
    servo1.write(pos);

Consistent use, and placement of curly braces would make your code more readable. Random indenting sucks. Tools + Auto Format takes care are that, with little effort on your part.

Testing for the state of the switch inside the for loop does not make sense. Testing in one case that the device is connected, but not in the other, does not make sense.

   if(PS3.PS3Connected || PS3.PS3NavigationConnected)
   {
      if(PS3.getButtonClick(TRIANGLE)==HIGH)
      {
         for(int pos = 0; pos < 180; pos++)
         {
            servo1.write(pos);
            delay(5); // Give the servo time to move
         }
      }
      else if(PS3.getButtonClick(CROSS)==HIGH)
      {
         for(int pos = 1800; pos >= 0; pos--)
         {
            servo1.write(pos);
            delay(5); // Give the servo time to move
         }
      }
   }

I just don't know how to make the arduino software know im talking about the Triangle button when defining the state and Time

Look at the state change detection example. That it deals with a switch that is wired directly to a pin is irrelevant. You can easily change digitalRead(somePin) to PS3.getButtonClick(someName).

You can easily change digitalRead(somePin) to PS3.getButtonClick(someName).

But how? iv'e been online for days searching for an answer

When are you going to ask the Moderator to merge your Threads so we don't have to read much the same stuff in 3 places.

...R

IDK? how is this helpful?

BlaiseyRex:
IDK? how is this helpful?

If people (including you) get all the info in one place it is much easier to help.

...R

But I was encountering two different problems,

Anyway I found a way to Make the TRIANGLE be defined as "button"

const int button= (PS3.getButtonClick(TRIANGLE));

But after I upload this code

 if(PS3.PS3Connected || PS3.PS3NavigationConnected){
     if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter);
    }
     }

In the serial monitor the push counter goes uncontrollable and the button in seconds, the counter reaches thousands without me pushing any button.

Any suggestions?

BlaiseyRex:
But I was encountering two different problems,

Give me strength ...

Isn't it all about a PS3 gamepad and a servo ? ?

...R

Yea I guess, OK Yea I would ask the moderator to merge the threads but I don't know how to,

BlaiseyRex:
Yea I guess, OK Yea I would ask the moderator to merge the threads but I don't know how to,

Click report to moderator and give the links to the Threads.

...R