Programming the Ultrasonic sensor


#1

I’m trying to get the ultrasonic sensor to work, but I don’t know where to put the block for it or what to do with it at all. I only have the most basic things down for programming the mBot, I can get it to move around and such. If anyone has any information it would be extremely helpful.


#2

Which robot are you using? mBlock has a block for Ultrasonic Sensor on [Port 3] distance that will return the distance in centimeters. You’d need to create a variable to capture the value or display the value by dropping it into a say block if you want to see it. You can also use the value for mathematical or logical operations as well.

Hope this helps.


#3

I wanted to use this to find out what the sensor was seeing as I wanted to see why my “beep when you get close to the sensor” code wasn’t working but when it says “say” where does the “readout” of the values actually appear?


#4

Add a delay after the ‘say’ to see the message. It should appear in a ballon over the panda.


#5

I have much better success with the Orion Program code start block than I do with the start flag. I’d write it like this:

Does that help?


#6

Yes, I have better luck uploading a program than running the sprite as well.


#7

I’ve had the same issue when running through the mBlock environment. The serial lag time is enough to throw off accurate readings. :slight_smile:


#8

Hi Guys,

Am running to problem and I just can’t figure it out.

My Ultrasonic sensor is not working. However, it is connected on port 3. Any suggestions?


#9

Can you post the code you are using to test the ultrasonic sensor?


#10

Hi Chuckmcknight!

I figured it out. I needed to reboot my computer so it could work -.- sorry!


#11

Hi,

Actually, I need your help. The ultra-sensor was working for a few but now it is not working. I have connected the cable on port 3. If you can help me, I will be so thankful!


#12

I’ve found that when I have sensors acting up it is usually time to go through the Upgrade Firmware + Reset Default Program cycle to clear out anything that might be hanging around in memory. After you do that, try running your program again.


#13

how did you get set distance?


#14

from control? its not there


#15

this happens


#16

Hi Ahmet,

I noticed from your screen shot that the robot does not appear to be connected to mBlock.

You can only use the Scratch block ‘say’ when you are either tethered via USB cable or if you are connected via WiFi or Bluetooth. You have to start the program with a when Clicked hat block from the Events palette, not an mBot program hat block from the Robots palette.

Also note that the shape of the ultrasonic sensor block indicates that it is a ‘reporter’ block which will return the current value of the ultrasonic sensor. You can create a variable, or if you use the when Clicked block as mentioned above, the say block from the Looks menu.

The error dialog you are getting indicates that the Orion hat block (which indicates that the program needs to be compiled to Arduino code and uploaded to the robot) does not support the ‘say’ block. This should be expected because the robot does not have a screen.

Keep in mind that an Arduino system is not a computer, but rather is a microcontroller-based system. When you are tethered via cable (or WiFi or Bluetooth), the program operates on your computer and special commands are sent to the robot to set or get data which can then be displayed on the computer screen.


#17

so how do i program the ultrosonic sensor?


#18

If you are ‘tethered’ to the computer with the USB cable or via WiFi or Bluetooth, you can use the ultrasonic reporter like this:

This program will report the reading from the ultrasonic sensor on the Scratch stage.

If you are writing an untethered program that runs by itself, you would do something like this:

This program will make the robot move forward until it gets within 5cm of an obstacle.


#19

Hello,i can upload this progam but my robot does nothing.(Robot starter kit,MeOrion)


#20

Maybe try < 10 instead of <2.5 … does it work then?