Page 223 - 15 Dangerously Mad Projects for the Evil Genius
P. 223
Chapter 15 ■ Surveillance Robot 199
LISTING 15-1 (continued) LISTING 15-1 (continued)
{ lastProx = prox;
int alarmTimeout = 0; if (prox > proxThreshold)
for (int i = 1; i < monitorDuration; {
i++) halt();
{ buzz(50); buzz(50);
int pirValue = delay(100);
analogRead(pirPin); back();
if (pirValue > 10) delay(700);
{ halt();
digitalWrite(buzzPlusPin, HIGH); return;
alarmTimeout = alarmDuration; }
} x += 10;
if (alarmTimeout <= 0) delay(10);
{ }
digitalWrite(buzzPlusPin, LOW); }
}
alarmTimeout —; void forward()
delay(1000); {
} setLeft(CLOCKWISE);
} setRight(CLOCKWISE);
}
void moveToNewPlace()
{ void back()
turnInRandomDirection(); {
forwardOrProximity(1500); setLeft(COUNTER_CLOCKWISE);
} setRight(COUNTER_CLOCKWISE);
}
void turnInRandomDirection()
{ void left()
int duration = random(100, 3000); {
left(); setLeft(CLOCKWISE);
delay(duration); setRight(COUNTER_CLOCKWISE);
halt(); }
}
void right()
void forwardOrProximity(int duration) {
{ setLeft(COUNTER_CLOCKWISE);
int x = 0; setRight(CLOCKWISE);
forward(); }
static float lastProx = 0;
float prox = 0; void halt()
while (x < duration) {
{ setLeft(HALT);
int rawProx = analogRead(proxPin); setRight(HALT);
prox = alpha * rawProx + (1 - }
alpha) * lastProx;
Serial.print(rawProx); void setLeft(int rotation)
Serial.print(" "); {
Serial.print(lastProx); if (rotation == HALT)
Serial.print(" "); {
Serial.println(prox); digitalWrite(leftAPin, LOW);
(continued on next page)