/*------------------------------------------------ Write strings to log file example program by techbricks.nl This program creates a file named "tachometer0-0.csv" (csv file, comma seperated file) If this file already exists it creates a file that not exists (increments the file number) "tachometer-0.csv" This file is 1024 bytes long. You can write strings to this file. Every string (line) is ended with a EOL (end of line). When this file is full a new file is created. The name of that file is "tachometer-.csv" (the file part number is incremented) This program will continue writing to file and creating new files until you press the orange button or the NXT runs out of memory. Important: If your NXT memory is full, use the Bicxcc file explorer to (re)move the files from your NXT!! 24 september 2009 NXC (build on NXC/NBC firmware version 1.28, Bricxcc 3.3 - build 3.3.7.20, check http://bricxcc.sourceforge.net/nbc/ for more info) Read more about it at www.techbricks.nl/My-NXT-projects/nxttachometerspeedcomputer.html Use this demo program to display digits for your own applications.... -------------------------------------------------*/ unsigned int result; byte fileHandle; short fileSize; short bytesWritten; string read; string write; int filenumber,filepart; string filename,sfilenumber,sfile,sfilepart; /*------------------------------------------------- Create a log file named "tachometer0-0.csv". If a file with that name already exists then it creates an other file. It increments the file number: "tachometer-0.csv" This file is 1024 bytes long. ---------------------------------------------------*/ void InitWriteToFile() { filenumber=0; filepart=0; filename="tachometer0-0.csv"; result=CreateFile(filename, 1024, fileHandle); // check if the file already exists while (result==LDR_FILEEXISTS) { CloseFile(fileHandle); filenumber=filenumber+1; filename=NumToStr(filenumber); filename=StrCat("tachometer",filename,"-0.csv"); result=CreateFile(filename, 1024, fileHandle); } // play a tone every time a file is created PlayTone(TONE_B7, 5); write = "RPM (last second),RPM (last minute)"; WriteLnString(fileHandle,write, bytesWritten); } /*------------------------------------------------- You can write strings to this file. Every string (line) is ended with a EOL (end of line). When this file is full a new file is created. The name of that file is "tachometer-.csv" (the file part number is incremented) ---------------------------------------------------*/ void WriteToFile(string write) { // write string to file result=WriteLnString(fileHandle,write, bytesWritten); // if the end of file is reached, close the file and create a new part if (result==LDR_EOFEXPECTED) { // close the current file CloseFile(fileHandle); // create the next file name filepart=filepart+1; sfilenumber=NumToStr(filenumber); sfilepart=NumToStr(filepart); // filename = "tachometer"++"-"++".csv" filename=StrCat("tachometer",sfilenumber,"-",sfilepart ,".csv"); // delete the file if it exists DeleteFile(filename); // create a new file CreateFile(filename, 1024, fileHandle); // play a tone every time a file is created PlayTone(TONE_B7, 5); WriteLnString(fileHandle,write, bytesWritten); } } // Close the file void StopWriteToFile() { // close the file CloseFile(fileHandle); } task main() { string text,text1,text2; //Create a new file InitWriteToFile(); // Never ending loop until the orange button is pressed or the NXT runs out of memory while (true<>ButtonPressed(BTNCENTER,false)&&(FreeMemory()>=2000)) { // Create a random text text1=NumToStr(Random(9999)); text2=NumToStr(Random(9999)); text=StrCat(text1,",",text2,"," ); // write the text to a file. The text will be end with a EOL WriteToFile(text); } // close the file StopWriteToFile(); }