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-6621e2b16ce77500892766-i/] method.
[crayon-6621e2b16ce7e358292187/]

Learn More: What’s a library?

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

Any code that starts with [crayon-6621e2b16ce80972046182-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.
[crayon-6621e2b16ce82083875847/]
We will assign the pin numbers the Ultrasonic Sensor will be connected to.
[crayon-6621e2b16ce83395586910/]

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

Next, inside  [crayon-6621e2b16ce8c735364391-i/] , we will now create and set the variable [crayon-6621e2b16ce8d929969434-i/] , with the method [crayon-6621e2b16ce8e000826730-i/] which we have not yet created. Later in the code the function will be made.
[crayon-6621e2b16ce8f173797476/]
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.
[crayon-6621e2b16ce90471003463/]
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:
[crayon-6621e2b16ce91080065252/]
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:
[crayon-6621e2b16ce92973173855/]
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-6621e2b16ce93557214107-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:
[crayon-6621e2b16ce95475197929/]
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-6621e2b16ce96456872134-i/]and [crayon-6621e2b16ce97395851620-i/]  are used, they basically extend the amount of time the delays last.

We now will code for the right IR sensors by typing.
[crayon-6621e2b16ce98442394709/]
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.
[crayon-6621e2b16ce99175024422/]
This checks every sensor for no black ring so all pins would be [crayon-6621e2b16ce9c594937263-i/]  and as well checks if the [crayon-6621e2b16ce9d473250252-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
[crayon-6621e2b16ce9e957492319/]
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-6621e2b16ce9f142391812-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.
[crayon-6621e2b16cea0797692015/]
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
[crayon-6621e2b16cea1377572707/]
You are now done coding!

Final code
[crayon-6621e2b16cea2365814833/]