Arduino Library - String and Serial Read/Write

Hello all

I'm having some trouble creating and implementing my own library file (.h & .cpp) and so I am after some help/ advice/ an example. My understanding of the Arduino language and C language is novice.

My goal: I plan to create a library file that contains the core 'write and read' functions I require, the 'variable' format for these being mostly the 'String' type. With this, I can then make simple sketches that will display the various 'string' data onto an LCD display as and when requested.

My 1st question is how do I pass or copy 'string' date from the library file to my sketch? I've used the following links to guide me but they don't show the 'string' example I am after.

Links I've refereed to:

https://playground.arduino.cc/Code/Library

Thanks in advance
Ross

Firstly, don't confuse Strings (objects created using the String library) and strings (zero terminated arrays of chars). Which do you mean to use ?

I can then make simple sketches that will display the various 'string' data onto an LCD display as and when requested.

What advantage are you hoping to get over the standard LCD library ?

Firstly, don't confuse Strings (objects created using the String library) and strings (zero terminated arrays of chars). Which do you mean to use ?

my 'String' or 'string' example - I am still somewhat confused as to which one I am referring to:

String Message 1;

Library code example:

void Fetch_Message1()
{
  Serial.write("Message1_Req\n");
  while (Serial.available() == 0) 
  {
    // do nothing
  }
  Message1 = Serial.readString();
  Message1.replace("\n", "");
}

Sketch code example:

void LCD_DISPLAY()
{
      CurrMilli = millis();
      if (CurrMilli - PrevMilli >= ReadInterval)
      {
        lcd.setCursor(0,0);
        lcd.print("                ");
        lcd.setCursor(0,0);
        lcd.print("Element 1:"+Message1);
      }
}

What advantage are you hoping to get over the standard LCD library ?

I want the serial write/ read to run continuously in the background, without effecting the time or speed of the sketch, so that the LCD updates the messages continuously (within a reasonable time frame).

my 'String' or 'string' example - I am still somewhat confused as to which one I am referring to

You really need to understand the difference before going any further.

You appear to be using Strings in the examples posted. In an environment with limited memory such as the Arduino, Strings are usually regarded as a bad thing because they have a reputation for fragmenting the already limited memory whereas strings don't

I am sorry but I still don't understand what you or trying to do with your library or why it offers an advantage. It is very easy to update an LCD display at intervals without affecting other program elements but why you would want to do so if the data to be displayed has not changed is beyond me.

This, of course is beyond the pale when discussing code affecting the speed of the program

  while (Serial.available() == 0) 
  {
    // do nothing
  }

UKHeliBob - I've sent you a PM

An extract from the PM

The problem I foresee is that if I am requesting 20+ parameters in one going i wont be able to constantly update the LCD in a reasonable time frame.

Whether you use a library that you have written or functions in your program it is going to take the same time to update the LCD.

Have you determined how often you need to update the LCD ? The maximum update frequency that would be required is when any of the data being displayed changes, but it is quite possible that you don't need to update it that frequently if the changes are small

Morning

I think a screen refresh rate of 10 Hz (100 ms) would be the minimum, 30 Hz (33.3 ms) ideal.

So I guess I can't run the serial read/ write and lcd update in two separate loops, running at the same time, as I imagined?

You cannot run two things at the same time whether you use a library or not. You can, or course, run 2 things quickly one after the other to give the appearance of them running at the same time.

100 milliseconds is an eternity even for the Arduino.

Can you please outline what you want to read and how you want to read it ? Presumably it is a serial interface and if so what is the baud rate ? What sort of LCD device do you want to write to ?

Can you please outline what you want to read and how you want to read it ? Presumably it is a serial interface and if so what is the baud rate ? What sort of LCD device do you want to write to ?

I will be reading values associated to various parameters, from what I've seen so far. I've used 'Strings' for the serial read/ write commands so I know what parameters (or values) I have called for. I left the baud rate at 9600 but I know that I can increase this value to increase the speed of the data communication.

I don't know what LCD display's I will use in the future but this is what I've used to date - https://www.amazon.co.uk/gp/product/B01EYW5R5M/ref=oh_aui_detailpage_o01_s00?ie=UTF8&psc=1#

I still don't have the foggiest idea what devices you will be reading from and how. Will you always be receiving 20 values at a time that need parsing out of the data steam or will the data arrive as individual serial messages?

As far as serial data is concerned it arrives quite slowly compared the the speed that the Arduino can service it, so there is a good chance that there will be time to update the LCD when required. The good thing about reading from a serial interface is that you only need to do anything when data is available. I am intrigued as to how you are going to display the values from 20 sensors on the 2 by 16 display.