-
Notifications
You must be signed in to change notification settings - Fork 0
/
Zombie.ino
307 lines (255 loc) · 7.91 KB
/
Zombie.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
// digital pin 2 has a pushbutton attached to it. Give it a name:
// Set up one relay to apply power to the motor
// and two to control direction by switching the 2 motor power leads between power and
// ground in respectively opposite polarities.
int CCMotorPower = 2;
int CCDirRelay_1 = 3;
int CCDirRelay_2 = 4;
//Project phototransistor setup
int photoTran = A7;
int reading = 0;
int lastval=0;
int initval=0;
char cmdbuf[200];
char valbuff[5];
unsigned long new_cmd_val=0;
boolean logtoserial=true;
void log_message(String message)
{
if(logtoserial == true){
Serial.println(message);
}
}
void HeadUp()
{
log_message("Head UP");
digitalWrite(LED_BUILTIN, HIGH);
// deenergize relay to connect NC contacts
digitalWrite(CCDirRelay_1, HIGH);
digitalWrite(CCDirRelay_2, HIGH);
// wait a little for the direction relays to settle
delay(250);
// just turn on motor for 250ms second
digitalWrite(CCMotorPower, LOW);
delay(250);
digitalWrite(CCMotorPower, HIGH);
digitalWrite(CCDirRelay_1, HIGH);
digitalWrite(CCDirRelay_2, HIGH);
}
void HeadDown()
{
log_message("Head Down");
digitalWrite(LED_BUILTIN, LOW);
// deenergize relays to connect NO contacts
digitalWrite(CCDirRelay_1, LOW);
digitalWrite(CCDirRelay_2, LOW);
delay(250);
// just turn on motor for 500 ms second
digitalWrite(CCMotorPower, LOW);
delay(250);
digitalWrite(CCMotorPower, HIGH);
digitalWrite(CCDirRelay_1, HIGH);
digitalWrite(CCDirRelay_2, HIGH);
}
void HeadCycle(){
// init head dir
int idir=1;
int hdown = false;
int trigger = false;
int offdelay = 0;
int dimmedval = 1;
while(true){
// sample the light trigger
// take a current light level reading
// force a min value of 0 so we are not multiplying by 0 below.
reading = analogRead(photoTran);
if(reading == 0){
reading=1;
}
//Serial.println(reading);
// if the current reading is 5 times brighter than the last reading
// then assume the trigger light is on.
if(reading > (lastval*5)){
Serial.print("on:");Serial.println(reading);
lastval=reading;
// if transitioning from trigger off to on then inti head)
if(trigger == false) {
HeadUp();
delay(500);
}
trigger=true;
}
// if the current reading is 5 times dimmer than the last reading
// then assume the trigger light is off.
else if(reading*5 < lastval){
lastval=reading;
if(trigger == true) {
// looks like the trigger light shut off
// do a sample of light to make sure
dimmedval = lastval;
// get a new sample for lastval
SampleLight(20);
// the new sample might vary but not be too much brighter
// than the inital dim value to be considered still off
Serial.print("off sample results ");Serial.print(lastval);Serial.print(":");Serial.println(dimmedval+10);
if(lastval <= dimmedval+10){
HeadDown();
//break;
trigger = false;
Serial.print("off:");Serial.println(lastval);
}
//}
//else {
// Serial.print("off delay:");Serial.println(offdelay);
//}
}
}
if(trigger == true){
idir = random(1,3);
Serial.println(idir);
if(idir == 1){
HeadUp();
hdown = false;
}
else {
if (hdown == false){
HeadDown();
hdown = true;
}
else {
HeadUp();
}
}
//delay(random(3, 6)*250);
delay(random(2, 7)*250);
}
// trigger is false so delay 200ms to take next trigger sample
else {
delay(200);
}
}
}
// take a 5 second sample of the current light level and use as the intial value;
void SampleLight(int sdel){
lastval=0;
initval=0;
for(int i=1; i<=10; ++i){
reading = analogRead(photoTran);
Serial.print(i);Serial.print(":");Serial.println(reading);
initval = initval+ reading;
lastval = initval/i;
Serial.println(lastval);
delay(sdel);
}
// make sure lastval is at least 1
if(lastval <=0){
lastval=1;
}
}
void setup()
{
// init motor control relays
// control pins set to High remove power from the relay coils
// and puts then in thier NC state
pinMode(CCMotorPower, OUTPUT);
digitalWrite(CCMotorPower, HIGH);
pinMode(CCDirRelay_1, OUTPUT);
digitalWrite(CCDirRelay_1, HIGH);
pinMode(CCDirRelay_2, OUTPUT);
digitalWrite(CCDirRelay_2, HIGH);
pinMode(LED_BUILTIN, OUTPUT);
// opens serial port, sets data rate to 9600 bps
Serial.begin(9600);
//Serial.setTimeout(time_increment);
Serial.println("Starting Zombie Head");
// setup and intialize photo transistor trigger
pinMode(photoTran,INPUT);
// take an initial sample of the current light level and use as the intial value;
SampleLight(500);
// set intial levl to atleast one so we are not multiplying by 0 in loop below.
if(lastval == 0){
lastval=1;
}
}
void loop()
{
String parse_cmd; // string object to parse
int parse_eql; // index of equal sign in command string
boolean parse_error; // parse error flag
String cmd;
String cmd_val;
int bytes_read=0;
int last_cmd_val;
//Serial.println("checking for cmd");
// check for a command
if (Serial.available() > 0) {
// read the command
cmdbuf[0] ='\0';
bytes_read =0;
bytes_read=Serial.readBytesUntil('\n', cmdbuf, 200);
//Serial.println(bytes_read);
cmdbuf[bytes_read] ='\0';
log_message("cmd:");
log_message(cmdbuf);
//Parse the command
parse_cmd = String(cmdbuf);
//Serial.print("cmd:");
//Serial.println(parse_cmd);
// first do some cleanup
//parse_cmd.toLowerCase();
parse_cmd.trim();
//flag invalid commands or syntax
parse_error = false;
// find the command , assume the first 3 characters
cmd = parse_cmd; //parse_cmd.substring(0,3);
//runflag=false;
// valid commands
// LogSerial
//"HeadUp"
//"HeadDown"
//
if(cmd.startsWith("LogSerial")) // this sets if logging should be output to the serial port
{
parse_eql = cmd.indexOf("=");
if(parse_eql > -1)
{
cmd_val=cmd.substring(parse_eql+1);
cmd_val.toCharArray(valbuff, 4);
new_cmd_val = atoi(&valbuff[0]);
//Serial.println(new_insert_freq);
//if(new_insert_freq> 0)
if(new_cmd_val==1){
logtoserial = true;
Serial.println("Log to serial = true ");
}
else{
logtoserial = false;
Serial.println("Log to serial = false ");
}
}
}
// execute a head up command
else if(cmd.startsWith("HeadUp"))
{
HeadUp();
}
// execute a head down command
else if(cmd.startsWith("HeadDown"))
{
HeadDown();
}
else if(cmd.startsWith("HeadCycle"))
{
HeadCycle();
}
else // just ignore the command b/c it's not recongized
{
parse_error = true;
}
}
// not taking command via the serial port so just cycle the head
else
{
HeadCycle();
}
}