Accept Input from Android Keyboard

This example shows how to accept input values from the Android™ keyboard and convert it to a logical output using Simulink® Support Package for Android™ Devices.

Introduction

In this example, the Simulink® model androidConvertTempExample, represents the equation that converts temperatures in Celsius to Fahrenheit:

F = (C X 9 / 5) + 32

This model has a Data Input block, Celsius, and a Data Display block, Fahrenheit. The Celsius block generates a Celsius field on your Android device screen to accept input from the keyboard. This value is passed to the Multiply block, which performs the operation (C X 9 / 5). The Add block adds the result of the previous calculation and the constant value of 32. This final result is displayed in the Fahrenheit field on your Android device.

Prerequisites

Before you start with the example, we recommend you complete:

Required Hardware

  • Android device

  • USB cable

Step 1: Configure the Data Input Block

1. Connect the Android device to your computer with a USB cable.

2. Install the Simulink Support Package for Android Devices as described in Install Support for Android Devices. If you have already installed the support package, skip this step.

3. Open the androidConvertTempExample model.

4. In the Simulink model, double-click the Celsius block and verify these parameter values in the Block Parameters dialog box.

     Parameter               | Value
  ---------------------------------------
     Character Vector Input  | off
     Initial Value           | 1.0
     Data Type               | double
     Sample Time             | 0.1

5. Click OK.

Step 2: Configure the Simulink Model

1. In the Modeling tab of the toolstrip, select Model Settings.

2. In the Configuration Parameters dialog box, select Hardware Implementation.

3. Set the Hardware board parameter to Android Device. This selection automatically populates the parameters in the Hardware board settings with the default values for Android.

4. From the Groups list under Target hardware resources, select Device options.

5. From the Device list, select your Android device. If your device is not listed, click Refresh.

Note: If your device is not listed even after clicking the Refresh button, ensure that you have enabled the USB debugging option on your device. To enable USB debugging, enter androidhwsetup in the MATLAB® Command Window and follow the onscreen instructions.

6. Click Apply. Click OK to close the dialog box.

Step 3: Deploy the Simulink Model on Android Device

1. On the Simulink editor toolbar, click the Deploy to Hardware button. The model is loaded and the application starts running on your device. The application creates a Celsius field and a Fahrenheit field on your device.

2. Enter a value in the Celsius field and press the tick button on your Android keyboard. The model algorithm converts the value specified in the Celsius field and displays the output in the Fahrenheit field.