User Tools

Site Tools

// Original by Nick
// mod by nao 2010
#include <SoftwareSerial.h>
SoftwareSerial mon=SoftwareSerial(2,3);

int incomingByte=0;
int fileNumber=1;

int noOfChars;
long int valToWrite;
char activityToLog;
long int x;
long int startLogTime = 0;
byte y=0;
void setup() {
  	               // opens serial port, sets data rate to 9600 bps
  Serial.println("IPA");                 // sets the vdip to use ascii numbers 
  mon.print("IPA");                                     //(so I can read them in the code easily!)
  Serial.print(13, BYTE);  // return character to tell vdip its end of message
  mon.print(13, BYTE);

void loop() {
  if (Serial.available() > 0) {    // read the incoming byte
    incomingByte =;
    if (incomingByte=='1'){                      // if it receives a 1
      Serial.print("OPW LOG%");                  // open to write creates a file - named
      Serial.print(fileNumber);                  // LOG%1.TXT first time round - .TXT is for the computer
      Serial.print(".TXT");                      // I have used the % sign in the name so I can 
                                             //search the disk for log files that it has created 
                                             //so as not to overwrite any that may be on already
      Serial.print(13, BYTE);                    // return character

    if (incomingByte=='2'){                      // if it receives a 2
      Serial.print("CLF LOG%");     // it closes the file
      Serial.print(fileNumber);     // LOG%1.TXT
      Serial.print(13, BYTE);       // return character


    if (incomingByte=='3'){                      // if it receives 3
      Serial.print("DIR");          // lists the directory on the memory stick
      Serial.print(13, BYTE);       // which will now include the file LOG%1.TXT

    if (incomingByte=='5'){
      activityToLog='P';                  // I will be using different letters to label my time stamps
      valToWrite=millis() - startLogTime; // time since we started logging
      x=valToWrite;                       // need to copy valToWrite as getting no of characters will consume it
      while (x>= 10){                     // counts the characters in the number
        noOfChars++;                      // thanks to D Mellis for this bit
      noOfChars +=2;                      //add 2 to the num as will also write the letter P and a return character

      Serial.print("WRF ");               //write to file (file needs to have been opened to write first)
      Serial.print(noOfChars);            //needs to then be told how many characters will be written
      Serial.print(13, BYTE);             //return to say command is finished
      Serial.print(activityToLog);        //followed by the info to write
      Serial.print(13, BYTE);             //write a return to the contents of the file (so each entry appears on a new line)
    if (incomingByte=='6'){                          
      fileNumber++;                                    //so we can create other files

    if (incomingByte=='7'){ 
      if (fileNumber>0){                               //and look at previous ones

    if (incomingByte=='8'){                        
      Serial.print("RD LOG%");            //reads the contents of named file
      Serial.print(13, BYTE);
    if (incomingByte=='9'){   
      startLogTime=millis();                          // reset timing
prensf2010/basicusbwriter.txt · Last modified: 2010/05/24 19:41 by nels_oscar