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-670b126f7db23751730932-i/] method.
[crayon-670b126f7db2c696239388/]

Learn More: What’s a library?

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

Any code that starts with [crayon-670b126f7db2e305564033-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-670b126f7db2f602732815/]
We will assign the pin numbers the Ultrasonic Sensor will be connected to.
[crayon-670b126f7db30534564929/]

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

Next, inside  [crayon-670b126f7db36670461453-i/] , we will now create and set the variable [crayon-670b126f7db37261148386-i/] , with the method [crayon-670b126f7db38529423417-i/] which we have not yet created. Later in the code the function will be made.
[crayon-670b126f7db39418899272/]
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-670b126f7db3a574503101/]
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-670b126f7db3b023069987/]
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-670b126f7db3d533297004/]
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-670b126f7db3e577247945-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-670b126f7db3f278166707/]
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-670b126f7db40500409701-i/]and [crayon-670b126f7db41232425428-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-670b126f7db42743044331/]
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-670b126f7db43167102268/]
This checks every sensor for no black ring so all pins would be [crayon-670b126f7db46210719909-i/]  and as well checks if the [crayon-670b126f7db48426029626-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-670b126f7db49073359097/]
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-670b126f7db4a407188533-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-670b126f7db4b454466405/]
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-670b126f7db4c494664244/]
You are now done coding!

Final code
[crayon-670b126f7db4d457846956/]