Lesson 14: Retrieving Text Input from Smartphone

 

Written by Jonathan Sim

You can find this lesson and more in the Arduino IDE (File -> Examples -> Andee). If you are unable to find them, you will need to install the Andee Library for Arduino IDE.

Works on iOS and Android
In this lesson, I'll show you how to create a text input button to retrieve user-input text from their smartphones!

Lesson14.png


When the text input button is pressed, the Andee app will display a keyboard to the user requesting input. As a developer, you have the option of choosing what kind of keyboard you'd like to show.

Here are keyboard layouts you can choose:

  • ALPHA_NUMERIC - Alpha-numeric keyboard.
  • ALPHA_NUMERIC_PW - Alpha-numeric keyboard, input as password.
  • NUMERIC - Numeric keyboard.
  • NUMERIC_PW - Numeric keyboard, input as password.


Contents

Top of the Code

#include <SPI.h>
#include <Andee.h>

// We'll create a text input button and a display box to display
// the user input
AndeeHelper displaybox;
AndeeHelper textInputButton;
char userInput[32];

setup()

void setup()
{
  Andee.begin();  // Setup communication between Annikken Andee and Arduino
  Andee.clear();  // Clear the screen of any previous displays
  setInitialData(); // Define object types and their appearance
}

setInitialData()

void setInitialData()
{
  displaybox.setId(0);
  displaybox.setType(DATA_OUT);
  displaybox.setLocation(0,0,FULL);
  displaybox.setTitle("Here's what you wrote");
  
  textInputButton.setId(1);
  textInputButton.setType(KEYBOARD_IN); // Sets object as a text input button
  textInputButton.setLocation(1,0,FULL);
  textInputButton.setTitle("Type something!");
  textInputButton.setKeyboardType(ALPHA_NUMERIC); 
  // Sets the type of keyboard to show to the user
}

loop()

void loop()
{
  if( textInputButton.isPressed() )
  {
    memset(userInput, 0x00, 32); // Empty the contents of the string before receiving user input
    textInputButton.ack(); // Acknowledge button press or else phone will be left waiting
    textInputButton.getKeyboardMessage(userInput); // Display keyboard and store input into userInput
    displaybox.setData(userInput); // Display user input on the display box
  }
  
  displaybox.update(); // Always remember to update so that new content will be displayed
  textInputButton.update();
  
  delay(500); // Always leave a short delay for Bluetooth communication
}
comments powered by Disqus