Motion Capture System

int noOfChars=0;        //the number of digits per a saved line when writing to USB
long int count = 1;     //the number of "frames recorded" or files writen
int x = 0;              //for when you really need an int.


boolean recording = false;  //initiate the button to be off

int btnPin = 10;  //the input pin for the on/off record button

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(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
}

//this function simplifies the calculation of characters for writing to the serial port.
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;
}
//  **MAIN LOOP**
void loop() 
{
  
if(digitalRead(btnPin) == HIGH ) //if the button is on do recording stuff
{
  //this little bit was recomended by jmknapp, to make sure that the buffer was emptied out each loop.. it seems to help a little bit. 
  if(x=Serial.available()>0)
  {
    while(x--)
    {
      Serial.read();
    }
  }
  
  if(!recording) //if the record btn is on, and we're not currently recording create a file to record in to.
  {
  //open a file
  Serial.print("OPW session");               // open to write creates a file
  Serial.print(count);                       // add an incremental number to the record
  Serial.print(".TXT");                      // call the file a txt for ease of use
  Serial.print(13, BYTE);                    // return
  recording = true;                          //set the record value to true so we don;t constantly create files
  delay(1000);                               //start after awhile to make sure we're not over powering the serial port durring a file creation(which seems to be easy to overpower)
  }
  //read and save the sensors!
  L_LV = analogRead(L_L);
  L_DV = analogRead(L_D);
  L_KV = analogRead(L_K);
  R_LV = analogRead(R_L);
  R_DV = analogRead(R_D);
  R_KV = analogRead(R_K);

  noOfChars=15;                        //we are always going todo 6 lines so we start with 6 return characters and six end-of-lines and then three more for our "sensor-set-break" character to register the frames with
  //add in each sensor value
  noOfChars+=charsInVar(L_LV);        
  noOfChars+=charsInVar(L_DV);        
  noOfChars+=charsInVar(L_KV);        
  noOfChars+=charsInVar(R_LV);        
  noOfChars+=charsInVar(R_DV);        
  noOfChars+=charsInVar(R_KV);        
  //doing writing stuff
 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
   delay(25);  //these periodic delays radicly effect the amount of data you can save, with the vdrive before it stops working.
 Serial.println(L_LV);             
   delay(25);
 Serial.println(L_DV);
   delay(25);
 Serial.println(L_KV);             
   delay(25);
 Serial.println(R_LV);
   delay(25); 
 Serial.println(R_DV);
   delay(25); 
 Serial.println(R_KV);
   delay(25);
 Serial.println("b");
   delay(25);
    }
  else  //if the button is off, do some house cleaning
    {
      if(recording)//if the btn is off and we're still recording then close the file and increment the counter
      {
         //close file
        Serial.print("CLF session");           // it closes the file
        Serial.print(count)
Serial.print(".TXT");                //txt was for ease of use latter on
        Serial.print(13, BYTE);              // return character
        recording = false;                  //finally done recording.
        count++;                 // increment the count to add a new file
      }
    }
}