Bluetooth-Display

In today's first part of the multi-part blog, we build a freely configurable Bluetooth LCD display. Via a serial Bluetooth profile, we provide the Bluetooth client with an ASCII menu, via which the LCD display is completely controllable. Furthermore, we will expand the menu across the blog parts and install new (comfort) functions. In the first part of the series, we build the selection menu, and implement a direct serial to display input that outputs the typed letters character by character directly visible to our display. We use the control character "Enter" to change the display lines (1-4) and to complete the input sequence, as well as the control character "Backspace" for deleting previous incorrect entries. We will call the function "Direct Print" in the menu. To establish the Bluetooth connection, a Bluetooth adapter on the PC can be used in conjunction with a terminal program such as "Putty", as well as a compatible mobile phone APP that supports a serial Bluetooth profile. To transfer the serial interface to the Bluetooth standard, we use the well-known Bluetooth module HC 05.

We take a look at the parts list of our project:

1x Bluetooth HC-05 Module

1x HD44780 LCD Display 4x20

1x Trimmer Potentiometer Max Resistance 10k.1

1x 130" Resistance Tolerance -5%

1x Uno R3 Board Type UNO R3 ATmega328P

Not many parts are needed for this project, and the connection of the individual components is also limited.

We upload the following code to our Arduino UNO:

 

#include <SoftwareSerial.H>
#include <LiquidCrystal.H>

#define MaxInputBufferSize 20 maximum 255 characters to adapt to vlcdr
#define rLcdChr 20
#define LcdRows 4
#define interval 1000

SoftwareSerial mySerial(7, 6); RX, TX
LiquidCrystal Lcd(8, 13, 12, 11, 10, 9);

variables

Serial Input Handling
Char TBuffer;
Char Cbuffer[MaxInputBufferSize+1];       USB Code Input Buffer
String Sbuffer = "";                      USB String Input Buffer
Int Value;                                USB Nummeric Input Buffer
Int  MnuState = 0;
Byte Ccount = 0;                          Number received Chars
Byte Inptype = 0;
Boolean StrInput = False;
Boolean NumberInput = False;
Boolean DataInput = False;
Boolean EnterInput = False;
Byte MenueSelection = 0;
Give Debug Information over serial Interface
Boolean DebugMode = False;
Boolean EchoMode = True;
Display Management
Boolean Directprint = False;
Byte DirectprintROW = 0;
Byte DirectprintLine = 0;

Boolean RefreshDisplay = False;

Void Setup()    {    Lcd.Begin(rLcdChr, LcdRows);   Lcd.Clear();   Lcd.setCursor(0, 0);   Lcd.Print(" Bluetooth ");   Lcd.setCursor(0, 1);   Lcd.Print(" Display ");   mySerial.Begin(9600);   Lcd.setCursor(0, 0);   mySerial.Flush();
}

Void SerialcommandProcessor()
{
Int A;
Inptype = 0;
Inptype = SerInputHandler();
0 no return 
1 number 
2 String 
3 Data
If ((Inptype > 0) & (!Directprint))               {    MenueSelection = 0;    If ((MnuState < 2) && (Inptype == 2)) {Sbuffer.Touppercase(); } For Easy Entering Commands      If ((Sbuffer == "D") && (MnuState == 0) && (Inptype == 2))       { MenueSelection = 15;}    Switch (MenueSelection)      {        Case 15:        {        Direct pPrint to Display        Directprint = True;        mySerial.println ("Directprint ON.");        If (Directprint)        {        DirectprintROW = 0;        DirectprintLine = 0;        Lcd.Clear();        Lcd.Cursor();        Lcd.Blink();        }        Value = 0;             Sbuffer = "";        MnuState = 0;        Break;         }        Default:        {          mySerial.println("-------Smart Bluetooth Display 1.0------");          mySerial.println("D - Direct Print");          mySerial.println("----------------------------------------");          mySerial.println("Type Cmd and press Enter");          mySerial.Flush();          MnuState = 0;          Value = 0;          Sbuffer = "";        }           }      } Input detected
Input buffer
}

