LEGO MindStorms EV3 – Use File Access Block to Store Data

In LEGO MindStorms EV3-G software, we could use File Access block to store text or number and read out them when necessary. However, what is the difference between File Access block and Varaible block? Are there any constraints fo it? In this post, I would like to explain a bit more about File Access block.

You could download the sample EV3-G project here:

Write into a File Access Block

If you just want a file to record the latest data, you could delete this file before writing into it. The logic for the File Access block is that when writing into a non-existing file, the program will create a file automatically. In this way, you could ensure there is only one record in the file.

If you do not delete this file first, the input data will be appended to the existing file.

Please note that you could not find the file name from Project Properties window. File Access block is like Variable block in EV3-G in certain degree. When you want to check the values stored in a file, you have to read out via Display block. In the above example, the value of “abc” file is read out and displays on the screen for 5 seconds.

(File Access blocks are not showing in Project Properties)

Read from a File Access Block

Besides reading a file right after writing into it, you could also read a file from another Program. Just ensure that both programs are within a same project. As shown in the below sample. The program reads the value of the file “abc” which is created in the above program.

Purpose of File Access Block – Close Mode

You could read and write File Access block without needing to close it. However, Close Mode has a unique function. After a file is closed and the next time it is read, it will begin reading from the first line.

It is useful when you put multiple lines of value in a file and would read out them in sequence.

In the following example, the program writes the reflective light intensity of a color sensor into a file called “multiple light” for three times. After that, the program closes the file. The purpose is that the next time it is opened to be read, it starts from the first line.

Comparion with Variable

File Access block looks similar to Varaible block. Both of them could be used to store data, but they are different in some aspects.

First, the difference lies in the amount of data stored. Variable block could only store one item, no matter it is number or text. File Access block could write and read for multiple times. The Read and Write modes read/write the file one line at a time. Using File Access blocks multiple times, the program could read/write multiple lines. If the last line of the file is reached, the output will be 0.

Another difference is the duration of the data. Once a program stops, Variable block could not keep the value any more. Therefore, Variable block is mainly used to store temporary data used in a program.

On the other hand, File Access block could store data longer. When the program stops running, the data is still maintained. Another program could use the data. Even after the project is closed and reopened, the data is still kept. This feature makes File Access block especially suitable for reading some initial calibration values, such as the reflective light intensity of the colour sensor, or the distance value of ultrasonic sensor, without worrying them get lost.

However, please also note that File Access block value is not permanent. If you disconnect LEGO smarthub from your computer and reconnect again, the data in the File Access block is lost, not to mention to restart your computer. Another constraint of File Access block is that you could not share it between different projects.

Anyway, File Access block is a good place to store the “global” common data shared by different programs in a project. If you would like to record the value just once and use it by several programs, it is a perfect choice.

Hope this little post is helpful! Enjoy the coding and have fun!

Note: All the analysis articles are copyright products of http://www.thecodingfun.com. Anyone re-posting them should credit author and original source. Anyone using them for commercial purposes or translating them into other languages should notify TheCodingFun and get confirmation first. All Rights Reserved.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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