Lesson 15: Using the Text Input Button to Control LCD Display

 

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 you can use the keyboard text input to change the display of a 16x2 LCD Display.

2013-10-04-17.37.06-1024x483.jpg


Contents

The Wiring

Click to view larger image


Pin 2 is connected to the RS pin of the LCD display.

Pin 3 is connected to the Enable (E) pin

While pins 4, 5, 6, 7 are connected to DB4, DB5, DB6, DB7 of the LCD display (for data transmission).

Top of the Code

#include <SPI.h>
#include <Andee.h>
#include <LiquidCrystal.h> // You'll need this for the display to work

// We'll need two textInput buttons to control each row of our
// 16x2 LCD Display. We'll also have two display boxes for feedback
AndeeHelper displayboxA;
AndeeHelper displayboxB;
AndeeHelper textInputButtonA;
AndeeHelper textInputButtonB;
char userInputA[17]; // Always declare (required characters + 1)
char userInputB[17];

/* Initialise LCD Display. Here's the pin connection that we're using:
   LCD RS pin to digital pin 2
   LCD Enable pin to digital pin 3
   LCD D4 pin to digital pin 4
   LCD D5 pin to digital pin 5
   LCD D6 pin to digital pin 6
   LCD D7 pin to digital pin 7 */
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);

setup()

void setup()
{
  Andee.begin();  // Setup communication between Annikken Andee and Arduino
  Andee.clear();  // Clear the screen of any previous displays
  
  // Tell Arduino how many rows and characters are available in the LCD Display
  lcd.begin(16, 2); // 16 characters by 2 rows
  lcd.clear(); // Clear screen
  
  setInitialData(); // Define object types and their appearance
}

setInitialData()

void setInitialData()
{
  displayboxA.setId(0);
  displayboxA.setType(DATA_OUT);
  displayboxA.setLocation(0,0,FULL);
  displayboxA.setTitle("LCD Line 1");
  
  displayboxB.setId(1);
  displayboxB.setType(DATA_OUT);
  displayboxB.setLocation(1,0,FULL);
  displayboxB.setTitle("LCD Line 2");  
  
  textInputButtonA.setId(2);
  textInputButtonA.setType(KEYBOARD_IN); // Sets object as a text input button
  textInputButtonA.setLocation(2,0,FULL);
  textInputButtonA.setTitle("Change Line 1");
  textInputButtonA.setKeyboardType(ALPHA_NUMERIC); 
  textInputButtonA.setColor(THEME_DEFAULT_DARK);

  textInputButtonB.setId(3);
  textInputButtonB.setType(KEYBOARD_IN); // Sets object as a text input button
  textInputButtonB.setLocation(3,0,FULL);
  textInputButtonB.setTitle("Change Line 2");
  textInputButtonB.setKeyboardType(ALPHA_NUMERIC); 
  textInputButtonB.setColor(THEME_DEFAULT_DARK);
}

Here's how your user interface will look:

Lesson15.png


loop()

void loop()
{
  if( textInputButtonA.isPressed() )
  {
    memset(userInputA, 0x00, 17); // Empty the contents of the string before receiving user input
    textInputButtonA.ack(); // Acknowledge button press or else phone will be left waiting
    textInputButtonA.getKeyboardMessage(userInputA); // Display keyboard and store input into userInput
    displayboxA.setData(userInputA); // Display user input on the display box
    
    // We'll need to clear the LCD display to get rid of stray characters from the
    // previous display
    lcd.clear(); // Clear screen
    // Print first row
    lcd.setCursor(0,0); // Go to left-most character, top row
    lcd.print(userInputA);
    // Print second row
    lcd.setCursor(0,1); // Go to left-most character, second row
    lcd.print(userInputB); 
  }
  
  if( textInputButtonB.isPressed() )
  {
    memset(userInputB, 0x00, 17); // Empty the contents of the string before receiving user input
    textInputButtonB.ack(); // Acknowledge button press or else phone will be left waiting
    textInputButtonB.getKeyboardMessage(userInputB); // Display keyboard and store input into userInput
    displayboxB.setData(userInputB); // Display user input on the display box
    
    lcd.clear(); // Clear screen
    lcd.setCursor(0,0); // Go to left-most character, top row
    lcd.print(userInputA);
    lcd.setCursor(0,1); // Go to left-most character, second row
    lcd.print(userInputB);
  }  
  
  displayboxA.update(); // Always remember to update so that new content will be displayed
  displayboxB.update();
  textInputButtonA.update();
  textInputButtonB.update();
  
  delay(500); // Always leave a short delay for Bluetooth communication
}
comments powered by Disqus