Separating servo commands

Hi Guys,

I'm working on a 6 DOF robotic arm controlled by PS3 controller (VIA usb cable and later dongle (having some issues with that as well but it works sometimes)).
Right now I'm trying to separate the analog inputs from one and other, and to get the servo to stay at the location when I finished pressing R2 or L2 or one of the knobs. What happens when I click R2 for example, both of the servos I had connected as moving simultaneously and as I leave the R2 button they both go back to their default position. I want to move each one separately with its own button, its not too bad for the grip, but I want the other axis to stay put when im not pressing the button anymore.

Im open to the idea of using the digital buttons and using small increments instead, but I dont know how to code it.

#include <SPI.h>
#include <PS3BT.h> //Comment this out for USB instead of BT
#include <PS3USB.h> //Uncomment this for USB instead of BT
#include <Servo.h>
#include <usbhub.h>

// Satisfy the IDE, which needs to see the include statment in the ino too.
#ifdef dobogusinclude
#include <spi4teensy3.h>
#endif

USB Usb;
//BTD Btd(&Usb);//Comment this out for USB instead of BT
// The next line contains the MAC Address of the BT dongle, change to your dongle's MAC Address
//PS3BT PS3(&Btd,0x00,0x01,0x02,0x03,0x04,0x05);//Comment this out for USB instead of BT
//00:01:02:03:04:05
//PS3USB PS3(&Usb); //Uncomment this for USB instead of BT
PS3USB PS3(&Usb,0x00,0x01,0x02,0x03,0x04,0x05); //Uncomment this for USB instead of BT

Servo servo1;//
Servo servo2;//
Servo servo3;//
Servo servo4;//
Servo servo5;//
Servo servo6;//
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"));
pinMode(0,OUTPUT);
pinMode(1,OUTPUT);
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
servo1.attach(0);//
servo2.attach(1);//
servo3.attach(2);//
servo4.attach(3);//
servo5.attach(4);//
servo6.attach(5);//
}
void loop()
{
Usb.Task();

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

servo1.write(map(PS3.getAnalogHat(RightHatY),0,255,0,180));//Pan
servo2.write(map(PS3.getAnalogHat(RightHatX),0,255,180,0));//Tilt
servo3.write(map(PS3.getAnalogHat(LeftHatY),0,255,0,180));//Pan
servo4.write(map(PS3.getAnalogHat(LeftHatX),0,255,180,0));//Tilt
servo3.write(map(PS3.getAnalogButton(L2),0,255,0,180));//Pan
servo4.write(map(PS3.getAnalogButton(R2),0,255,180,0));//Tilt

}

// Analog button values can be read from almost all buttons
if (PS3.getAnalogButton(L2) || PS3.getAnalogButton(R2)) {
  Serial.print(F("\r\nL2: "));
  Serial.print(PS3.getAnalogButton(L2));
  if (PS3.PS3Connected) {
    Serial.print(F("\tR2: "));
    Serial.print(PS3.getAnalogButton(R2));
  }

else{
servo1.write(110);
servo2.write(90);
servo3.write(110);
servo4.write(90);
servo5.write(110);
servo6.write(90);
}

 //if(PS3.getButtonClick(PS)){

// PS3.disconnect();//Comment this out for USB instead of BT
// }
}
}

I gues this wished functionaltly could be solved via a small FSM, using the SWITCH/CASE statement.
e.g.
switch (command) {
case Command1:
..
break;
case Command2:
..
break;
.
.
.

Do you mean something like this?

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

case servo1.write(map(PS3.getAnalogHat(RightHatY),0,255,0,180));//Pan
break;
case servo2.write(map(PS3.getAnalogHat(RightHatX),0,255,180,0));//Tilt
break;

did you try it?
I have no idea how does the PS3 libary works.

I think that you got your brackets wrong. Let the IDE format your code and check the result.

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