Now that we have a the IR Sensor working, let's get it driving. This code will look similar to the Sumo Bot code. Just like before, you'll need to change everything in bright greento make the robot do what you want. When you're done, this code will allow the robot to drive forward until there is something in its path. It should then turn until the path is clear before starting to drive forward again.
Here's what the text-based code could look like
void setup()
{
pinMode( 2, INPUT);
}
void loop()
{
if (digitalRead(2))
{
//Code to make it drive forward goes here
}
else
{
//Code to make it turn goes here
}
}
This code will prevent your robot from driving into objects but it only turns one direction. This means it won't work very well for solving a maze.
Independent Challenge: Maze Solver
Can you work with a partner to create a robot with two IR sensors: one pointed forward and right, one pointed forward and left? This will allow your robot to detect objects in front of it and know which side they are on. That way the robot can turn either way to avoid running into things. You'll probably need to use multiple if/then blocks and an "AND" block. Here's one possible solution. Note that the second IR sensor is connected to pin 4.