How to Program the Ultrasonic Sensor + 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
void setup(); method.

Learn More: What’s a library?

A library is a saved set of
functions()  and variables that provide users with improved functionality when coding.

Any code that starts with
SmartInventor._____  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  
void setup(); , 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  
void loop(); , we will now create and set the variable
currentDistance , with the method
getPingDistance() 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.

Now type.

The variables BackTime and TurnTime add by 1 every loop. They are necessary in order for the program to keep track of how long its been turning or going backwards without using delays. Delays would make the robot drive forward and not check the IR sensors or Ultrasonic sensor at the same time. The if statement allows for these variables to be reset once they are used. Later in the code there use will be shown.

Now we must program what to do if the IR sensors detect the black line on the sumo ring. This code uses all the IR sensor from pins 11 through pins 18, except  14 and 15. This is just in case one of the IR sensor fails to detect the line.

So type

<!– Crayon Syntax Highlighter v_2 acheter du vrai viagra.7.2_beta –>

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 left side of the Inventor board on the bottom. This means the black ring has absorbed the Infrared light and not bounced it off for the sensor to detect. Thereby the pin stays
LOW when no IR light is reflected.

Now lets code what should happen if the sensor detect 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
BackTime  and
TurnTime  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
HIGH  and as well checks if the
currentDistance  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
void  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