How to Program the Motion Capture Bot

This lesson will go over how to program the Motion Capture Bot

What it Does

This robot is designed to play a game. Once it senses an object in front of it, it will wait a random amount of time before it then quickly closes. The goal is to not have your hand caught in it.

Objectives

Program the controller in such a way that:

  1. Waits for an object to be sensed
  2. Then waits for a random amount of time
  3. Then quickly close

For this robot we need it close its hands when it see something in front of. We will get started by doing the setup code. We need to include the SmartInventor library up at the top of our code. Since we will be using a motor we also need to set it up in void setup().

Then we will start by finding the base value that our center sensor is reading so that we can know if it is seeing a non-normal value. We will read the value of the sensor in void setup() and save it to a variable.
Now we can read the sensor and check to see if there is something close to it.
Then if it is we can close the hands, but to make it more fun we are going to toss in a little bit of randomness. We will make it wait for a random amount of time before it closes the hands. To make this happen will add one to a variable named handTime each time through the loop that the something is in front of the sensor. Then if it is greater than minimumTime we can close the hand and set handTime back to 0. MinimumTime will be set to a random number between 50 and 255 each time through the loop. This way we will not be able to know how soon it will close, but have to wait for handTime to be larger than a random number between 50 and 255.
Now we can add in the closing part. Inside of the if(handTime > minimumTime) if statement we will add in the close the hands code.
Next to give you a little bit of a warning we will add in some buzzers to notify you that it is about to close its hands.

Final Code

Then if you want more of a challenge you could remove the Buzzer or speed up the close.