{"id":120,"date":"2022-08-21T14:01:24","date_gmt":"2022-08-21T13:01:24","guid":{"rendered":"https:\/\/stormappsonline.co.uk\/blog\/?p=120"},"modified":"2022-09-01T20:03:56","modified_gmt":"2022-09-01T19:03:56","slug":"microbit-basic-data-logging-2","status":"publish","type":"post","link":"https:\/\/stormappsonline.co.uk\/blog\/2022\/08\/21\/microbit-basic-data-logging-2\/","title":{"rendered":"micro:bit basic data logging 2"},"content":{"rendered":"\n<p>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.<br><br>This is an extension to the <a href=\"https:\/\/stormappsonline.co.uk\/blog\/2022\/08\/21\/microbit-basic-data-logging-1\/\">first program about logging data<\/a>. 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.<br><br>It was inspired by <a href=\"https:\/\/teachingmathsnsw.wordpress.com\/2017\/03\/01\/microbit-data-logging\/\">this blog post<\/a> <\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"563\" height=\"613\" src=\"https:\/\/stormappsonline.co.uk\/blog\/wp-content\/uploads\/2022\/08\/log_program_2.png\" alt=\"\" class=\"wp-image-121\" srcset=\"https:\/\/stormappsonline.co.uk\/blog\/wp-content\/uploads\/2022\/08\/log_program_2.png 563w, https:\/\/stormappsonline.co.uk\/blog\/wp-content\/uploads\/2022\/08\/log_program_2-276x300.png 276w\" sizes=\"(max-width: 563px) 100vw, 563px\" \/><figcaption>Second program to write text to a file and read the contents afterwards<\/figcaption><\/figure>\n\n\n\n<p><strong>Lines 1 to 3 initialisation<\/strong><br><br><strong>Lines 5 to 7 Writes a line of text (data) to file.<\/strong><br>The \u201cfile handle\u201d (which is the reference to the file, used by open, write and close operations) and the data to be written are passed in.<br><br>This function seems hardly needed as it is only two lines long, which could be in the main part of the code \u2013 where the file open and file close are called from.<br><br><strong>Lines 9 \u2013 17 Function readData() to read data from file<\/strong><br><strong>Line 9<\/strong> The name of the file is passed in<br><strong>Line 10<\/strong> Variable \u2018eof\u2019 indicates the end of the file has been reached (eof is an abbreviation for end of file, and often used in computing).<br><strong>Line 11<\/strong> opens the file, \u201cr\u201d is used to indicate we are reading the file<br><strong>Line 12 \u2013 16<\/strong> reads a line of text from the file and prints it out, \u201c\\n\u201d is added to the denote the end of the line<br><strong>Lines 14 \u2013 15<\/strong>: It seems reasonable that if we read from the file and no text is returned \u2013 that is the end of the file, and eof is set to be True<br><strong>Line 17<\/strong> closes the file<br><br>It should be noted that the style of readData() is different from writeData() in that it opens reads all data and closes the file.<br><br><strong>Lines 19 \u2013 36 main part of the program<\/strong><br><br>lines 20 \u2013 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.<br>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<br>lines 28 \u2013 33 This loops around 10 times, with a delay of 250 ms (0.25 seconds) .<br>Line 29 The x-acceleration is read<br>line 30 time is formatted into a form like 0.25, 0.5 etc. Some maths here!<br>Line 32 the data is wriiten to file (usng the file handle)<\/p>\n\n\n\n<p>line 34 the file is closed (using the file handle)<br><br><strong>Lines 36 to 38 Read data from file<\/strong><br>The data is read from the file. All of the work is done in readData()<br><br><strong>Instructions for Use:<\/strong><br>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.<br><br>After that, the program will open the file and read back values.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"511\" height=\"853\" src=\"https:\/\/stormappsonline.co.uk\/blog\/wp-content\/uploads\/2022\/08\/output_program_2.png\" alt=\"\" class=\"wp-image-122\" srcset=\"https:\/\/stormappsonline.co.uk\/blog\/wp-content\/uploads\/2022\/08\/output_program_2.png 511w, https:\/\/stormappsonline.co.uk\/blog\/wp-content\/uploads\/2022\/08\/output_program_2-180x300.png 180w\" sizes=\"(max-width: 511px) 100vw, 511px\" \/><figcaption>Output from program 2<\/figcaption><\/figure>\n\n\n\n<p>The program as a text file can be <a href=\"https:\/\/stormappsonline.co.uk\/python\/logging_data_2.py\">downloaded here<\/a><br><br>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.<br><br>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. <em>Code can be freely downloaded, adapted and used. If a school could mention our software for primary\/elementary students we would be very grateful <a href=\"https:\/\/www.stormeducational.co.uk\/\">stormeducational.co.uk<\/a> (with free online demos at&nbsp;<a href=\"https:\/\/www.stormedapps.co.uk\/\"><em>stormedapps.co.uk<\/em><\/a>).<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&hellip; <a class=\"more-link\" href=\"https:\/\/stormappsonline.co.uk\/blog\/2022\/08\/21\/microbit-basic-data-logging-2\/\">Continue reading <span class=\"screen-reader-text\">micro:bit basic data logging 2<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[20,12,21],"tags":[15,13,14],"_links":{"self":[{"href":"https:\/\/stormappsonline.co.uk\/blog\/wp-json\/wp\/v2\/posts\/120"}],"collection":[{"href":"https:\/\/stormappsonline.co.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/stormappsonline.co.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/stormappsonline.co.uk\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/stormappsonline.co.uk\/blog\/wp-json\/wp\/v2\/comments?post=120"}],"version-history":[{"count":7,"href":"https:\/\/stormappsonline.co.uk\/blog\/wp-json\/wp\/v2\/posts\/120\/revisions"}],"predecessor-version":[{"id":142,"href":"https:\/\/stormappsonline.co.uk\/blog\/wp-json\/wp\/v2\/posts\/120\/revisions\/142"}],"wp:attachment":[{"href":"https:\/\/stormappsonline.co.uk\/blog\/wp-json\/wp\/v2\/media?parent=120"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/stormappsonline.co.uk\/blog\/wp-json\/wp\/v2\/categories?post=120"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/stormappsonline.co.uk\/blog\/wp-json\/wp\/v2\/tags?post=120"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}