forked from Make3DPrintingProjects/Skycam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathskycam.ino
157 lines (146 loc) · 2.93 KB
/
skycam.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
#include <Wire.h>
#include <Servo.h>
const byte slaveAddr = 0x50;
int registerAddr;
String value = "";
String toSend = "";
int i = 0;
int drive1Pin = 5;
int drive2Pin = 6;
int panPin = 10;
int tiltPin = 11;
Servo drive1;
Servo drive2;
Servo pan;
Servo tilt;
void off() {
digitalWrite(7, HIGH);
delay(20000);
digitalWrite(8, HIGH);
digitalWrite(7, LOW);
}
String getDriveDirection() {
int g = drive1.read();
if (g > 90) {
return "1";
} else if (g < 90) {
return "0";
} else {
return "2";
}
}
void center() {
pan.write(180);
tilt.write(90);
}
void parseString(String s) {
int comma = s.indexOf(',');
String command = s.substring(0, comma);
int value = s.substring(comma + 1).toInt();
if (command == "move") {
if (value == 0) {
drive1.write(45);
drive2.write(45);
toSend = getDriveDirection();
}
else if (value == 1) {
drive1.write(135);
drive2.write(135);
toSend = getDriveDirection();
}
else if (value == -1) {
toSend = getDriveDirection();
}
else {
drive1.write(90);
drive2.write(90);
toSend = getDriveDirection();
}
}
else if (command == "pan") {
if (value == 0) {
int cv = pan.read();
pan.write(cv + 5);
toSend = String(cv + 5);
} else if (value == 1) {
int cv = pan.read();
pan.write(cv - 5);
toSend = String(cv - 5);
} else if (value == -1) {
toSend = String(pan.read());
} else {
pan.write(180);
}
}
else if (command == "tilt") {
if (value == 0) {
int cv = tilt.read();
tilt.write(cv + 5);
toSend = String(cv + 5);
} else if (value == 1) {
int cv = tilt.read();
tilt.write(cv - 5);
toSend = String(cv - 5);
} else if (value == -1) {
toSend = String(tilt.read());
} else {
tilt.write(135);
}
}
}
void receiveRegister(int x){
int inChar;
inChar = Wire.read();
if(char(inChar) != '\n'){
value += char(inChar);
}
else {
Serial.print("I Recieved ");
Serial.println(value);
parseString(value);
value = "";
}
}
void respondData(){
int len = toSend.length()+1;
if(i+1<len){
char ascii_num[len];
toSend.toCharArray(ascii_num, len);
Serial.print("Sending char ");
Serial.println(ascii_num[i]);
Wire.write(ascii_num[i]);
i += 1;
}
else {
Wire.write('\n');
i = 0;
}
}
void loop() {
// put your main code here, to run repeatedly:
delay(100);
if (digitalRead(12)) {
off();
}
}
void setup() {
TWBR=100000L;
Serial.begin(9600);
Wire.begin(slaveAddr);
Wire.onReceive(receiveRegister);
Wire.onRequest(respondData);
drive1.attach(drive1Pin);
drive2.attach(drive2Pin);
pan.attach(panPin);
tilt.attach(tiltPin);
drive1.write(90);
drive2.write(90);
pan.write(180);
tilt.write(135);
pinMode(7, OUTPUT);
digitalWrite(7, LOW);
pinMode(8, OUTPUT);
digitalWrite(7, LOW);
pinMode(12, INPUT);
delay(500);
}