Stap 5: programmering
Veel camera's die worden geëxploiteerd door RS232 signalering hexadecimale opdrachten gebruiken. Deze opdrachten worden verzonden naar de camera als een reeks hexadecimale bytes. Een momentopname van de handleiding waarin enkele van de opdrachten is hierboven weergegeven.
Een hexadecimale byte-array is gemaakt voor elke opdracht die u wilt gebruiken. Wanneer de opdracht is het belangrijk overeenkomen met de baud-rate van uw apparaat en verzendt elke byte in de array opeenvolgend. Van de handleiding is er gezien dat de baud-rate is 9600 bps dus dat is wat is opgegeven in de code. Het is ook belangrijk om op te geven welke poorten u voor TX en RX gebruiken gaat. Dit wordt gedaan bij het opzetten van een SoftwareSerial.
Deze voorbeeldcode hieronder heeft de camera 2 vooraf gedefinieerde posities elke vijf seconden afwisselend.
// Serial Communication // October 29, 2015 // Purpose: Control the camera position using RS232 communications through a DB9 connector // with an attached RS232 Shield. // Status: WORKING#include <SoftwareSerial.h>// Do not use 0 and 1 if you want to be able to upload code while shield is attached to Arduino SoftwareSerial mySerial(2, 3); //2 is TX, 3 is RX // Camera Commands // Default message 8x 01 04 3F 02 0p FF where x is camera number and p is preset number byte preset1[7] = {0x81, 0x01, 0x04, 0x3F, 0x02, 0x01, 0xFF}; // Camera 1 preset byte preset2[7] = {0x81, 0x01, 0x04, 0x3F, 0x02, 0x02, 0xFF}; // Camera 2 presetbyte address_command[4] = {0x88, 0x30, 0x01, 0xFF}; // Sets camera address (Needed for Daisy Chaining) byte if_clear[5] = {0x88, 0x01, 0x00, 0x01, 0xFF}; // Checks to see if communication line is clearint delayTime = 500; //Time between commandsvoid setup() { Serial.begin(9600); // Serial Communication w/ Computer for Debugging mySerial.begin(9600); // Serial Communication w/ Camera //Send Address command for (int i = 0; i < 4; i++) { mySerial.write(address_command[i]); Serial.println("Address Byte Sent"); } delay(delayTime); //delay to allow camera time for next command //Send IF_clear command for (int i = 0; i < 5; i++) { mySerial.write(if_clear[i]); Serial.println("If Clear Byte Sent"); } }void loop() { for (int i = 0; i < (sizeof(preset1)); i++) // sizeof returns a count of bytes, not numbers if using int, float, or long { mySerial.write(preset1[i]); // sends each byte sequentially Serial.println("Camera Position 1 Byte Sent"); } delay(5000); // Waits for 5 seconds for (int i = 0; i < (sizeof(preset2)); i++) // sizeof returns a count of bytes, not numbers if using int, float, or long { mySerial.write(preset2[i]); // sends each byte sequentially Serial.println("Set Camera Position 2 Byte Sent"); } delay(5000); }
Het tweede voorbeeldcode kunt u bepalen van de richting waarin de camera pannen middels het commando's via het terminal-venster op uw computer.
// Serial Communication // October 30, 2015 // Purpose: Control the camera position using keyboard inputs // Status: WORKING#include <SoftwareSerial.h>// Do not use 0 and 1 if you want to be able to upload code while shield is attached to Arduino SoftwareSerial mySerial(2, 3); //2 is TX, 3 is RX // Camera Commands // Default message 8x 01 04 3F 02 0p FF where x is camera number and p is preset number byte preset1[7] = {0x81, 0x01, 0x04, 0x3F, 0x02, 0x01, 0xFF}; // Camera 1 preset byte preset2[7] = {0x81, 0x01, 0x04, 0x3F, 0x02, 0x02, 0xFF}; // Camera 2 preset// Camera Controls byte panLeft[9] = {0x81, 0x01, 0x06, 0x01, 0x09, 0x09, 0x01, 0x03, 0xFF}; // Pan Camera Left byte panRight[9] = {0x81, 0x01, 0x06, 0x01, 0x09, 0x09, 0x02, 0x03, 0xFF}; // Pan Camera Right byte panUp[9] = {0x81, 0x01, 0x06, 0x01, 0x09, 0x09, 0x03, 0x01, 0xFF}; // Pan Camera Up byte panDown[9] = {0x81, 0x01, 0x06, 0x01, 0x09, 0x09, 0x03, 0x02, 0xFF}; // Camera Pan Down byte panStop[9] = {0x81, 0x01, 0x06, 0x01, 0x09, 0x09, 0x03, 0x03, 0xFF}; // Camera Pan Downbyte address_command[4] = {0x88, 0x30, 0x01, 0xFF}; // Sets camera address (Needed for Daisy Chaining) byte if_clear[5] = {0x88, 0x01, 0x00, 0x01, 0xFF}; // Checks to see if communication line is clearint delayTime = 500; //Time between commandsvoid setup() { Serial.begin(9600); // Serial Communication w/ Computer for Debugging mySerial.begin(9600); // Serial Communication w/ Camera //Send Address command for (int i = 0; i < 4; i++) { mySerial.write(address_command[i]); } delay(delayTime); //delay to allow camera time for next command //Send IF_clear command for (int i = 0; i < 5; i++) { mySerial.write(if_clear[i]); } Serial.print("Please Input Commands"); }void loop() { // Checks for serial input from terminal if (Serial.available() > 0) { char inChar = Serial.read(); // read incoming serial data: if (inChar == '1') // Camera Preset 1 { for (int i = 0; i < (sizeof(preset1)); i++) { mySerial.write(preset1[i]); // sends each byte sequentially } } else if (inChar == '2') // Camera Preset 1 { for (int i = 0; i < (sizeof(preset2)); i++) { mySerial.write(preset2[i]); } } else if (inChar == 'w') // Pan Camera Up { for (int i = 0; i < (sizeof(panUp)); i++) { mySerial.write(panUp[i]); } } else if (inChar == 'a') // Pan Camera Left { for (int i = 0; i < (sizeof(panLeft)); i++) { mySerial.write(panLeft[i]); } } else if (inChar == 's') // Pan Camera Down { for (int i = 0; i < (sizeof(panDown)); i++) { mySerial.write(panDown[i]); } } else if (inChar == 'd') // Pan Camera Right { for (int i = 0; i < (sizeof(panRight)); i++) { mySerial.write(panRight[i]); } } else if (inChar == 'q') // Stop Camera Pan { for (int i = 0; i < (sizeof(panStop)); i++) { mySerial.write(panStop[i]); } } } }