mBot + Servo + Ultrasonic sensor


#1

MakeBlock,

I would be interested to see sample code that uses the Ultrasonic sensor mounted on a servo - as shown in the GIF below…

This is the code I have used on my robot

The problem I experience is that the output varies, sometimes…

a) I got readings left and right but not center.

b) I get zero left, center and right.

c) I got the same reading regardless of what was in front i.e. variable value did not change.

In an attempt to fix the zero output I forced the program to loop until it got a value greater than zero from the ultrasonic sensor (making sure to reset the variable first).

This did not work for me either.

So I would be very interested in seeing a MakeBlock example.

Regards
Michael


#2

To be honest, I’ve not had much luck with the Ultrasonic sensor. I’m using it to detect walls for my maze solver, but that’s about it. I originally wanted to use it to control distance, but as you note, it’s got issues. :frowning:


#3

I have been using a servo to drive the IR motion sensor in the fashion shown. (Hunt/Seek sensor)
I won’t get into all the details, but I found that driving the sensor to a stationary position to do the read is best.

Also the short wait after the sensor read, as you have there, is good for serial lag and sensor reset (the IR motion has about a 1 sec. reset time)

This is for the Orion board, you might have to change to mBot commands, but the flow should be the same.

Upload the file with Scratch, as IDE board selection is tricky.

**Edit: Clear buffer command missing after second serial write

You want to use the Arduino IDE serial monitor to see the readout (Scratch’s always is in HEX at screen bottom)

Disconnect the serial port in Scratch to use the IDE serial monitor, and set the baud to 115000.

Below is a series of readings. The delay time is too short, and the sensor bounces at 0 degrees and reads zero or about 356 unless the object is very close. The farther the distance, the longer the sample time required.


What next with mBot Kit?
#4

It’s not perfect, here’s my solution.


#5