Bluetooth-Display - Teil 4

Welcome to the fourth part of the Bluetooth display series.

 

In today's part our display receives a further small extension of the hardware in addition to a mandatory additional operator menu. With this new hardware extension, the messages stored in the internal memory can be retrieved without the operator menu via a button!

To do this we use another free port of our microcontroller (compatible with Uno R3) as an entrance. More specifically, we use Port 2 as an entrance. At this entrance there is a button connected to mass, whose key pressure is evaluated by our Arduino.

But now for the actual construction and implementation of enlargement. In the first step we expand our hardware as shown in the following schematic to the KY-004 button module. The Bluetooth display has a standard Arduinos 12 text memory with 4x20 characters.

These are displayed ascending from 0 to 11 when pressing the button. When the message 11 is displayed on the display and the button is pressed, the counter jumps back to message 0.

Bluetooth Display - Part 4- Switch Plan

 

Next we upload the following custom code to our microcontroller for this extension:

 

 

'35; includes <SPI.h>
'35; includes <Wire.h>
'35; includes <Software.h>
'35; includes <EEPROM.h>
'35; includes <LiquidCrystal..h>
'35; includes <uc/sleep.h>

\35; define MaxInputBufferSize 20 // adjust maximum 255 characters to vlcdr
\35; define EMPLOYMENT 990
\35; define rLcdChr 20
\35; define LcdRows 4
\35; define interval 1000
\35; define BackgroundLight 5 // Port 5 LED Backlight
\35; define SwitchPin. 2 // Port 12 Keyboard Message Selection
\35; define DelayTOPWROFF. 500

// EEprom Storage Cell Addresses for Configuration
\35; define EEFadeSeconds  993
\35; define EEPINE 996
\35; define EQUIPMENT 997
\35; define END 998


Software mySerial(7, 6); RX, TX
LiquidCrystal. lp(8, 13, 12, 11, 10, 9);

//variables
exchange DisplayBankContent = 0;

//Serial Input Handling
char TBuffer;
char Cbuffer[MaxInputBufferSize + 1];     //USB code input buffer
String Sbuffer = "";                      //USB String Input Buffer
int value;                                //USB Number Input Buffer
exchange Ccount = 0;                          //Number received Chars
exchange Inptype = 0;
boolean StrInput = false;
boolean Number = false;
boolean Data = false;
boolean EnterInput = false;
exchange Menu = 0;

//Press-button
boolean Switchstate. = true;
boolean SwitchstateBuffer = true;
exchange SelectedMsg = 0;

//Give Debug Information about serial interface
boolean EchoMode = true;

//EEPROM
int eeaddress;                 //EEPROM Address Pointer
exchange EEPromBanks = 0;          //Used for Calculating the EEPROM Banks
//SerMnueControl
exchange Multi = 0;            // Maximum menu depth 255 icl Sub
exchange Selection = 0;

//Real Time Clock
long previousMillis = 0;       // will store last time was measured
long previousMillisB = 0;       // will store last time was measured

//Display management
boolean DisplayLock. = false;
boolean Directprint = false;
exchange DirectPrintROW = 0;
exchange DirectPrintLine = 0;

boolean RefreshDisplay = false;
exchange FRM = 0; Used fpr Writing Operations to eeprom so save Wire cycles
// Battery Monitoring
float Volume;
boolean PowersaveMode = false;

// PWM light control

exchange Current = 0;
exchange Target = 0;
exchange FadeSeconds = 0; // Standard = 3


void setup()
{   EEPromBanks = EMPLOYMENT / ((rLcdChr) * LcdRows);   lp.start(rLcdChr, LcdRows);   lp.clear();   lp.setCursor(0, 0);   lp.print("Bluetooth");   lp.setCursor(0, 1);   lp.print("Display");   mySerial.start(9600);   pinMode(SwitchPin., INPUT PULLUP); // Keyboard Selection Text from EEprom   pinMode(BackgroundLight, OUTPUT); // Display lighting / Display ON /AUS   digitalWrite(BackgroundLight, LOW);   // read Config   FadeSeconds = EEPROM.read(EEFadeSeconds);   Current = 0;   Target = 0;   lp.setCursor(0, 4);   if (DisplayLock.) {     lp.print("System locked");   }   Further Setup Routines   lp.setCursor(0, 0);   Target = 255;   mySerial.flush();
}

