Page 222 - 15 Dangerously Mad Projects for the Evil Genius
P. 222
198 15 Dangerously Mad Projects for the Evil Genius
Step 11. Testing the Robot Theory
Set it down in the middle of the room and turn it In this section, we take a detailed look at the code
on. It will start in its monitoring mode, so if you for the robot, the distance sensor and also look at
move in front of it, it should detect you and sound techniques for controlling motors.
the alarm. After a while, it should rotate in a
random direction and set off for its new position.
The Program
If it finds itself on a collision course, it will
The sketch (Arduino parlance for a program) is
back up a little before going back into monitoring
shown in Listing 15-1.
mode.
The program makes a great starting point for
You may find that your motor connections are
anyone wishing to develop robots using the Arduino.
the wrong way around and have to swap over a
few leads. If your robot is behaving erratically, you At the top of the file, we define the various pins
can find a helpful test sketch on this at to be used. The two variables—proxThreshold and
www.dangerouslymad.com. It lets you send pirThreshold—set the value that must be exceeded
commands using the Serial Monitor to move to indicate an imminent collision or detection of
forward, back, left, and right, as well as report the movement, respectively, using the outputs of these
values from the sensors.
LISTING 15-1 LISTING 15-1 (continued)
// Surveillance Bot pinMode(leftBPin, OUTPUT);
pinMode(rightAPin, OUTPUT);
#define HALT 0 pinMode(rightBPin, OUTPUT);
#define CLOCKWISE 1 pinMode(pirPin, INPUT);
#define COUNTER_CLOCKWISE 2
digitalWrite(leftAPin, LOW);
int leftAPin = 7; digitalWrite(leftBPin, LOW);
int leftBPin = 6; digitalWrite(rightAPin, LOW);
int rightAPin = 5; digitalWrite(rightBPin, LOW);
int rightBPin = 4;
pinMode(posPin, OUTPUT);
int posPin = 14; pinMode(negPin, OUTPUT);
int negPin = 15; pinMode(buzzPlusPin, OUTPUT);
int proxPin = 2; pinMode(buzzMinusPin, OUTPUT);
int pirPin = 3; digitalWrite(posPin, HIGH);
int buzzPlusPin = 9; digitalWrite(negPin, LOW);
int buzzMinusPin = 8; Serial.begin(9600);
}
float proxThreshold = 500;
float alpha = 0.5; void loop()
int pirThreshold = 10; {
int monitorDuration = 120; // seconds monitor();
int alarmDuration = 10; // seconds moveToNewPlace();
delay(1000);
void setup() }
{
pinMode(leftAPin, OUTPUT); void monitor()