Assembling a string

Still don't seem to be able to get my head around strings :confused:

I want to take 12x digits of a phone number, stored individually in 12x EEprom addresses.
The string needs to start with a + symbol.

So, my result need to look like this:

String GSM_User_No= "+445959723224";

Each number is stored as:

EEPROM.write (1,4);
EEPROM.write (1,4);
EEPROM.write (1,5);
EEPROM.write (1,9); etc etc

Later on retrieved into:

GSMuser1 = EEPROM.read(1);
GSMuser2 = EEPROM.read(2);
GSMuser3 = EEPROM.read(3);
GSMuser4 = EEPROM.read(4);

How do I re-arrange that into a useable string? I just can't get it to read back correctly

I thought (incorrectly):

String GSM_User_No= ("+"+GSMuser1+GSMuser2+GSMuser3+GSMuser4 etc);

Ideally, I would do away with the GSMuser variable all together, and just assemble the string directly from EEPROM if possible.

It's just a formatting issue I think? Google has yet to come to the rescue

Is the value in EEPROM a single digit or a single digit ASCII code?

As usual..... literally 2 minutes after asking the question, I get it

String GSM_Eng_No= String("+")+String(GSMuser1)+String(GSMuser2)+String(GSMuser3)+String(GSMuser4) etc

Do NOT use the "String" object on an arduino, it will cause memory corruption and eventually a crash - use char arrays instead. When using EEPROM, I would suggest you to use get & put:

#define EEPROM_ADDRESS 123
uint8_t PHONE_NUMBER[6] = {44, 59, 59, 72, 32, 24};

//Store the number
EEPROM.put(EEPROM_ADDRESS, PHONE_NUMBER);

//Load the number
EEPROM.get(EEPROM_ADDRESS, PHONE_NUMBER);

//Convert to string
char buf[14];
sprintf(buf, "+%d%d%d%d%d%d", PHONE_NUMBER[0], PHONE_NUMBER[1], PHONE_NUMBER[2], PHONE_NUMBER[3], PHONE_NUMBER[4], , PHONE_NUMBER[5]);

Serial.println(buf);

The code is untested.

EDIT: "%d" in the format should probably be "%02d" in order to support leading zeros.

@Danois90 I would not use BCD but the chars directly including the trailing zero.

#include <EEPROM.h>

char GSM_User_No[14]= "+445959723224";

void setup() {
  Serial.begin(250000);
  EEPROM.put(1, GSM_User_No);
  char getItBack[14];
  EEPROM.get(1, getItBack);
  Serial.println(getItBack);
}
void loop() {}
+445959723224

Whandall:
@Danois90 I would not use BCD but the chars directly including the trailing zero.

If that sollution is possible for OP, it would of cause be the simplest :wink:

EDIT: But in that case, using a const progmem would be even better:

const char PHONE_NUMBER[] PROGMEM = {"+445959723224"};

Eh?
But.... the Sim800L GSM library examples all use strings to send the number to the GSM module.

So I have to re-write the way those examples work? That totally sucks and is out of my ability I would think.

Why is the string Object so useless then? Isn't that a bit of a fundamental flaw?
Used strings many times before with other processors....

Brilliant

There are two strings only ever basically get declared at the boot in the variable declarations.
They are not likely to get changed during general running of the program.
The phone number gets 'set' in a settings routine that is probably only ever run once.

Is that still going to be an issue?

We've got nothing against strings - it's Strings we don't like.

eh?

SteveRC2017:
eh?

We've got nothing against strings - it's Strings we don't like.

Read this to see why we do not like Strings. (String class).

Well I just looked at all my code, which is heavily built around the (apparently useless) String filled GSM examples.

It's beyond me how to change that, so it's going to have to stay.
Just have to see how reliable it turns out to be.

Oh... and that link doesn't work (took me to some dodgy page??), but it might be the page I Googled after 'Strings are useless' was mentioned

fixed the link. That psge also has information on using strings and string functions in place of Strings.

Hmm. That is the page I read. To be honest... I understand the Heap. stack stuff, but then just got lost. Beyond me.
I certainly don't understand how to replace my current code with something other than Strings.

My phone numbers for example.
They are read from 12x EEPROM addresses at the start of the code and put into the Users phone number String.

They can only be changed for a digit 0-9 in the users setup routine (which will very rarely be accessed - you only set your phone number once) and then written back to the same EEPROM address.

From then on, they are only ever recalled once at the boot to create the initial String. Is that going to cause an issue? They are not constantly changed.

I might throw this over to a Teensy, where I have more memory anyway.

I might have a go at changing the GSM code, but I just had a go and it didn't go well :confused:

Below is a snippet of the code. This part if anything is more likely to cause issues, as it's reading data from the GSM module:

String GSM_readSerial() {
      GSM_timeout = 0;
      while  (!GSMSerial.available() && GSM_timeout < 12000  )
      {
        delay(13);
        GSM_timeout++;
      }
      if (GSMSerial.available()) {
        return GSMSerial.readString();
      }
    }

:o Never as simple as I think

Search for Robin'2's serial input basics tutorial. It shows how to read serial data into a string without using delays or blocking functions like readStrin().

PJRC appears to have fixed the String bug when using Teensy (I believe)

String corrupts the heap and crashes - Suggestions for the Arduino Project - Arduino Forum - post 10.

So, I moved the project over to a Teensy 3.2.

To be honest, I much prefer the Teensy's anyway. I was just trying to use up my old stock of Arduino's.

Will still try to learn the String 'stuff', but hoping my current code will be OK for now