// ###################################################################################################### //
void run()
{   Serial();   runrealTimeClock();   Displayprocessor();   SwitchProcessor();   //End Main loop
}
// ###################################################################################################### //

void Text(exchange Row)
{   mySerial.Print("Text for Bank" + String( Selection) + "ROW" + String (Row) + ":");
}

void Serial()
{   int a;   Inptype = 0;   Inptype = SerInputHandler();   // 0 no return   // 1 Number   // 2 String   // 3 Date   if ((Inptype > 0) & (!Directprint))   {     Menu = 0;     if ((Multi < 2) && (Inptype == 2)) {       Sbuffer.toUpper();  For Easy Entering Commands     }     if ((Sbuffer == ECHO) && (Multi == 0) && (Inptype == 2))     {       Menu = 2;     }     if ((Sbuffer == "A") && (Multi == 0) && (Inptype == 2))       {       Menu = 3;     }     Erasing ALL EEprom Content     if ((Sbuffer == E) && (Multi == 0) && (Inptype == 2))       {       Menu = 4;     }     if ((Sbuffer == YES) && (Multi == 1) && (Inptype == 2))      {       Menu = 5;     }     if ((Sbuffer != YES) && (Multi == 1) && (Inptype == 2))     {       Menu = 6;     }     //Edit Selected Content     if ((Sbuffer == "W") && (Multi == 0) && (Inptype == 2))       {       Menu = 7;     }     if ((Multi == 2) && (value  < EEPromBanks) && (Inptype == 1)) {       Menu = 8;     }     if (Multi == 3)                                               {       Menu = 9;     }     if (Multi == 4)                                               {       Menu = 10;     }     //Display Selected Content     if ((Sbuffer == P) && (Multi == 0) && (Inptype == 2))       {       Menu = 11;     }     if ((Multi == 5) && (Inptype == 1))                           {       Menu = 12;     }     if ((Sbuffer == "R") && (Multi == 0) && (Inptype == 2))       {       Menu = 13;     }     if ((Multi == 6) && (Inptype == 1))                           {       Menu = 14;     }     if ((Sbuffer == "D") && (Multi == 0) && (Inptype == 2))       {       Menu = 15;     }     if ((Sbuffer == "A") && (Multi == 0) && (Inptype == 2))       {       Menu = 16;     }     if ((Sbuffer == "B") && (Multi == 0) && (Inptype == 2))       {       Menu = 17;     }     if ((Multi == 7) && (Inptype == 1))                           {       Menu = 18;     }     if ((Sbuffer == FADE) && (Multi == 0) && (Inptype == 2))     {       Menu = 19;     }     if (Multi == 9)                                               {       Menu = 20;     }     if (Multi == 10)                                              {       Menu = 21;     }     if (Multi == 12)                                              {       Menu = 25;     }     switch (Menu)     {       case 2:         {           mySerial.print(Echo);           EchoMode = !EchoMode;           if (EchoMode) {             mySerial.Print("Him.");           } ed           {             mySerial.Print(OFF.);           }           mySerial.Print("");           mySerial.flush();           value = 0;           Multi = 0;           Sbuffer = "";           break;         }       case 3:         {           mySerial.Print("Read EEEPROM Content:" );           mySerial.flush();           for (int a = 0; a < EEPromBanks; a++)           {             mySerial.Print("EEPROM Memory Bank: + String(a) );             mySerial.flush();             for (int b = 1; b <= LcdRows; b++)             {               mySerial.print(Row. + String(b) + ": ");               mySerial.flush();               for (int c = 0; c < rLcdChr; c++)               {                 eeaddress = 0;                 eeaddress = (a * (rLcdChr) * LcdRows) + ((rLcdChr) * b) + c;                 value = EEPROM.read(eeaddress);                 mySerial.print(char(value));                 mySerial.flush();               }               mySerial.Print(" ");               mySerial.flush();             }           }           Sbuffer = "";           mySerial.Print("No more EEPROM Banks available.");           mySerial.flush();           break;         }       case 4:         {           value = 0;           mySerial.print(Erasing EEPROM);           mySerial.Print(YES/NO:);           mySerial.flush();           Multi = 1;           Sbuffer = "";           break;         }       case 5:         {           value = 0;           mySerial.print(Erasing EEPROM);           mySerial.Print("Stand by.");           mySerial.flush();           for (int a = 0; a < EEPromBanks; a++)           {             //Memory Bank a             mySerial.Print(Clear Bank. + String(a));             for (int b = 1; b <= LcdRows; b++)             {               for (int c = 0; c < rLcdChr; c++)               {                 eeaddress = 0;                 eeaddress = (a * (rLcdChr) * LcdRows) + ((rLcdChr ) * b) + c;                 FRM = EEPROM.read(eeaddress);                 if (FRM > 0)                 {                   EEPROM.write(eeaddress, 00); // Formatting                   mySerial.print(".");                   value++;                   delay(30);                   mySerial.flush();                 }               }             }             mySerial.Print("");             mySerial.flush();           }           mySerial.Print("");           mySerial.Print(Finished. + String(value) + "Change cleared");           mySerial.Print("");           mySerial.flush();           Sbuffer = "";           Multi = 0;           break;         }       case 6:         {           value = 0;           Sbuffer = "";           Multi = 0;           mySerial.Print("To abortion.");           mySerial.flush();           break;         }       case 7:         {           mySerial.Print("EEPPROM Bank Number (0-" + String(EEPromBanks - 1) + "):");           mySerial.flush();           Multi = 2;           value = 0;           Sbuffer = "";           break;         }       case 8:         {           Selection = value;           Text(1);           Multi = 3;           Sbuffer = "";           value = 0;           break;         }       case 9:         {           WriteEEPROM(Selection, 1);           Text(2);           value = 0;           Multi = 4;           Sbuffer = "";           break;         }       case 10:         {           WriteEEPROM(Selection, 2);           value = 0;           Multi = 0;           Sbuffer = "";           Text(3);           mySerial.flush();           value = 0;           Multi = 9;           Sbuffer = "";           break;         }       case 11:         {           value = 0;           mySerial.Print("EEPPROM Bank Number (0-" + String(EEPromBanks - 1) + "):");           Multi = 5;           Sbuffer = "";           mySerial.flush();           break;         }       case 12:         {           SelectedMsg = value;           DisplayBank(value);           break;         }       case 13:         {           value = 0;           mySerial.Print("EEPPROM Bank Number (0-" + String(EEPromBanks - 1) + "):");           Multi = 6;           Sbuffer = "";           mySerial.flush();           break;         }       case 14:         {           a = value;           if ( a < EEPromBanks)           {             mySerial.Print("Memory Bank: + String(a) );             mySerial.flush();             for (int b = 1; b <= LcdRows; b++)             {               mySerial.print(Row. + String(b) + ": ");               mySerial.flush();               for (int c = 0; c < rLcdChr; c++)               {                 eeaddress = 0;                 eeaddress = (a * (rLcdChr) * LcdRows) + ((rLcdChr) * b) + c;                 value = EEPROM.read(eeaddress);                 mySerial.print(char(value));                 mySerial.flush();               }               mySerial.Print(" ");               mySerial.flush();             }           } ed           {             mySerial.Print("Value out of Range.");           }           value = 0;           Sbuffer = "";           Multi = 0;           break;         }       case 15:         {           // Direct pPrint to Display           Directprint = true;           mySerial.Print ("Directprint ON.");           if (Directprint)           {             DirectPrintROW = 0;             DirectPrintLine = 0;             lp.clear();             lp.cursor();             lp.flash();           }           value = 0;           Sbuffer = "";           Multi = 0;           break;         }       case 16:         {           value = 0;           Sbuffer = "";           Multi = 0;           break;         }       case 17:         {           mySerial.Print("Display Brightness: (max 255)");           Multi = 7;           value = 0;           Sbuffer = "";           break;         }       case 18:         {           if ((value < 256))           {             Target = value;             mySerial.Print(Brightness: + String (Target) + Set);           } ed           {             mySerial.Print("Value out of Range.");           }           Multi = 0;           value = 0;           Sbuffer = "";           break;         }       case 19:         {           mySerial.Print("Fade Delay: (max 255 Sec)");           Multi = 12;           value = 0;           Sbuffer = "";           break;         }       case 20:         {           WriteEEPROM(Selection, 3);           value = 0;           Multi = 0;           Sbuffer = "";           Text(4);           mySerial.flush();           value = 0;           Multi = 10;           Sbuffer = "";           break;         }       case 21:         {           WriteEEPROM(Selection, 4);           value = 0;           Multi = 0;           Sbuffer = "";           break;         }       case 25:         {           if ((value > 0) & (value < 251))           {             FadeSeconds = value;             EEPROM.write(EEFadeSeconds, FadeSeconds);             mySerial.Print("Currency" + String (value) + "set.");           } ed           {             value = 0;             Sbuffer = "";             mySerial.Print("Value out of Range.");           }           value = 0;           Multi = 0;           Sbuffer = "";           break;         }       default:         {           if (DisplayLock.)           {             lp.clear();             DisplayLock. = false;           }           mySerial.Print("Smart Bluetooth Display 1.1--");           mySerial.Print("S - Read All EEPROM Banks");           mySerial.Print("E- WAS AT EEPROM BANKS");           mySerial.Print("W- Write sel. EEPROM Bank);           mySerial.Print("R- Read sel. EEPROM Bank);           mySerial.Print("P -Print EEPROM Bank on Display");           mySerial.Print("----------------------------------------");           mySerial.Print("D - Direct Print");           mySerial.Print("B - Display Brighness Current Value: + String (Current));           mySerial.Print("Other: ECHO");           mySerial.Print("----------------------------------------");           mySerial.Print("Type Cmd and press Enter");           mySerial.flush();           Multi = 0;           value = 0;           Sbuffer = "";         }     }   } // Input detected
}

