In the previous chapter, we have taught Max:Bot how to use its “eyes” (the digital crash sensors and the light sensor) to avoid obstacles and sense the brightness of light. However, have you ever noticed that there are another pair of real eyes on his face. What can they do?
They are eyes with superpower. But you have to promise me to keep it as our secret. This pair of eyes are the secret weapon of Max:Bot . Let’s learn them together.
1. How the ultrasonic sensor works?
2. How to add package?
3. Diagram aided programming
What may surprise you again is that Max:Bot is more a reliable tool than a good companion. Once we have Max:Bot , all other measuring tools, like rulers, tapes, etc., can be thrown away. In short, Max:Bot can measure distance with ease.
As we may know, when vibrating, objects produce sound waves. Some of them can be heard by our ears, while the others cannot. Scientists named the vibrating times per second as the sound frequency with its unit named as Hertz. Almost every human is able to hear the sound frequencies ranging from 20 to 20000 Hertz.
However, most of us is not able to hear those sound frequencies that are higher than 20000 Hertz or lower that 20 Hertz. Therefore, for those sound frequencies that are higher than 20000 Hertz, scientists have another name for them, it is the “ultrasonic”.
How can the ultrasonic sensor measure distance?
As we have learnt above that the ultrasonic sensor can, through the air, send out ultrasonic through the transmitting end. Once encountering obstacles, the sent out ultrasonic will be bounced back immediately and received by the receiving end. Therefore, the ultrasonic sensor can calculate the distance through the formula: S=340t/2. Among which, “S” is the distance; “340” means ultrasonic travels at 340 meters per second in air; “t” is the total amount of time; “/2” means the time we need for the calculation is the time the ultrasonic travels to the obstacle. But the “t” (the total amount of time) is the time both for transmitting and receiving, so the “t” need to divide 2.
You may now have been convinced that Max:Bot is a professional and powerful tool. He can even calculate the distance within one second! Let’s try it!
（1） Star a new program and name it as “ultrasonic measures distance”
（2） Add package
The function area has listed some basic functions. However, for requirements of special functions, we have to use the “Add package”. We need to add the “sonar” function in this part.
Click “Add package” and search “sonar”. Then click the package named “sonar” and it will be listed in the function area automatically.
（3） We need first to place the “show number” function from “Basic” into the “forever” loop. Then put the “ping unit” function from “Sonar” in the “show number” function.
The image above is from the ultrasonic sensor. Among which, the “COMP/Trig” represents the transmitting end and should be connect to P1, and “ECHO” the receiving end to P2. While programming, in the “ping unit” function, we should change the Pin of “trig” to P1, and the “echo” to P2.
（4） By putting all the functions listed above together, we will have the final program as below. Download it to Max:Bot and the calculated distance between Max:Bot and the obstacle will be displayed.
💡 Please note: Keep in mind to swich Max:Bot on. To make the calculation more acurate, it is better to maintain the distance between Max:Bot and the obstacle within 5cm to 300cm.
Above we have learnt how to use Max:Bot to measure a distance. Do you have any idea about how to use it measure your height? Just try it!
Whether you believe or not, traffic accidences are, nowadays, taking place almost anywhere at any time. Cars make our life more convenient, but at the same time, put us in danger. How can we help drivers to be more sensitive about those potential dangerous? Max:Bot does the job.
（1） Start a new program and name it as “car safeguard”
（2） Item How to use the variable “item”?
Sensory system of Max:Bot carry real-time detections to the changing surroundings. For instance, brightness may vary from light to light.
Which means Max:Bot requires a special function to sense and transform the variable brightness into a certain value. Thus we have the “set item to” function. Among which, the “item” in the middle is the detected variable. The whole function means: set the value of the variable named “item” to.
Find the “set item to” function from “Variables”, and place it into the “forever” loop.
In this part, the data that has been detected in real time is the distance that the ultrasonic travels. Therefore, we need to set the value of the variable according to the detected distance. We have learnt how to use the “ping unit” function to calculate the distance, so, in this part, the “ping unit” function should be placed in the “set item to” function.
Since the value of the variable named “item” has been set, we can now use the variable “item” directly in later parts. You can find the variable “item” from the “Variables” in the function area.
（3） Once the distance between the obstacle ahead and Max:Bot is shorter than the preset value, Max:Bot will then stop automatically to avoid rear-end collision.
It sounds like a very good idea, but how to achieve this? We can use the diagram below as a guidance.
（4） Combine all the function blocks listed above together, the final program goes as below. Download it to Max:Bot . Then you can enjoy a nice and safe road trip!
As you may have noticed that there are two “forever” loops. It is because that we need Max:Bot to operate two programs simultaneously without influence each other. On the one hand, it should detect the value of the variable, and, on the other hand, it itself should determine when to stop and when to go forward. So we need to use two “forever” loops in this part. You can find the “forever” loop from the “Basic” in the function area.
When driving, the car will not stop completely but to gradually slow down. When the distance between it and the car ahead becomes closer and closer, its speed will be reduced correspondingly.
Let’s try it!
💡 Tips: the “and” function can be used to judge different distances.