/* RESCUE ROBOTICS WORKSHOP Save to a file Introduction to File IO Ron Kessler Created 8/17/2018 Updated: 8/17/2018 Did final compile/test. Saves one value of data to the NXT File (test.txt). PURPOSE: While the datalogger is good for real time sensor testing, you may need to save data in your own format. This project shows how to save a reading to a file on the robot. Use the companion project called ReadFromFile to retrieve that one piece of data. SETUP: You will see how to save a number to file, and determine if there was an error or not. We will start by defining the file and some other parameters the NXT needs. We will use "IOException" (exception = error) to see if there was any problem. YOU ALWAYS DO THIS WITH FILE IO, BLUETOOTH, OR WI-FI COMMUNICATIONS. */ task main() { //---STEP 1: Define the stuff we need to access this file TFileHandle fileNumber; //internal number used to locate the file. TFileIOResult IOException; //tells us if there is an error during the IO string myFileName = "test.txt"; //my file name. int fileSize = 100; //same as when we saved 2 file short data2Save = 15; //pretend this is data from a sensor. //---STEP 2: Tell the robot to delete the old file to start clean. Delete("test.txt",IOException); //don't do this if you want to append to existing data! //---STEP 3: Now tell the NXT we want to save/write data to the file. // Send it the 4 pieces of data (arguments) it needs in the parentheses. OpenWrite(fileNumber, IOException, myFileName, fileSize); //creates the file if it doesn't exist //---STEP 4: MAKE IT SO! This takes value in data2Save and writes it to the file. WriteByte(fileNumber, IOException, data2Save); //---STEP 5: ALWAYS CLOSE THE FILE OR YOU WON'T BE ABLE TO ACCESS IT AGAIN. Close(fileNumber, IOException); //---STEP 6: Check if there was an error. If IOException = 0 then everything is fine. Otherwise no. nxtDisplayCenteredTextLine(1, "Rescue Robotics"); nxtDisplayCenteredTextLine(2, "Saving data"); if(IOException == 0) nxtDisplayTextLine(5, "Data was saved!"); else nxtDisplayTextLine(5, "Error while saving...."); wait1Msec(5000); //let us see the result for a few seconds }