void WriteEEPROM(exchange FBank, exchange FRY)
{   exchange Writecounter;   Writecounter = 0;   mySerial.print(Saving);   for (int c = 0; c < rLcdChr; c++)   {     eeaddress = 0;     eeaddress = (FBank * (rLcdChr) * LcdRows) + ((rLcdChr) * FRY) + c;     value = EEPROM.read(eeaddress);     if (Sbuffer[c] != value)     {       EEPROM.write(eeaddress, Sbuffer[c]);       mySerial.print(".");       Writecounter++;     }   }   mySerial.Print(" " + String (Writecounter) + "Change Written.");
}

void ClearCBuffer. ()
{   for (exchange a = 0; MaxInputBufferSize - 1; a++)     Cbuffer[a] = 0;
}

exchange SerInputHandler()
{   exchange result = 0;   int c;   int d;   int a;   int b;   result = 0;   if (CheckforserialEvent())   {     if ((Number) and not (Data) and not (StrInput))    //Numbers only     {       Sbuffer = "";       value = 0;       StrInput = false;       Number = false;       Data = false;       EnterInput = false;       a = 0;       b = 0;       c = 0;       d = 0;       Sbuffer = Cbuffer; /Number is up! taken over in SBUFFER if necessary.       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;       } ed       {         ClearCBuffer.;         Ccount = 0;         result = 1;                                                //Number Returncode         Number = false;         StrInput = false;         Data = false;         EnterInput = false;         Ccount = 0;         return result;       }     }     if ((StrInput) and not (Data))                          //String Input only     {       Sbuffer = "";       Sbuffer = Cbuffer;       value = 0;       StrInput = false;       Number = false;       Data = false;       EnterInput = false;       Ccount = 0;       ClearCBuffer.;       result = 2;                                                 //Number Returncode     }     if (Data) {       Sbuffer = "";       Sbuffer = Cbuffer;       value = 0;       StrInput = false;       Number = false;       Data = false;       EnterInput = false;       Ccount = 0;       ClearCBuffer.;       result = 3;                                               //Number Returncode     }     if ((EnterInput) and not (StrInput) and not (Number) and not (Data))     {       Sbuffer = "";       value = 0;       Ccount = 0;       ClearCBuffer.;       result = 4;                                               //Number Returncode     }     Number = false;     StrInput = false;     Data = false;     EnterInput = false;     Ccount = 0;     return result;   }   return result;   //End CheckforSerialEvent
}

// 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.Print("");         DirectPrintLine = 0;         DirectPrintROW = DirectPrintROW + 1;         if ( DirectPrintROW > 3)         {           Directprint = false;           lp.noCursor();           lp.NoBlink();           Sbuffer = "";           value = 0;         } ed         {           lp.cursor();           lp.flash();           lp.setCursor(0, DirectPrintROW);         }       }       EnterInput = true;       return true;     } ed if (TBuffer > 47 && TBuffer < 58 )     {       if ( Ccount < MaxInputBufferSize)       {         Cbuffer[Ccount] = TBuffer;         Ccount++;         if ((Directprint))         {           lp.print(char(TBuffer));           DirectPrintLine = DirectPrintLine + 1;           if ( Ccount > MaxInputBufferSize - 1)           {             lp.noCursor();             lp.NoBlink();           } ed {             lp.cursor();             lp.flash();           }         }         if (EchoMode) {           mySerial.print(char(TBuffer));           mySerial.flush();         }       } ed {         mySerial.print("#");       }       //Number Input detected       Number = true;     }     ed if (TBuffer > 64 && TBuffer < 123 )     {       if ( Ccount < MaxInputBufferSize)       {         Cbuffer[Ccount] = TBuffer;         Ccount++;         if ((Directprint))         {           lp.print(char(TBuffer));           DirectPrintLine = DirectPrintLine + 1;           if ( Ccount > MaxInputBufferSize - 1)           {             lp.noCursor();             lp.NoBlink();           } ed {             lp.cursor();             lp.flash();           }         }         if (EchoMode) {           mySerial.print(char(TBuffer));           mySerial.flush();         }       } ed {         mySerial.print("#");       }       // if (DebugMode) {mySerial.println("Debug: Char over Serial received");       // mySerial.flush();}       //Character Char Input detected       StrInput = true;     }     ed if ( (TBuffer == 127 )  |  (TBuffer == 8 ) )     {       if ( DirectPrintLine > 0 )       {         DirectPrintLine = DirectPrintLine - 1;         lp.setCursor(DirectPrintLine, DirectPrintROW);         lp.print(" ");         lp.setCursor(DirectPrintLine, DirectPrintROW);       }       if (( DirectPrintLine == 0 ) & ( DirectPrintROW > 0 ))       {         DirectPrintROW = DirectPrintROW - 1;         DirectPrintLine = rLcdChr - 1;         lp.setCursor(DirectPrintLine, DirectPrintROW);       }       if ( Ccount > 0)       {         Ccount--;         Cbuffer[Ccount] = 0;         if ((Directprint))         {           if ( Ccount > MaxInputBufferSize - 1)           {             lp.noCursor();             lp.NoBlink();           } ed {             lp.cursor();             lp.flash();           }         }         if (EchoMode) {           mySerial.print("-");           mySerial.flush();         }       }     }     ed     {       if ( Ccount < MaxInputBufferSize)       {         Cbuffer[Ccount] = TBuffer;         Ccount++;         if ((Directprint))         {           DirectPrintLine = DirectPrintLine + 1;           if (TBuffer < 128) {             lp.print(char(TBuffer));           } ed  {             lp.print(String(TBuffer));           }           if ( Ccount > MaxInputBufferSize - 1)           {             lp.noCursor();             lp.NoBlink();           } ed {             lp.cursor();             lp.flash();           }         }         if (EchoMode) {           mySerial.print(char(TBuffer));           mySerial.flush();         }       } ed {         mySerial.print("#");       }       //Data Input detected       Data = true;     }     return false;   }   return false;
}

