Control a Lego aerial tramway with an Android phone app

Android Program

To control our Lego tramway, we wrote an Android app [4] using the free App Inventor, a web-based application that runs on Windows, Mac, and Linux for creating Android apps. (If you have an iOS device, check out the Kino App Inventor [5]; if you have a Windows phone, look at Windows Phone App Studio [6].) To use App Inventor, you need a Google account. For this project, we used the same Google account we use with our Android phone, but you could use an account that you have with Gmail or Google+, too.

In App Inventor, you work in two screens to create a phone app. The first screen is accessed by pressing the Designer button in the upper right corner. In the Designer screen, you will create the layout and the look and feel of the application. For the second screen, click on the Blocks button. In this screen, you will create the logic or program for your app. App Inventor programming is similar to programming in Scratch.

In the Designer screen, you simply select the item you want from the User Interface Palette and drop it into the middle Viewer screen (Figure 7). Once a component is put into the Viewer window, it is given a default name and properties. In the Components window, you can rename a component, and the Properties window allows you to change things like the text, color, and sizing.

Figure 7: App Inventor Designer screen.

For our application, we used three visible components: a ListPicker to set up the Bluetooth connection, a Slider to adjust the motor speed from 0% to 100%, and three buttons, which display Forward, Stop, and Backward.

To make the application more understandable, change the label for your ListPicker1 component by clicking on it and entering Connect in the Text field in the Properties window. Next, in the Components window, rename your buttons BT_Back, BT_Stop, and BT_Forward. Finally, select each button in the component window and change the size, color, and text in its Properties window. For example, on BT_Forward we use BackgroundColor Green, FontSize 50, Width 80%, Height 20%, and Text FORWARD.

A couple of components that you can't see ("Non-visible components" in the Viewer) also are required, including BluetoothClient and NxtDrive. Just like the visible components you drag the non-visible component that you want on to the Viewer window. In the Properties section of the NxtDrive1 component, enter your BluetoothClient1 component name for the BluetoothClient field and CB for the DriveMotors field, which matches our Lego motor setup on port B or C.

In the Blocks screen (Figure 8), you can drag blocks from the left Blocks window to the main Viewer section. For the Gondola app (Figure 9) the following blocks are used:

Figure 8: Move blocks from the Blocks window to the Viewer window when in the App Inventor Blocks screen to create your programs.
Figure 9: App Inventor Blocks screen.

The when(ListPicker1.BeforePicking) do statement is called when the Connect button is pushed. This block shows a list of all the phone's paired Bluetooth devices, which is generated by connecting the blocks set(ListPicker1.Elements) to and BluetoothClient1.AddressesAndNames.

The when(ListPicker1.AfterPicking) do statement is called after the paired NXT device is selected. Inside this block, ListPicker1.Selection is an input to the block call(BluetoothClient1.Connect) address(). An if() block is included for future error checking, but it is not used here.

The when(BT_Forward.Click) do statement is called when the Forward button is clicked in the app. The speed of the motor is set by the position of the Slider (Slider1.ThumbPosition). The direction of the motor is set by the block call(NxtDrive1.MoveForwardIndefinitely).

The when (BT_Back.Click) do statement is called when the Back button is clicked. The direction of the motor is set by the block call(NxtDrive1.MoveBackIndefinitely).

The when(BT_Stop.Click) do is called when the Stop button is clicked. The motor is stopped by the block call(NxtDrive1.Stop).

Running the App

To put your new app onto your Android device, you can either save it as an APK file (the native Android application file format), or you can download your app directly using a QR (Quick Response) code. You select the Build drop-down menu option for both methods (Figure 10).

Figure 10: Build your Android app.

If you save your app as an APK file, you can email it to your friends or move your application to a microSD card. The microSD card can then be moved to your phone. Your Android phone will recognize the APK file, and clicking the file loads the app. If you want to install your app with the QR code method, you'll need to install one of the many free QR code readers [7] on your phone. We found that the QR code method was the fastest and simplest approach.

Buy this article as PDF

Express-Checkout as PDF

Pages: 6

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content