Ultrasonic sensor and Motion sensor working together


#1

I have added the compass, motion sensor and ultrasonic sensor to my Rover. It is my first robot and has an Orion board. I am having trouble programing them in mBlock to all work together to operate my robot. Any help?


#2

Start simple and add sensors one by one.

Read this page, especially the guide pdf:
http://learn.makeblock.com/en/getting-started-programming-with-mblock/


#3

My son has already created an obstacle avoiding robot using the ultrasonic sensor but is having trouble with messing the other sensors. The “if and then” controls as they get more detailed and relate to more than one sensor.


#4

Can you post a screenshot of the code? Tough to help out when we can’t see what he’s doing. :slight_smile:


#5

Here you go!


#6

One issue that I see is that you are using two if statements. Note that you are generating a random number back to back so depending on what occurs there you are making two movements, not one. Try putting the turn statements into an if-else block first, i.e.,:

if (pick random (1) to (2) = (1)) then
    turnRight
else
    turnLeft

and see if that helps straighten things out.


#7

Yep I see that and will change that. I works great though, the question he has and me as well is how to add an additional sensor(s) mainly the compass and motion sensors working together with the ultrasonic. I think we are getting lost in the “if” statements as you say. Thanks for your help you guys have a great company and we are very impressed.


#8

You did not even get in touch with Makeblock employees here :slight_smile: All volunteers


#9

Well thanks any way. I still need help messing the sensors together in little more detail sorry for the ignorance but I am a newbie after all. Trying to get my son out of the video game world and into the real one.


#10

The trick with multiple sensors is to realize that the microcontroller doesn’t multitask so you’ll have to stack the statements reading the sensors into a sequence. Just like in any other program, you’ll need to build custom blocks for each sensor subsystem and call those blocks from within your main program loop, i.e.,:

loop ()
do ultrasonic sensor processing
do motion detection processing
do compass processing

Use variables to store values so that the blocks can interact with each other.

I wrote an eBook that is geared towards the STEM version of the mBot that you may find helpful. While not all of the programs will work with the Orion or Auriga boards as they are written, they do discuss the basic principles of operation and how to program using mBlock. The last program is a simple maze solver that uses the ultrasonic sensor and the line follower to navigate a maze.