Hello all.
//Protocol global variables.
byte pelcoD[7] = {0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; //Synch Byte, Address, Command 1, Command 2, Data 1, Data 2, Checksum.
byte pelcoP[8] = {0xA0, 0x00, 0x00, 0x00, 0x00, 0x00, 0xAF, 0x00}; //STX, Address, Data 1, Data 2, Data 3, Data 4, ETX, Checksum.
I am creating a sketch to control dome cameras with Pelco telemetry of P & D types and the code above is how i am creating my data packets for each protocol. Is it possible to place byte pelcoD and pelcoP into the same structure even tho the arrays are of different sizes? And if so how would i write it?
Thanks.
This code below is far from complete but it's my start on trying to create a multi protocol controller.
/*------------------------------------------------------------
Created by - Andrew Hughes
------------------------------------------------------------*/
//Define pin names.
#define pin_joystickButton 2 //Joystick button connected to this pin.
#define pin_xAxis A0 //Joystick connected to this pin, pan.
#define pin_yAxis A1 //Joystick connected to this pin, tilt.
#define pin_zAxis A2 //Joystick connected to this pin, twist.
//Joystick global variables.
byte joystickButton = 1; //Button on top of the joystick to be used for switching between zoom & focus.
unsigned int xyzAxis;
unsigned int xCenter;
unsigned int yCenter;
unsigned int zCenter;
unsigned int xAxisMin; //Pan rigt.
unsigned int xAxisMax; //Pan left.
unsigned int yAxisMin; //Tilt up.
unsigned int yAxisMax; //Tilt down.
unsigned int zAxisMin; //Anitclockwise.
unsigned int zAxisMax; //Clockwise.
unsigned int threshold; //Used for increasing the center area(dead zone).
byte joystickValues[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; //Pan right, pan left, tilt up, tilt down, zoom in, zoom out, focus far, focus near, iris open, iris close.
//Protocol global variables.
byte pelcoD[7] = {0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; //Synch Byte, Address, Command 1, Command 2, Data 1, Data 2, Checksum.
byte pelcoP[8] = {0xA0, 0x00, 0x00, 0x00, 0x00, 0x00, 0xAF, 0x00}; //STX, Address, Data 1, Data 2, Data 3, Data 4, ETX, Checksum.
//Other global variables.
byte address = 0x01; //Stores address selection number from a switch. To be impilmented.
byte newControl; //Used to determin control state.
byte oldControl; //Used to determin control state.
void setup() {
//Initialise the hardware serial port.
Serial.begin(9600);
while (!Serial)
{
; // Wait for serial port to connect. Needed for Leonardo only.
}
joystickCalabration();
}
void loop() {
//joystickButton = digitalRead(pin_joystickButton); //Not connected yet.
xyzAxis = analogRead(pin_xAxis);
//Pan right.
threshold = xCenter - 25;
if(xyzAxis < threshold) {
xyzAxis = map(xyzAxis, threshold, xAxisMin, 1, 8);
Serial.print(F("PR"));
Serial.println(xyzAxis);
joystickValues[0] = xyzAxis;
}
else {
joystickValues[0] = 0; //Clears the last stored value.
}
//Pan left.
threshold = xCenter + 25;
if(xyzAxis > threshold) {
xyzAxis = map(xyzAxis, threshold, xAxisMax, 1, 8);
Serial.print(F("PL"));
Serial.println(xyzAxis);
joystickValues[1] = xyzAxis;
}
else {
joystickValues[1] = 0; //Clears the last stored value.
}
xyzAxis = analogRead(pin_yAxis);
//Tilt up.
threshold = yCenter - 25;
if(xyzAxis < threshold) {
xyzAxis = map(xyzAxis, threshold, yAxisMin, 1, 8);
Serial.print(F("TU"));
Serial.println(xyzAxis);
joystickValues[2] = xyzAxis;
}
else {
joystickValues[2] = 0; //Clears the last stored value.
}
//Tilt down.
threshold = yCenter + 25;
if(xyzAxis > threshold) {
xyzAxis = map(xyzAxis, threshold, yAxisMax, 1, 8);
Serial.print(F("TD"));
Serial.println(xyzAxis);
joystickValues[3] = xyzAxis;
}
else {
joystickValues[3] = 0; //Clears the last stored value.
}
xyzAxis = analogRead(pin_zAxis);
//Anticlockwise.
threshold = zCenter - 25;
if(xyzAxis < threshold) {
xyzAxis = map(xyzAxis, threshold, zAxisMin, 1, 8);
if(joystickButton == 1) {
joystickValues[4] = 1; //Zoom mode.
Serial.print(F("ZI"));
Serial.println(1);
}
else if(joystickButton == 2){
joystickValues[6] = 1; //Focus mode.
Serial.print(F("FF"));
Serial.println(1);
}
else if(joystickButton == 3){
joystickValues[8] = 1; //Iris mode.
Serial.print(F("IO"));
Serial.println(1);
}
}
else {
joystickValues[4] = 0; //Clears the last stored value.
joystickValues[6] = 0; //Clears the last stored value.
joystickValues[8] = 0; //Clears the last stored value.
}
//Clockwise.
threshold = yCenter + 25;
if(xyzAxis > threshold) {
xyzAxis = map(xyzAxis, threshold, zAxisMax, 1, 8);
if(joystickButton == 1) {
joystickValues[5] = 1; //Zoom mode.
Serial.print(F("ZO"));
Serial.println(1);
}
else if(joystickButton == 2){
joystickValues[7] = 1; //Focus mode.
Serial.print(F("FN"));
Serial.println(1);
}
}
else if(joystickButton == 3){
joystickValues[9] = 1; //Iris mode.
Serial.print(F("IC"));
Serial.println(1);
}
else {
joystickValues[5] = 0; //Clears the last stored value.
joystickValues[7] = 0; //Clears the last stored value.
joystickValues[9] = 0; //Clears the last stored value.
}
delay(1000);
protocol_pelcoD();
}
// Reads the min, max and center values of the joystick.
void joystickCalabration() {
delay(3000);
Serial.println(F("Calabrate Joystick"));
Serial.print(F("Pan Right "));
delay(3000);
xAxisMin = analogRead(pin_xAxis);
Serial.println(xAxisMin);
Serial.print(F("Pan Left "));
delay(3000);
xAxisMax = analogRead(pin_xAxis);
Serial.println(xAxisMax);
Serial.print(F("Tilt Up "));
delay(3000);
yAxisMin = analogRead(pin_yAxis);
Serial.println(yAxisMin);
Serial.print(F("Tilt Down "));
delay(3000);
yAxisMax = analogRead(pin_yAxis);
Serial.println(yAxisMax);
Serial.print(F("Anticlockwise "));
delay(3000);
zAxisMin = analogRead(pin_zAxis);
Serial.println(zAxisMin);
Serial.print(F("Clockwise "));
delay(3000);
zAxisMax = analogRead(pin_zAxis);
Serial.println(zAxisMax);
Serial.println(F(".Release Joystick."));
delay(3000);
Serial.print(F("xCenter "));
xCenter = analogRead(pin_xAxis);
Serial.println(xCenter);
Serial.print(F("yCenter "));
yCenter = analogRead(pin_yAxis);
Serial.println(yCenter);
Serial.print(F("zCenter "));
zCenter = analogRead(pin_zAxis);
Serial.println(zCenter);
Serial.println(F("Calabrate Complete"));
}