Make an “on/off” button using the Mindstorms touch sensor

This is a relatively simple program that turns a Mindstorms touch sensor into an on/off or start/stop button. I wrote this for a rudimentary robot that includes one motor connected to the EV3’s Port A. The touch sensor is connected to Port 1. Motors are connected to lettered ports and sensors are connected to numbered ports. Writing the program took several steps.

First, I wrote a program that simply turns on the motor.

Screen Shot 2017-09-10 at 1.07.24 PM

This program includes two programming “blocks”. The start block, which is necessary to run any program, and the large motor block. In the large motor block, I indicated Port A, because that’s the port that my motor is plugged into. The instruction to turn on the motor is sent to whatever port is indicated in the programming block.

Next, I inserted a wait block before the large motor block.

Screen Shot 2017-09-10 at 1.21.15 PM

In the wait block, I specified Port 1, selected the touch sensor (underneath the icon for the hourglass), selected “compare – state”, and then selected “2”. State “2” means that the touch sensor has been “bumped”. (“0” would mean that the touch sensor has been released, and “1” would mean that the touch sensor is being pressed and held.)

If I ran the above program, “bumping” the touch sensor would cause the motor to start revolving. However, bumping the touch sensor again would not stop the motor.

Next, at the end of the existing program, I added another wait block, and another large motor block. I configured the second wait block just like the first wait block. The large motor block, at the end of the program, is configured to turn the motor off.

Screen Shot 2017-09-10 at 1.23.44 PM.png

When I run this program, bumping the touch sensor will cause the motor to rotate (to turn on); the next time the touch sensor is bumped — whether immediately after, or twenty minutes later — the large motor will stop.

Incidentally, when the large motor block is configured to turn off the motor, you have the option to tell the motor to brake, or to let it coast to a stop.

Screen Shot 2017-09-10 at 1.23.27 PM.png

I chose to let it coast to a stop.

Now, my program allows the touch sensor to first turn on the motor, and then turn it off. However, once it turns the motor off, the motor won’t turn on again unless I re-start the program.

In order to turn the motor on again by simply bumping the touch sensor, I put all of the blocks in my program — except for the Start block — inside a “loop”. The loop is a very powerful programming tool. Anything inside of it can repeat either forever or until some specified event happens. In this case, I’m going to put my program inside a forever loop.

Screen Shot 2017-09-10 at 1.30.47 PM.png

Notice that at the end of the loop, there is a symbol for infinity (nearby the word “unlimited” appeared when I hovered over the symbol with my cursor).

This program has turned the touch sensor into an on/off button. After I download it into my EV3, I will need to start the program running before the “on/off” button works.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s