Remote control a catapult with an App build with App Inventor

In this previous article we learned how to build an App with App Inventor and connect it to a Lego Mindstorms NXT. Now we will extend that App to control a Catapult robot. If you have not done so already, first build the Catapult.

Now that you are able to connect your phone to the NXT, it is time to make the firing system work. After the following steps, you will be able to fire your Catapult from a distance with just one push of a button!

Step 1 – Edit the user interface

catapult_app_uiIn order to fire the Catapult, you will need a firing button. Switch back to the Designer. Drag a Button to the Viewer. Label it “Fire” and rename it to “Fire”. You can change the font size, as well as the width and height of the button in the Properties window if you like. For this example, we changed the FontSize to 20, the Height to 80 pixels and set Width to “Fill parent”, this means the button will span the full width of the screen.
The firing mechanism is operated by a motor. Motors are controlled by the “NxtDrive” block, drag it onto the viewer like you did with the “NxtDirectCommands” block. In the Properties Window, set the BluetoothClient to BluetoothClient1. Set DriveMotors to C (as the motor operating the firing mechanism should be connected to port C on your NXT brick), leave StopBeforeDisconnect on and set WheelDiameter to 10.

Step 2 – Program the firing mechanism

catapult_app_block2All that has to be done now, is to let the motor move when the fire button is pressed. Switch back to Blocks. You may have noticed that Fire and NxtDrive1 have been added to the list of blocks. Select “Fire” and click on the block “When Fire.Click”. This block will work as an event-handler for when the firing button is pressed.
Under Blocks, select NxtDrive1 and find the block “call NxtDrive1.MoveForward”. This block will power the engine in the forward direction. Add two value blocks, one for power and one for distance. Set the power to 60 and the distance to 10.

Step 3 – Fire!

Connect your phone to the NXT brick again, and check whether the firing mechanism works.

Contributed by Yanick Brezet.