Hardware Reference
In-Depth Information
The
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
state-
ment sets up the sonar sensor so that it knows about the proper trigger and echo pins. In
the
setup()
function, you are going to set the
OUTPUT
mode for the two pins that will
control the DC motors and then turn one motor on (the motor that powers the walking)
and the other motor off (the motor that turns the device).
The
loop()
function checks the sonar sensor using the
sonar.ping()
command.
When the distance is greater than
0
and less than
5
, you will turn the walking motor off,
turn the turning motor on for one second, and then turn the turning motor off and turn the
walking motor back on. When the spider encounters an object, the program should turn
the walking motor off, turn the spider by about 90 degrees, and then turn the walking mo-
tor back on.
Now, upload your program. Your robot should now move forward, and when it senses a
barrier, it stops and turns and then moves in the new direction. Your robot can move
around and sense barriers! Implementing the intelligence to go between points A and B
while avoiding barriers is an interesting and complex problem that you can now explore.
There are algorithms that show how to program your robot to respond when it senses a
barrier by moving efficiently around it. For more information on this, see
re-