Advertisement
Guest User

Untitled

a guest
Nov 1st, 2016
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.71 KB | None | 0 0
  1. /*
  2. This program is used for an Arduino to receive and decode PELCO-D PTZ Commands
  3.  
  4. Thanks to Michael Blaylock for his sketch. Learning how to read and procces serial data
  5.  
  6. */
  7.  
  8. #include <LiquidCrystal.h>
  9.  
  10. LiquidCrystal lcd(12, 11, 9, 8, 7, 6);
  11.  
  12. byte outArray[7]; // read data Pelco Command
  13. unsigned stopcheck; // For checking when a STOP command is received (257 Decimal)
  14. int checksum; // For Calculating Checksum. Sum of the payload bytes (bytes 2 through 6) in the message
  15. int ByteNumber;
  16. int MotorSpeed;
  17.  
  18. void setup(){
  19.  
  20. lcd.begin(16, 2); // set up the LCD's number of columns and rows:
  21.  
  22. Serial.begin(9600); // baud rate 9600 can be 1200,2400 or 4800
  23.  
  24. pinMode(3, OUTPUT);
  25. pinMode(5, OUTPUT);
  26. pinMode(13, OUTPUT);
  27.  
  28. digitalWrite(3, HIGH);
  29. digitalWrite(5, HIGH);
  30.  
  31. }
  32.  
  33. void displayData() // Display the array in serial monitor for debugging
  34. {
  35. for (int j = 0; j<7; j+=1){
  36. Serial.println(outArray[j],HEX);}
  37. Serial.println("Data Printed");}
  38.  
  39. void loop()
  40. {
  41. if ( Serial.available () > 0) {
  42. outArray[ByteNumber ++] = Serial.read();}
  43.  
  44. if ( ByteNumber > 6){ // process it
  45. ByteNumber = 0; // ready for next time
  46. // displayData(); // for debugging
  47.  
  48. stopcheck = outArray[0] + outArray[1] + outArray[2] + outArray[3] + outArray[4] + outArray[5] + outArray[6] ; // Calculate if STOP Command is received
  49. if ( stopcheck == 257){ // When stopcheck is 257 decimal a STOP command is received
  50. StopActions();} // Stop all PTZ Actions
  51.  
  52. else{ printCommand();} // Print Pelco command on LCD
  53.  
  54. if ( bitRead(outArray[3],0) == 0 ){ // When BIT 0 = 0 command 2 than data is Normal command (PTZ)
  55. Decoderen();} // Try to decode the Pelco Command
  56.  
  57. if ( bitRead(outArray[3],0) == 1 ){ // When BIT 0 = 1 command 2 than data is an Extended command
  58. ExtendedCommands();} // Try to decode the Extended Pelco Command
  59.  
  60. } // end if full
  61. } // end of loop
  62.  
  63.  
  64. void Decoderen()
  65. {
  66.  
  67. lcd.setCursor(0,1);
  68.  
  69. MotorSpeed = map (outArray[4], 0, 0x3F, 255, 0);
  70. // PAN TILT:
  71. if ( bitRead(outArray[3],1) == 1 ){
  72. analogWrite(3 , MotorSpeed);
  73. // digitalWrite(3, HIGH);
  74. lcd.print("RIGHT SPEED: ");
  75. lcd.print(outArray[4]);}
  76.  
  77. if ( bitRead(outArray[3],2) == 1 ){
  78. analogWrite(5, MotorSpeed);
  79. // digitalWrite(5, HIGH);
  80. lcd.print("LEFT SPEED: ");
  81. lcd.print(outArray[4]);}
  82.  
  83. if ( bitRead(outArray[3],3) == 1 ){
  84. lcd.print("UP SPEED: ");
  85. lcd.print(outArray[5]);}
  86.  
  87. if ( bitRead(outArray[3],4) == 1 ){
  88. lcd.print("DOWN SPEED: ");
  89. lcd.print(outArray[5]);}
  90.  
  91. // ZOOM IRIS FOCUS:
  92. if ( bitRead(outArray[2],2) == 1 ){
  93. lcd.print("Iris Close");}
  94. if ( bitRead(outArray[2],1) == 1 ){
  95. lcd.print("Iris Open");}
  96. if ( bitRead(outArray[2],0) == 1 ){
  97. lcd.print("Focus Near");}
  98. if ( bitRead(outArray[3],7) == 1 ){
  99. lcd.print("Focus Far ");}
  100. if ( bitRead(outArray[3],6) == 1 ){
  101. lcd.print("Zoom Wide ");}
  102. if ( bitRead(outArray[3],5) == 1 ){
  103. lcd.print("Zoom Tele ");}
  104.  
  105. }
  106.  
  107. void ExtendedCommands()
  108. {
  109.  
  110. lcd.setCursor(0,1);
  111.  
  112. if ( outArray[2] == 0 ){ // Only continu when Word 3 is 0
  113.  
  114. if ( outArray[3] == 0x03 ){ // SET PRESET
  115. lcd.print("Set Preset: ");
  116. lcd.print(outArray[5]-1);} // PRINT Preset. -1 to calculate right preset
  117.  
  118. if ( outArray[3] == 0x05 ){ // Clear Preset
  119. lcd.print("Clear Preset:");
  120. lcd.print(outArray[5]-1);} // PRINT Preset. -1 to calculate right preset
  121.  
  122. }}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement