Below is a simple program to demonstrate writing text to a file named log.txt. A complete line of text is joined together, including reading the soil moisture on analog pin A1, and the timer. The program loops once per minute (60 seconds, 60000 milliseconds).

When the Circuit Playground Express (CPX) is reset, one reading is taken per minute and written to the file log.txt. The program displays a rainbow animation while taking readings. When the program has taken 10 readings, collection is done and the pixels turn all green.

The append file with line block is used. If log.txt already exists, the new values will be added to the end of the file, otherwise a new file will be opened and the text output. If you want to have a clean file for each data run, rename log.txt to another name between runs.

An on start block is used here as for this example only a finite number of readings is desired. Then we want MakeCode to close the file and make it available on the MAKECODE flash drive for viewing.

Opening log.txt in a text editor shows the output from this program:

You can see the formatting of the data output line took more blocks, but it is more flexible than the Datalogger extension allows.

The Storage module also allows for reading files. This is ideal for reading a configuration file which may specify to the program how to accomplish the task at hand.

This guide was first published on Aug 27, 2019. It was last updated on Aug 27, 2019.

This page (Storage Use) was last updated on Aug 26, 2019.

Text editor powered by tinymce.