I recently posted before and have now got my parts and programming. I have some code working but my problem is the stop commands.
When I press L2 button on the joystick it outputs the correct command with the correct hex depending on how hard I press the trigger.
My problem is after I depress the triiger I need it to send a stop command, so just now when I press the trigger I get constant stream of data when I let go I cant seem to get it to send the stop command.
here is the code
if (PS3.getAnalogButton(L2) < 255){
Serial2.write(byte(SynchByte)); // Synch Byte
Serial2.write(byte(CurrCameraAddress)); // Camera Address
Serial2.write(byte(0x00));
Serial2.write(byte(0x00)); // no command
Serial2.write(byte(0x00));
Serial2.write(byte(0x00)); // all stop
byte checkSum = (byte(CurrCameraAddress) + byte(00) + byte(0x00) + byte(0x00) + byte(00)%256);
Serial2.write(byte(checkSum)); //check sum is the sum of bytes (excluding the synchronization byte) modulo 256
delay (1);
}
else{
Serial2.write(byte(SynchByte)); // Synch Byte - byte 1
Serial2.write(byte(CurrCameraAddress)); // Camera Address - byte 2
Serial2.write(byte(command1)); // byte 3
Serial2.write(byte(command2)); // byte 4
Serial2.write(byte(PanSpeed)); // byte 5
Serial2.write(byte(TiltSpeed)); // byte 6
byte checkSum = (byte(CurrCameraAddress) + byte(command1) + byte(command2) + byte(PanSpeed) + byte(TiltSpeed)%256);
Serial2.write(byte(checkSum)); //check sum is the sum of bytes (excluding the synchronization byte) modulo 256
Serial.flush();
}
}
if anybody can help me out that would be great, I am knew to this so still learning. I have attached the ino file also
Ok so I can get it to print to the stop command but it prints it out everytime, if I remove the if and else statement it prints out after the normal command meaning its stop starting all the time
no the problem is, when I press L2 on joypad it starts moving but its also getting the stop command straight away aswell, so its a jerky reaction , I need the stop command to print when I release, when no input from serial is being seen a stop command should be sent
no the library doesn't have a released event, I have looked at the other PTZ posts and can't see how any of them sent a stop a command after analog input. button pressed and a stop command works fine. as its one command and one stop, the analog is what I can't get to work
thanks for that, I tried it outwith like you suggested and the checksum seems to be the same on the ones I tried. it also moves the camera, so it must be correct. I just need a way to get the stop command last
we don't even know what this "stop command" is, if you managed to get it to send it once, you should be able to do it again in a different order, but you didnt post you updated code.
ok here Is my code. the stop command just prints out all the time. like the hex example previous posts
where the stop command comes after, maybe it cant be done as the ps3 analog doesn't return to a zero position.
[/[code]
#include <PS3USB.h>
// Satisfy the IDE, which needs to see the include statment in the ino too.
#ifdef dobogusinclude
#include <spi4teensy3.h>
#include <SPI.h>
#endif
USB Usb;
/* You can create the instance of the class in two ways */
PS3USB PS3(&Usb); // This will just create the instance
//PS3USB PS3(&Usb,0x00,0x15,0x83,0x3D,0x0A,0x57); // This will also store the bluetooth address - this can be obtained from the dongle when running the sketch
bool printAngle;
uint8_t state = 0;
//=========================
//Start Pelco-D section
//=========================
//
//set some Pelco command vars
int PelcoMaxSpeed = 200;
int PelcoMinSpeed = 0;
byte CurrCameraAddress = 01;
byte SynchByte = 0xFF;
byte PanSpeed = 00;
byte TiltSpeed = 00;
byte ZoomSpeed = 00;
/*
command 1
7 Sense
6 Reserved
5 Reserved
4 Auto/Manual Scan
3 Camera On/Off
2 Iris Closed
1 Iris Open
0 Focus Near
*/
byte command1 = B00000000;
//set up some working vars so we don't need to calculate them every loop itteration
int rightRange = 0;
int leftRange = 0;
int upRange = 0;
int downRange = 0;
int zoomInRange = 0;
int zoomOutRange = 0;
//=========================
//end Pelco-D setup
//=========================
void stop() {
Serial2.write(byte(SynchByte)); // Synch Byte
Serial2.write(byte(CurrCameraAddress)); // Camera Address
Serial2.write(byte(0x00));
Serial2.write(byte(0x00)); // no command
Serial2.write(byte(0x00));
Serial2.write(byte(0x00)); // all stop
byte checkSum = (byte(CurrCameraAddress) + byte(00) + byte(0x00) + byte(0x00) + byte(00)) %256;
Serial2.write(byte(checkSum)); //check sum is the sum of bytes (excluding the synchronization byte) modulo 256
delay (2);
}
void setup() {
Serial.begin(115200);
Serial2.begin(9600);
CurrCameraAddress = 00;
// while (CurrCameraAddress <= 3) // send the set zoom speed command to all 3 cameras
{
CurrCameraAddress = CurrCameraAddress + 01;
}
#if !defined(__MIPSEL__)
while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
#endif
if (Usb.Init() == -1) {
Serial.print(F("\r\nOSC did not start"));
while (1); //halt
}
Serial.print(F("\r\nPS3 USB Library Started"));
pinMode(L2,INPUT);
}
void loop() {
Usb.Task();
if (PS3.PS3Connected || PS3.PS3NavigationConnected) {
if (PS3.getAnalogHat(LeftHatX) > 137 || PS3.getAnalogHat(LeftHatX) < 117 || PS3.getAnalogHat(LeftHatY) > 137 || PS3.getAnalogHat(LeftHatY) < 117 || PS3.getAnalogHat(RightHatX) > 137 || PS3.getAnalogHat(RightHatX) < 117 || PS3.getAnalogHat(RightHatY) > 137 || PS3.getAnalogHat(RightHatY) < 117) {
Serial.print(F("\r\nLeftHatX: "));
Serial.print(PS3.getAnalogHat(LeftHatX));
Serial.print(F("\tLeftHatY: "));
Serial.print(PS3.getAnalogHat(LeftHatY));
if (PS3.PS3Connected) { // The Navigation controller only have one joystick
Serial.print(F("\tRightHatX: "));
Serial.print(PS3.getAnalogHat(RightHatX));
Serial.print(F("\tRightHatY: "));
Serial.print(PS3.getAnalogHat(RightHatY));
}
}
// Analog button values can be read from almost all buttons
// stat L2 program
if (PS3.getAnalogButton(L2) >0 & PS3.getAnalogButton(L2) <63 || PS3.getButtonClick(UP))
if (PS3.getButtonClick(L2)==HIGH) {
Serial.print(F("\r\nL2: "));
Serial.print(PS3.getAnalogButton(L2));
/*
command 2
7 Focus Far
6 Zoom Wide
5 Zoom Tele
4 Down
3 Up
2 Left
1 Right
0 Always 0
*/
byte command2 = B00000000;
PanSpeed = PS3.getAnalogButton(L2) ;
TiltSpeed = PS3.getAnalogButton(UP);
command1 = B00000000;
command2 = B00000100;
Serial2.write(byte(SynchByte)); // Synch Byte - byte 1
Serial2.write(byte(CurrCameraAddress)); // Camera Address - byte 2
Serial2.write(byte(command1)); // byte 3
Serial2.write(byte(command2)); // byte 4
Serial2.write(byte(PanSpeed)); // byte 5
Serial2.write(byte(TiltSpeed)); // byte 6
byte checkSum = (byte(CurrCameraAddress) + byte(command1) + byte(command2) + byte(PanSpeed) + byte(TiltSpeed)) %256;
Serial2.write(byte(checkSum)); //check sum is the sum of bytes (excluding the synchronization byte) modulo 256
if (PS3.getButtonClick(L2)==LOW){
stop();
}
}
}
Can you post the code the specifically relates to the problem.
Is it this part
PanSpeed = PS3.getAnalogButton(L2) ;
TiltSpeed = PS3.getAnalogButton(UP);
command1 = B00000000;
command2 = B00000100;
Serial2.write(byte(SynchByte)); // Synch Byte - byte 1
Serial2.write(byte(CurrCameraAddress)); // Camera Address - byte 2
Serial2.write(byte(command1)); // byte 3
Serial2.write(byte(command2)); // byte 4
Serial2.write(byte(PanSpeed)); // byte 5
Serial2.write(byte(TiltSpeed)); // byte 6
byte checkSum = (byte(CurrCameraAddress) + byte(command1) + byte(command2) + byte(PanSpeed) + byte(TiltSpeed)) %256;
Serial2.write(byte(checkSum)); //check sum is the sum of bytes (excluding the synchronization byte) modulo 256
if (PS3.getButtonClick(L2)==LOW){
stop();
}
}
}
or is it the stop() function.
Where in the code is the problem occurring and where is the stop command being sent (or how did you get it to send a stop command the first time). Do this, and I feel it will be easier to diagnose the problem.
You may need to set a flag for when the button is released such that the stop command is sent then the flag is set. In the next loop the stop command is not sent until the flag is reset by the button pushed again.
Ps911 - yes it is that part, i am only concentrating on one part just now. Im not sure how it will know when button is realised, there is nothing in the library. I tried input high and low but that doesnt work
Ok il explain again, the ps3 in puts analog between 1-255 i have then sent this out the serial has a hex along with other bytes. The camera moves but continues to move unless it sees the stop command.
In my routine its sending out the move command, then straight away the stop command. So its goes then stops goes then stops, i need it to continuesly send the move commands depending on how far i pull the trigger for the speed. As one of the bytes is speed between 00 and FF. Then when no input from ps3 it sends the stop command in this case is FF010000000001
tried that, im giving up on it. the L2 and R2 don't return to 0 or anything low, if u press then depress the value can be anywhere between 1-150 its random all the time.
1 - 150?! then you need to design an algorithm to determine when you are actually pressing it...like when you press it just a little bit, is it higher than 150? You should record the output.
PS991 - thanks for your help, the problem Is the PS3 controller. you have to be real slow with it to see anything less than say 5 like you said, then it prints the stop. if its a quick trigger action then its doesn't see less than 5 to send the stop. even if I increase to say less than 10. I probably had it working long ago while trying different options, and was being too quick with the trigger button to not see an analog value low enough to send the stop.