{"id":115,"date":"2022-08-21T13:49:53","date_gmt":"2022-08-21T12:49:53","guid":{"rendered":"https:\/\/stormappsonline.co.uk\/blog\/?p=115"},"modified":"2022-08-22T17:47:11","modified_gmt":"2022-08-22T16:47:11","slug":"microbit-basic-data-logging-1","status":"publish","type":"post","link":"https:\/\/stormappsonline.co.uk\/blog\/2022\/08\/21\/microbit-basic-data-logging-1\/","title":{"rendered":"micro:bit basic data logging 1"},"content":{"rendered":"\n<p>This is a first program to write text to a file and read the contents of the file again<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"339\" height=\"276\" src=\"https:\/\/stormappsonline.co.uk\/blog\/wp-content\/uploads\/2022\/08\/log_program_1.jpg\" alt=\"\" class=\"wp-image-117\" srcset=\"https:\/\/stormappsonline.co.uk\/blog\/wp-content\/uploads\/2022\/08\/log_program_1.jpg 339w, https:\/\/stormappsonline.co.uk\/blog\/wp-content\/uploads\/2022\/08\/log_program_1-300x244.jpg 300w\" sizes=\"(max-width: 339px) 100vw, 339px\" \/><figcaption>A first program to write text to a file and read the contents afterwards<\/figcaption><\/figure>\n\n\n\n<p>There are three parts to writing data to file:<br>&#8211; opening the file<br>&#8211; writing data<br>&#8211; closing the file afterwards<br><br><strong>Line 3: opening the file <\/strong><br>The &#8216;open&#8217; commands says open a file with name \u201cstorm.txt\u201d; the w indicates the file is to be opened to write, and the function open returns a \u201cfile handle\u201d (which is the reference to the file, used by write and close operations with the file, which we store in variable fw)<br><br><strong>Lines 4-6 writing data to the file. <\/strong><br>This writes three lines of text to the file. At the end of each lines we add \u201c\\n\u201d (which indicates end of line)<br><br><strong>Line 7 closing the file. <\/strong><br>Sometimes you can get away without doing this, but the computer may not like it, and unexpected things can happen.<br><br><strong>Lines 9 to 16 Read from the File<\/strong><br>Again, there are three parts to reading from a file:<br>&#8211; opening the file<br>&#8211; reading data<br>&#8211; closing the file afterwards<br>Again, note the use of the \u201cfile handle\u201d, which, this time, we store in variable fr<br><br>There is an important difference when reading from a file &#8211; how do we know when we have read everything? In this case, we know there are 3 lines, but that could easily be changed.<br><br>Line 9 Here we have a variable &#8216;eof&#8217; to indicate that the end of the file has been reached (eof is an abbreviation for end of file, and often used in computing).<br>Lines 14 \u2013 15: 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><br>Line 10 opens the file. Note it is the same file as we had on line 5 , and this time, we use \u201cr\u201d to indicate we are reading the file<br>Line 12 &#8211; 13 reads a line of text from the file and prints it out. Remember on lines 6-8 \u201c\\n\u201d was added to the denote the end of the line<br>line 16 closes the file<br><br><strong>Instructions for Use:<\/strong><br>Flash to the micro:bit and it will run and display the output<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"398\" height=\"196\" src=\"https:\/\/stormappsonline.co.uk\/blog\/wp-content\/uploads\/2022\/08\/log_output_program_1.jpg\" alt=\"\" class=\"wp-image-118\" srcset=\"https:\/\/stormappsonline.co.uk\/blog\/wp-content\/uploads\/2022\/08\/log_output_program_1.jpg 398w, https:\/\/stormappsonline.co.uk\/blog\/wp-content\/uploads\/2022\/08\/log_output_program_1-300x148.jpg 300w\" sizes=\"(max-width: 398px) 100vw, 398px\" \/><figcaption>Output from program<\/figcaption><\/figure>\n\n\n\n<p><br>The program as a text file can be <a href=\"https:\/\/stormappsonline.co.uk\/python\/logging_data_1.py\">downloaded here<\/a><\/p>\n\n\n\n<p>There is a further program <a href=\"https:\/\/stormappsonline.co.uk\/blog\/2022\/08\/21\/microbit-basic-data-logging-2\/\">to read and write data to a file<\/a><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. But 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\u00a0<a href=\"https:\/\/www.stormedapps.co.uk\/\"><em>stormedapps.co.uk<\/em><\/a>).<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is a first program to write text to a file and read the contents of the file again There are three parts to writing data to file:&#8211; opening the file&#8211; writing data&#8211; closing the file afterwards Line 3: opening the file The &#8216;open&#8217; commands says open a file with name \u201cstorm.txt\u201d; the w indicates&hellip; <a class=\"more-link\" href=\"https:\/\/stormappsonline.co.uk\/blog\/2022\/08\/21\/microbit-basic-data-logging-1\/\">Continue reading <span class=\"screen-reader-text\">micro:bit basic data logging 1<\/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\/115"}],"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=115"}],"version-history":[{"count":4,"href":"https:\/\/stormappsonline.co.uk\/blog\/wp-json\/wp\/v2\/posts\/115\/revisions"}],"predecessor-version":[{"id":128,"href":"https:\/\/stormappsonline.co.uk\/blog\/wp-json\/wp\/v2\/posts\/115\/revisions\/128"}],"wp:attachment":[{"href":"https:\/\/stormappsonline.co.uk\/blog\/wp-json\/wp\/v2\/media?parent=115"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/stormappsonline.co.uk\/blog\/wp-json\/wp\/v2\/categories?post=115"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/stormappsonline.co.uk\/blog\/wp-json\/wp\/v2\/tags?post=115"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}