void Displayprocessor()  // In the case of a blue display, scroll function is omitted as this only "lubricates"
{   if (RefreshDisplay)   {     lp.clear();     RefreshDisplay = false;     for (int b = 1; b <= LcdRows; b++)     {       lp.setCursor(0, b - 1);       mySerial.print(Row. + String(b) + ": ");       for (int c = 0; c < rLcdChr; c++)       {         eeaddress = 0;         eeaddress = (DisplayBankContent * (rLcdChr) * LcdRows) + ((rLcdChr) * b) + c;         value = 0;         value = EEPROM.read(eeaddress);         if (value > 31) // Do not show special characters         {           mySerial.print(char(value));           lp.print(char(value));         } ed         {           lp.print(char(32));         }       }       mySerial.Print();     }   }
}

void runrealTimeClock()    //TIME
{   // Real Time Clock & Countdown   // long previousMillis = 0; // will store last time was measured   // byte SecDivider = 0;   unsuitable long currentMillis = millis();   int StepValue = 0;   // PWM Display Control   StepValue = 4 * FadeSeconds;   if (currentMillis - previousMillis > StepValue)   {     previousMillis = currentMillis;     if (Current < Target        )     {       Current = Current + 1;       analog (BackgroundLight, Current);     } ed if (Current > Target)     {       Current = Current - 1;       analog (BackgroundLight, Current);     }   }   if (currentMillis - previousMillisB > 1000)   {     // Secondary     previousMillisB = currentMillis;   }
}

void DisplayBank ( exchange cobank)
{   if (cobank  < EEPromBanks )   {     RefreshDisplay = true;   // Initialize Display Output     DisplayBankContent = cobank;     mySerial.Print("Bank" + String(cobank) + "is displayed on LCD");     Multi = 0;     Sbuffer = "";     value = 0;     mySerial.flush();   } ed   {     mySerial.Print(Bank not available.);     value = 0;     Multi = 0;     Sbuffer = "";     mySerial.flush();   }
}

void SwitchProcessor()
{   Switchstate. = digitalRead(SwitchPin.);   if ((!Switchstate.) && (SwitchstateBuffer) && (not DisplayLock.))// Query Switches   {     SwitchstateBuffer = false;     Directprint = false;     lp.noCursor();     lp.NoBlink();     SelectedMsg = SelectedMsg + 1;     if (SelectedMsg >  EEPromBanks - 1 )     {       SelectedMsg = 0;     }     lp.clear();     lp.setCursor(0, 0);     lp.print("Bank: + String(SelectedMsg) + "selected");     lp.setCursor(0, 2);     delay(10);     value = DelayTOPWROFF.;     while (digitalRead(SwitchPin.) == 0)     {       delay(1);       if (value > 0) {         value = value - 1;       };       lp.setCursor(0, 3);     }     DisplayBank(SelectedMsg);   }   if (Switchstate.)   {     SwitchstateBuffer = true;     // delay(10);   }
}

 

In addition to the hardware extension, we now have a small auxiliary function in the menu: the command is "echo". With this we switch on and off the Echo character function of the serial interface. In order to clarify what effect this has, the Echo function is first switched off with the Echo command in the following screenshot. This is cleared with "Echo OFF". With the same command, the echo function is reactivated. (Toggle function). This is similarly validated with "Echo ON". From this point on, each character is acknowledged.

Part 4- Display Menu

 

I wish you a lot of fun in rebuilding and, as always, until next time.

DisplaysFor arduinoProjects for advancedSensors

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