Boolean CheckforserialEvent()
{   while (mySerial.available()) {      Get the new byte:      TBuffer = mySerial.Read();       If (TBuffer > 9 && TBuffer < 14)        {         Cbuffer[Ccount] = 0;         TBuffer =0;         if (EchoMode)           {            mySerial.print(char(13));            mySerial.flush();          }            if (Directprint)           {           mySerial.println("");           DirectprintLine = 0;           DirectprintROW = DirectprintROW + 1;           if ( DirectprintROW > 3)            {                     Directprint = false;                    lcd.noCursor();                    lcd.noBlink();                    Sbuffer = "";                    value = 0;                                 } else            {           lcd.cursor();           lcd.blink();           lcd.setCursor(0,DirectprintROW);           }           }          EnterInput = true;           return true;      } else if (TBuffer > 47 && TBuffer <58 )       {        if ( Ccount < MaxInputBufferSize)           {             Cbuffer[Ccount] = TBuffer;               Ccount++;             if ((Directprint))               {                  lcd.print(char(TBuffer));                  DirectprintLine = DirectprintLine + 1;                  if ( Ccount > MaxInputBufferSize -1)                   {                    lcd.noCursor();                    lcd.noBlink();                    } else {                    lcd.cursor();                    lcd.blink();                    }                                 }                      if (EchoMode) {              mySerial.print(char(TBuffer));              mySerial.flush();             }            } else {mySerial.print("#"); }            //Number Input detected       NumberInput = true;       }        else if (TBuffer > 64 && TBuffer < 123 )       {        if ( Ccount < MaxInputBufferSize)           {             Cbuffer[Ccount] = TBuffer;               Ccount++;             if ((Directprint))               {                  lcd.print(char(TBuffer));                                 DirectprintLine = DirectprintLine + 1;                  if ( Ccount > MaxInputBufferSize -1)                   {                    lcd.noCursor();                    lcd.noBlink();                    } else {                    lcd.cursor();                    lcd.blink();                    }                       }                if (EchoMode) {              mySerial.print(char(TBuffer));              mySerial.flush();             }            } else {mySerial.print("#"); }       //Character Char Input detected       StrInput = true;      }     else if ( (TBuffer == 127 )  |  (TBuffer == 8 ) )          {                       if ( DirectprintLine > 0 )                  {                  DirectprintLine = DirectprintLine - 1;                  lcd.setCursor(DirectprintLine, DirectprintROW);                   lcd.print(" ");                  lcd.setCursor(DirectprintLine, DirectprintROW);                   }                                                       if (( DirectprintLine == 0 ) & ( DirectprintROW > 0 ))                  {                  DirectprintROW = DirectprintROW - 1;                   DirectprintLine = rLcdChr -1;                  lcd.setCursor(DirectprintLine, DirectprintROW);                   }          if ( Ccount > 0)           {             Ccount--;             Cbuffer[Ccount] = 0;             if ((Directprint))               {                  if ( Ccount > MaxInputBufferSize -1)                   {                    lcd.noCursor();                    lcd.noBlink();                    } else {                    lcd.cursor();                    lcd.blink();                    }                       }                      if (EchoMode) {              mySerial.print("-");              mySerial.flush();             }            }        }          else         {                      if ( Ccount < MaxInputBufferSize)           {             Cbuffer[Ccount] = TBuffer;               Ccount++;              if ((Directprint))               {                  DirectprintLine = DirectprintLine + 1;                  if (TBuffer < 128) {lcd.print(char(TBuffer)); } else  {lcd.print(String(TBuffer)); }                  if ( Ccount > MaxInputBufferSize -1)                   {                    lcd.noCursor();                    lcd.noBlink();                    } else {                    lcd.cursor();                    lcd.blink();                    }                       }                if (EchoMode) {              mySerial.print(char(TBuffer));              mySerial.flush();             }            } else {mySerial.print("#"); }       //Data Input detected       DataInput = true;       }      return false;      }     return false;   }    void ClearCBuffer ()

{
 for (byte a= 0; MaxInputBufferSize -1;a++)   Cbuffer[a] = 0;
}    byte SerInputHandler()
{
byte result = 0;
int c;
int d;
int a;
int b;
result = 0;
if (CheckforserialEvent())     {       if ((NumberInput) and not (DataInput)and not (StrInput))     //Numbers only        {        Sbuffer = "";        value = 0;        StrInput = false;        NumberInput = false;        DataInput = false;        EnterInput = false;         a = 0;        b = 0;        c = 0;        d = 0;        Sbuffer = Cbuffer; // Zahl wird AUCH ! in SBUFFER �bernommen, falls ben�tigt.        if (Ccount == 1) { value  = Cbuffer[0]- 48 ; }        if (Ccount == 2) {           a = Cbuffer[0] - 48 ;           a = a * 10;          b = Cbuffer[1] - 48 ;          value = a + b;          }        if (Ccount == 3) {           a = Cbuffer[0] - 48 ;           a = a * 100;          b = Cbuffer[1] - 48 ;          b = b * 10;          c = Cbuffer[2] - 48 ;          value = a + b + c;          }        if (Ccount == 4) {           a = Cbuffer[0] - 48 ;           a = a * 1000;          b = Cbuffer[1] - 48 ;          b = b * 100;          c = Cbuffer[2] - 48 ;          c = c * 10;          d = Cbuffer[3] - 48 ;          value = a + b + c + d;          }        if (Ccount >= 5)            {            Sbuffer = "";            value = 0;            Sbuffer = Cbuffer;            ClearCBuffer;            result = 2;             } else           {            ClearCBuffer;            Ccount = 0;            result = 1;                                                //Number Returncode            NumberInput = false;            StrInput = false;            DataInput = false;            EnterInput = false;             Ccount = 0;            return result;            }        }      if ((StrInput) and not (DataInput))                          //String Input only        {        Sbuffer = "";        Sbuffer = Cbuffer;        value = 0;        StrInput = false;        NumberInput = false;        DataInput = false;        EnterInput = false;         Ccount = 0;        ClearCBuffer;        result = 2;                                                 //Number Returncode        }       if (DataInput) {        Sbuffer = "";        Sbuffer = Cbuffer;        Value = 0;        StrInput = False;        NumberInput = False;        DataInput = False;        EnterInput = False;         Ccount = 0;        ClearCBuffer;        result = 3;                                               Number Returncode        }        If ((EnterInput) and not (StrInput) and not (NumberInput) and not (DataInput))        {        Sbuffer = "";        Value = 0;        Ccount = 0;        ClearCBuffer;        result = 4;                                               Number Returncode        }             NumberInput = False;    StrInput = False;    DataInput = False;    EnterInput = False;     Ccount = 0;    Return result;     } 
Return result;          End CheckforSerialEvent
}


// ###################################################################################################### //

Void Loop() 

{
SerialcommandProcessor();
}

// ###################################################################################################### //

After uploading the code, we now connect via the Bluetooth module and with a serial Bluetooth profile on our Arduino. We get the following menu after pressing the "Enter" button:

We now type the letter "D" and then press "Enter":

The following is a confirmation that "Directprint" has been turned on. Everything we type now is displayed directly and without delay in both the LCD display and the serial interface. So we "see" in the terminal what we write and can also correct typos with "backspace".

An important note: For this function of the display described in this part, the code may seem quite complex. The pure function could also be realized with simpler code. Also, some parts of the code have already been optimized or prepared for the following parts of the series.

Individual sections of the code may only be explained in the following parts:

 

Now I wish you a lot of fun recreating and until the next time.

DisplaysFor arduinoProjects for beginners

Leave a comment

All comments are moderated before being published

Recommended blog posts

  1. Install ESP32 now from the board manager
  2. Lüftersteuerung Raspberry Pi
  3. Arduino IDE - Programmieren für Einsteiger - Teil 1
  4. ESP32 - das Multitalent
  5. OTA - Over the Air - ESP programming via WLAN