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