Advertisement
Guest User

Untitled

a guest
Jun 1st, 2022
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <SoftwareSerial.h>
  2.  
  3. const byte rxPin = 3;
  4. const byte txPin = 4;
  5.  
  6. SoftwareSerial SerialRS = SoftwareSerial(rxPin, txPin);
  7.  
  8. const int LED = 13;
  9.  
  10. const int P_LEFT = 12;    //left
  11. const int P_UP = 11;      //up
  12. const int P_RIGHT = 10;   //right
  13. const int P_DOWN = 9;     //down
  14. const int P_ZOOM_IN = 7;  //zoom in
  15. const int P_ZOOM_OUT = 8; //zoom out
  16.  
  17. const byte address = 1; //camera address
  18. byte speed = 100; // can be changed by pressing left+up or left+down
  19.  
  20. const byte C_STOP = 0x00;
  21. const byte C_UP = 0x08;
  22. const byte C_DOWN = 0x10;
  23. const byte C_LEFT = 0x04;
  24. const byte C_RIGHT = 0x02;
  25.  
  26. const byte C_ZOOMIN = 0x20;
  27. const byte C_ZOOMOUT = 0x40;
  28.  
  29. const byte C_SET_PAN_POSITION = 0x4B;  // 1/100ths of degree
  30. const byte C_SET_TILT_POSITION = 0x4D; // 1/100ths of degree
  31.  
  32. bool stoped = false;
  33.  
  34. void setup()
  35. {
  36.   pinMode(LED, OUTPUT);
  37.  
  38.   pinMode(P_LEFT, INPUT);     //left
  39.   pinMode(P_UP, INPUT);       //up
  40.   pinMode(P_RIGHT, INPUT);    //right
  41.   pinMode(P_DOWN, INPUT);     //down
  42.   pinMode(P_ZOOM_IN, INPUT);  //zoom in
  43.   pinMode(P_ZOOM_OUT, INPUT); //zoom out
  44.  
  45.   Serial.begin(9600);
  46.   SerialRS.begin(2400);
  47.   Serial.println("Pelco D controller");
  48.  
  49.   sendPelcoDFrame(C_STOP, 0, 0);
  50.   delay(10);
  51.   sendPelcoDFrame(C_SET_PAN_POSITION, 23, 28);
  52.  
  53.   blinkLED();
  54. }
  55.  
  56. void loop()
  57. {
  58.  
  59.   if (check(P_LEFT) && check(P_DOWN))
  60.   {
  61.     speed = speed - 10;
  62.     if (speed < 0)
  63.       speed = 0;
  64.     Serial.print("Speed:");
  65.     Serial.println(speed);
  66.     blinkLED();
  67.     delay(1000);
  68.     return;
  69.   }
  70.  
  71.   if (check(P_LEFT) && check(P_UP))
  72.   {
  73.     speed = speed + 10;
  74.     if (speed > 255)
  75.       speed = 255;
  76.     Serial.print("Speed:");
  77.     Serial.println(speed);
  78.     blinkLED();
  79.     delay(1000);
  80.     return;
  81.   }
  82.  
  83.   if (check(P_LEFT))
  84.   {
  85.     sendPelcoDFrame(C_LEFT, speed, speed);
  86.     Serial.println("left");
  87.     stoped = false;
  88.   }
  89.   else if (check(P_RIGHT))
  90.   {
  91.     sendPelcoDFrame(C_RIGHT, speed, speed);
  92.     Serial.println("right");
  93.     stoped = false;
  94.   }
  95.   else if (check(P_UP))
  96.   {
  97.     sendPelcoDFrame(C_UP, speed, speed);
  98.     Serial.println("up");
  99.     stoped = false;
  100.   }
  101.   else if (check(P_DOWN))
  102.   {
  103.     sendPelcoDFrame(C_DOWN, speed, speed);
  104.     Serial.println("down");
  105.     stoped = false;
  106.   }
  107.   else if (check(P_ZOOM_IN))
  108.   {
  109.     sendPelcoDFrame(C_ZOOMIN, 0, 0);
  110.     Serial.println("zoom in");
  111.     stoped = false;
  112.   }
  113.   else if (check(P_ZOOM_OUT))
  114.   {
  115.     sendPelcoDFrame(C_ZOOMOUT, 0, 0);
  116.     Serial.println("zoom out");
  117.     stoped = false;
  118.   }
  119.   else
  120.   {
  121.     if (!stoped)
  122.     {
  123.       sendPelcoDFrame(C_STOP, 0, 0);
  124.       delay(10);
  125.       sendPelcoDFrame(C_STOP, 0, 0);
  126.       Serial.println("stop");
  127.       stoped = true;
  128.     }
  129.   }
  130.  
  131.   if (!stoped)
  132.   {
  133.     digitalWrite(LED, HIGH);
  134.   }
  135.   else
  136.   {
  137.     digitalWrite(LED, LOW);
  138.   }
  139.  
  140.   delay(100);
  141. }
  142.  
  143. bool check(int pin)
  144. {
  145.   return (digitalRead(pin) == HIGH);
  146. }
  147.  
  148. void sendPelcoDFrame(byte command, byte data1, byte data2)
  149. {
  150.   byte bytes[7] = {0xFF, address, 0x00, command, data1, data2, 0x00};
  151.   byte crc = (bytes[1] + bytes[2] + bytes[3] + bytes[4] + bytes[5]) % 0x100;
  152.   bytes[6] = crc;
  153.  
  154.   for (int i = 0; i < 7; i++)
  155.   {
  156.     SerialRS.write(bytes[i]);
  157.     // Serial.print(bytes[i], HEX); //debug
  158.   }
  159.   // Serial.println(); //debug
  160. }
  161.  
  162. void blinkLED()
  163. {
  164.   for (int i = 0; i < 4; i++)
  165.   {
  166.     digitalWrite(LED, HIGH);
  167.     delay(100);
  168.     digitalWrite(LED, LOW);
  169.     delay(100);
  170.   }
  171. }
  172.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement