Write String to Arduino EEPROM Update And Example

*This is a followup to my Write String to Arduino EEPROM article.*

As it goes, sometimes you find one thing when looking for another. In this case, I was doing some research on a project I’m planing to make in the near future. This is when I came across a line of code that would make turning a Char Array into a String much easier and faster then the method I was using for both my EEPROM and FRAM read_String functions.

When I was reading information back from memory and returning a String, the first operation I needed to do was convert a Char Array into a String. Each character was returned one at a time and then needed to be reconstructed into a String type variable. To do this I used the concat, which would append to a String variable. To use concat, I needed to loop through a Char array, this would add each new character to the String (See Below).

This code does work well, though it is not fast. Concat by itself is slow, and if you look in the loop above you can see a delay statement. I found when testing the code, that due to the speed in which concat works, if you don’t delay between uses it can cause garbage data. The time penalty in the code might not seem too bad, but makes it self very noticeable when reading long or many Strings. At the time I wrote this function, I did not know of another way of doing this operation but was always unhappy with its performance. Now I have fixed it.

While I was reading up on some info for a future project (Was reading this) I found that you can typecast a Char array into a String.

Quickly, typecasting is making a variable  of one type work like another. For example you could typecast a character variable as an integer and then preform a math operation on it.

 

In this case I can typeset the Char array as a String which would mean I would not need the for loop nor the time delays. The resulting code is as follows:

 

This code is a little bit smaller and much faster. There is no need for the delay anymore which means no more hang when reading large Strings of data.

Here is an example code of writing and reading back a string to the EEPROM.

 

Arduino C, Programing , , , , ,

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.