Motion Capture System

int noOfChars=0;  //the number of degits per a saved line when writing to USB
long int valToWrite=0;  //the value which gets writen to USB
long int x=0;  //transfer variable for a loop control
long int count = 0;  //the number of "frames recorded" or files writen

int btnPin = 10;  //the input pin for the on/off record button
boolean buttonOn = false;  //initiate the button to be off

int L_L = 5;  //left hip lateral sensor
int L_D = 4;  //left hip dorsal sensor
int L_K = 2;  //left knee sensor
// right side
int R_L = 0;  //right hip lateral sensor
int R_D = 1;  //right hip dorsal sensor
int R_K = 3;  //right hip knee

// carier values
int L_LV = 0;  //the value of the left hip lateral sensor
int L_DV = 0;  //the value of the left hip dorsal sensor
int L_KV= 0;  //the value of the left knee sensor
int R_LV = 0;  //the value of the right hip lateral sensor
int R_DV = 0;  //the value of the right hip dorsal sensor
int R_KV = 0;  //the value of the right knee sensor

void setup() 
{ 
      Serial.begin(9600);                    // opens serial port, sets data rate to 9600 bps
        Serial.print("IPA");                  // sets the vdip to use ascii numbers (so I can read them in the code easily!)
        Serial.print(13, BYTE);               // return character to tell vdip its end of message
        //pinMode(13, OUTPUT);                  //return character
        
        pinMode(L_L, INPUT);                  //set the left lateral sensor to input
        pinMode(L_D, INPUT);                  //set the left dorsal sensor to input
        pinMode(L_K, INPUT);                  //set the left knee sensor to input
        pinMode(R_L, INPUT);                  //set the right lateral sensor to input
        pinMode(R_D, INPUT);                  //set the right dorsal sensor to input
        pinMode(R_K, INPUT);                  //set the right knee sensor to input
        
        pinMode(btnPin, INPUT);               //set the on/off record button to input
}
long int charsInVar(long int i)
{
  long int r = 1;
  long int temp;
  temp = i;
  while (temp >= 10)  //tanks to d mellis and nick for this bit
  {
  r++;
  temp/=10;
  }

return r;
}
void loop() 
{ 
    if(digitalRead(btnPin) == HIGH ) //if the button is on start "recording"
  {
// read the analog values ( 0 - 1023)
// our sensors non-bent values vary based on wire length but they seem to hover around 370 for short wires, to 400 and something for long wires
  
  // the left angle values
  L_LV = analogRead(L_L);
  L_DV = analogRead(L_D);
  L_KV = analogRead(L_K);
  // the right angle values
  R_LV = analogRead(R_L);
  R_DV = analogRead(R_D);
  R_KV = analogRead(R_K);
  //now that our angle values are in memory open a file and write them to the usb
  
  delay(6000);
//open a file
  Serial.print("OPW f");                  // open to write creates a file - named frame
  Serial.print(count);                  // add an incremental frame number to the record
  Serial.print(".TXT");                      // call the file a txt for ease of use
  Serial.print(13, BYTE);                    // return


//write too the file
 //valToWrite = valToWrite + 1;                       // the value we want to log
  //x=valToWrite;                       // need to copy valToWrite as getting no of characters will consume it
  noOfChars=6;                        //we are going todo 6 lines so we start with 6 return characters always, so we need calculate the amount to write for the sensor values

  noOfChars+=charsInVar(L_LV);        //add in each sensor value
  noOfChars+=charsInVar(L_DV);        //add in each sensor value
  noOfChars+=charsInVar(L_KV);        //add in each sensor value
  noOfChars+=charsInVar(R_LV);        //add in each sensor value
  noOfChars+=charsInVar(R_DV);        //add in each sensor value
  noOfChars+=charsInVar(R_KV);        //add in each sensor value
 //noOfChars +=1;                      //add 1 to the num as we will also write the 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(L_LV);            //actually save the file 
 Serial.print(13, BYTE);         //write a return
 Serial.print(L_DV);            
 Serial.print(13, BYTE);
  Serial.print(L_KV);            
 Serial.print(13, BYTE);
  Serial.print(R_LV);            
 Serial.print(13, BYTE);
  Serial.print(R_DV);            
 Serial.print(13, BYTE);
  Serial.print(R_KV);            
 Serial.print(13, BYTE);

 
 //close file              
  Serial.print("CLF f");           // it closes the file
  Serial.print(count);                 // close the current frames count
  Serial.print(".TXT");                //txt was for ease of use latter on
  Serial.print(13, BYTE);             // return character
  count= count + 1;                            // increment the count to add a new file
  }
  else  //if the button is off, reset the file count, to make sure we have a fresh record every time the system is used
    count = 0;
    valToWrite=100;
}

Carlo and Don