micro:bit basic data logging 2

Second program to write text to a file and read the contents of the file, It has been written as I could not find an easy example of writing and reading to a file with the micro:bit.

This is an extension to the first program about logging data. This program stores real time data (the x acceleration over a period of 2.5 seconds) and reads back that data. It also tidies the reading and writing of data into functions.

It was inspired by this blog post

Second program to write text to a file and read the contents afterwards

Lines 1 to 3 initialisation

Lines 5 to 7 Writes a line of text (data) to file.
The “file handle” (which is the reference to the file, used by open, write and close operations) and the data to be written are passed in.

This function seems hardly needed as it is only two lines long, which could be in the main part of the code – where the file open and file close are called from.

Lines 9 – 17 Function readData() to read data from file
Line 9 The name of the file is passed in
Line 10 Variable ‘eof’ indicates the end of the file has been reached (eof is an abbreviation for end of file, and often used in computing).
Line 11 opens the file, “r” is used to indicate we are reading the file
Line 12 – 16 reads a line of text from the file and prints it out, “\n” is added to the denote the end of the line
Lines 14 – 15: It seems reasonable that if we read from the file and no text is returned – that is the end of the file, and eof is set to be True
Line 17 closes the file

It should be noted that the style of readData() is different from writeData() in that it opens reads all data and closes the file.

Lines 19 – 36 main part of the program

lines 20 – 22 shows a right arrow, to indicate that you need to press the button B. The reason for testing n=0, and setting n afterwards is to ensure the code runs only once.
lines 24-26 the file is open for writing (note the variable fw which is a file handle, referring to the file), and a title for the data is written to the file
lines 28 – 33 This loops around 10 times, with a delay of 250 ms (0.25 seconds) .
Line 29 The x-acceleration is read
line 30 time is formatted into a form like 0.25, 0.5 etc. Some maths here!
Line 32 the data is wriiten to file (usng the file handle)

line 34 the file is closed (using the file handle)

Lines 36 to 38 Read data from file
The data is read from the file. All of the work is done in readData()

Instructions for Use:
Flash to the micro:bit and it will run and display a right arrow. You should press button B and then waggle the microbit for 2.5 seconds. The values of the x-acceleration will be shown on the screen.

After that, the program will open the file and read back values.

Output from program 2

The program as a text file can be downloaded here

The file has been written as a csv (comma separated variable) format, which is designed to be read from a spreadsheet. However, the file is only temporarily stored on the micro:bit.

This has been written as an example of to how to use files on the micro:bit, comments and suggestions to improve this would be welcome. Code can be freely downloaded, adapted and used. If a school could mention our software for primary/elementary students we would be very grateful stormeducational.co.uk (with free online demos at stormedapps.co.uk).

Leave a comment