Week 4 with the mBot Dealers Choice "Afraid but not too afraid of objects"

In the ultrasonic sensor walk through there was a challenge question that piqued my interest. 

The challenge was:

"Can you write a program that follows an object? So if the object is too close, the robot goes backwards, if the object is far away, it goes forward, and if the object is not near or far, the robot stops.

Can you change your program to make the robot move at different speeds?"

At first I thought this would be simple.. just have to set some limits and it'll work.. right?

I spent about 2 hours trying to get the below working...many times it would just sit there and do nothing making noises like it was confused. During other test runs it would go forward till the object was close, then move backwards if you moved it closer, but it would NEVER stop a certain distance away. Getting it to stop when it was not too close and not too far was the challenging part. During this process I tried various AND blocks and OR blocks..but they just didn't do what I wanted.

None of this worked:

Click here for full resolution photo

So after a couple hours and it being 1045 at night, I gave up and started clicking around randomly, which led me to the Data&Blocks section. I noticed "make a variable" and then wondered if the problem with my other code is that it didn't allow for things to change, everything was coded in hard values. With this info I went off to google and searched "variables in mBlock" and with that a helpful guide was right there showing everyone what to do with variables. 

After reading/watching up on that, the code below achieves the desired results, which took about 20 minutes to get figured out. I'd imagine Kay and Papert would be happy to find out about what happened that night! 

Code to answer the first challenge above, in this video it always moves forward and backward at the same speed:

Click here for video:

The last question was can you do the above but have it move at random speeds...coming right up!!


Click here for video

    • Sachin Rochlani
      Sachin Rochlani

      Hi Jeff. I enjoyed reading this post. When learning how to control the mBot (or anything new), it's important for students to understand that it takes time and patience. You said it took you a couple hours to figure this out. At first, I was becoming frustrated because it was taking me too long to make my mBot do what I wanted it to do. Initially, I thought something like this would not be good for my students (elementary school) because they would become frustrated. But after reading your post, I think it's a valuable lesson for them. Sometimes when we learn something new it takes a lot of time.

      • Susan Granata
        Susan Granata

        Jeff - I wish you had posted this before I finished my maze - it would have helped:)

        Good perseverance - I totally understand the hours of wasted clicking!

      Computer Science for Teachers Spring 2018

      Computer Science for Teachers Spring 2018

      Here is the online home for CS for Teachers.

      Latest comments