How to Program the Ultrasonic Sensor + Sumo Bot

This lesson will demonstrate how to use the Ultrasonic Sensor combined with the Sumo Bot

This Robot will behave as a normal sumo bot but will have the added feature of being able to detect another sumo bot in front of it. Once it locks onto another robot it rams at full speed while making sure to avoid crossing the sumo ring.

Wiring up

You will need four female to female jumper cables and of course the ultrasonic sensor.

Hookup the red wire from the “VCC” pin from the sensor to V on the Inventor board. The yellow wire in this case is connected to the TRIG pin which is connected to the smart inventor board on pin 4 of PORTD. The white wire is connected to the ECHO pin which is connected to the smart inventor board on pin 5 of PORTD.  The green wire is connected to the ground “GND” pin which can be any of the pins labeled G.

Programming the Ultrasonic Sensor

First, begin the code by including the smart inventor library.

Enter the following before the [crayon-666c85e6a45c8223175153-i/] method.

Learn More: What’s a library?

A library is a saved set of [crayon-666c85e6a45d2861308848-i/]  and variables that provide users with improved functionality when coding.

Any code that starts with [crayon-666c85e6a45d3777902624-i/]  is a function in the SmartInventor library.  Any variable that highlights ORANGE, is an initialized variable from an included library.

Learn More

Now declare the following variables. One will be for speed and the others will be used to time the amount of time the robot goes backwards or forwards when it reaches the black line on the sumo ring.
We will assign the pin numbers the Ultrasonic Sensor will be connected to.

Next, inside  [crayon-666c85e6a45d6076048760-i/] , assign the pin mode for the trigger pin and echo pin.
Do the same for all the IR sensors on the smart inventor board. Declare them all as inputs.
Now setup the Smart inventor board so it may use the motors.
Make the Sumobot start off going forwards.

Next, inside  [crayon-666c85e6a45dd095867683-i/] , we will now create and set the variable [crayon-666c85e6a45df631836817-i/] , with the method [crayon-666c85e6a45e0088737137-i/] which we have not yet created. Later in the code the function will be made.
So now we need a way to know how far away the other robot is, one way is to use the buzzer to change its sound frequency so the tone changes. The way we will do this is with this code.
So if the distance to the object in front of it is below 50 centimeters we want the Inventor board to buzz. The starting tone is 2000 and if the distance to the object is lets say 20 centimeters then the buzzer will buzz at 2000-(20)x25=1500. Thereby the closer the object the higher the frequency.

SmartInventor.Buzz( )

SmartInventor.Buzz( Frequency, Tempo);

This function will allow you to input numbers for the frequency and tempo and based on those values you can manipulate the sound that the buzzer makes.

Frequency is the amount of vibrations that the buzzer will make within a designated time. Acceptable numbers will range from 0 to 9,223,372,036,854,775,807.

Tempo is the speed at which you want the frequency to occur. Acceptable numbers will range from 0 to 2,147,483,647.

Next, type this in your program:
The variables backTime and turnTime should be incremented by 1 every iteration. This is so the program can keep track of how long the Sumobot has been going forward and backwards. The if statement checks to make sure that backTime & turnTime don’t exceed 200/500, respectively. When that happens in either case, the variable will be reset to a value of 100. Later on, we’ll explain the purpose of these two variables.

Now we must tell the IR sensors what to do when they detect the black sumo ring line. Our program uses all the IR sensors from pins 11 through pins 18, with the exception of pins 14 and 15. This is just in case one of the IR sensor fails to detect the line.

Type the following code into your program:
This if statement basically checks if IR sensor 11 or 12 or 13 do not detect any IR light. These IR sensors are all the ones on the bottom-left hand side of the Inventor board. The pin stays [crayon-666c85e6a45e7594264761-i/] when no IR light is reflected (meaning the black ring has absorbed the infrared light from the sensor).

Now let’s tell the Sumobot what to do when the sensor detects the black ring:
The robot will move back and buzz then it will turn to the right and finally it will move forward again.

Here you can see how [crayon-666c85e6a45e9258105401-i/]and [crayon-666c85e6a45eb184002880-i/]  are used, they basically extend the amount of time the delays last.

We now will code for the right IR sensors by typing.
This piece works similar to the other if statement but now it just checks the right side for the black ring.

Now we will use the ultrasonic sensor to ram at full speed if no black ring is detected and the object is detected. So type the following.
This checks every sensor for no black ring so all pins would be [crayon-666c85e6a45f1469783897-i/]  and as well checks if the [crayon-666c85e6a45f2631540910-i/]  is below 6 centimeters.

You are now done with the main loop, now we will create the function that produces the distance and returns the value of distance.  So outside of the curly braces of the loop, begin by typing
We are now creating a method or function that when used in the program the function returns an integer. Usually when we used methods the word [crayon-666c85e6a45f5604562539-i/]  is used, which just means that when the function is used nothing is returned. Now we will code the actual function that does the math and detecting of the distance.
Basically the sensor turns on and off the trigger speaker emitting a pulse of high frequency sound, the echo speaker just waits and times the amount of time it takes. Then once the time is known, the program does the math to find the distance traveled. This distance is then “returned” with the line
You are now done coding!

Final code