Output to .csv

edited November 2013 in Arduino

I'm trying to get processing to read a serial port on which I've collected data with an Arduino. I want this exported to a .csv file. I'm getting (and want) three outputs from the Arduino serial monitor, but processing gives me an array of several (usually about a 3x39). None of the numbers, or their sums are the values from Arduino. I'm using a win7 computer and a 2.1 version of processing.

Arduino code:

unsigned long sensorPin1 = A1;    //in line, sensor 2   (velocity sensor 1)
unsigned long sensorValue1 = 300;  //initial sensor value (Read)
unsigned long sensorValueold1 = 0;  //secondary sensor check (for comparison to determine if there was a change in value i.e object passed through)

unsigned long sensorPin2 = A2;    //in line, sensor 3 (velocity sensor 2)
unsigned long sensorValue2 = 300;    //initail sensor value (Read)
unsigned long sensorValueold2 = 0;  //secondary sensor check (for comparison to determine if there was a change in value i.e object passed through)

unsigned long sensorPin0 = A0;    //in line, sensor 1 (initial timing sensor for acceleration calculation)
unsigned long sensorValue0 = 0;  //initial sensor value (Read)
unsigned long sensorValueold0 = 0;  //secondary sensor check (for comparison to determine if there was a change in value i.e object passed through)

float timer0 = 0;  // time of object leaving sensor 1
float timer1 = 0;  // time of object passsing through sensor 2
float timer2 = 0;  // time of object passing through sensor 3

unsigned long timeElapsed1 = 0;    //time elapsed for velocity calculation
unsigned long timeElapsed2 = 0;  //time elapsed from leaving sensor 1 till passing through sensor 3
int counter0 = 0;
int counter1 = 0;      //to determine of sensor 2 has been tripped
int counter2 = 0;    //to determine of sensor 3 has been tripped

int trans1 = 13;    // initializes charging transistor
int buttonpress = 10;
int countend = 0;


void setup() {
  Serial.begin(115200);
  pinMode(trans1, OUTPUT);
  pinMode(buttonpress, INPUT);

}

void loop(){

  int buttonpressPin = digitalRead(buttonpress);    // checks button state
  //Serial.println(buttonpressPin);   // for testing, display button state (0, 1)
  digitalWrite(trans1, LOW);    // ensures the capacitor is not charging
  if (buttonpressPin == HIGH){    //checks to see if button has been pressed
    digitalWrite(trans1, HIGH);  //turns charging circuit on
    delay(15000);                // charges for 7 seconds, aprox 350 volts
    digitalWrite(trans1, LOW);  // turns the charging circuit back off

  }




  //Serial.println(sensorValue0);
  sensorValueold0 = sensorValue0;    //sets the old value of the sensor 1 reading to the new reading
  sensorValue0 = analogRead(sensorPin0);  //reads and resets the new value of sensor reading
  if (sensorValueold0 > sensorValue0+10 && counter0 == 0){  //compares the two readings   if sensor value changes, time stamp
    timer0 = micros();      // time stamp in microseconds
     //Serial.println(timer0);
    counter0++;
  }
  sensorValueold1 = sensorValue1;        // same as above for sensor 2
  sensorValue1 = analogRead(sensorPin1);
  if (sensorValueold1 < sensorValue1-15 && counter1 ==0){
    timer1 = micros(); 
     //Serial.println(timer1);
    counter1++;
  }
  sensorValueold2 = sensorValue2;      // same as above for sensor 3
  sensorValue2 = analogRead(sensorPin2);
  if (sensorValueold2 < sensorValue2-15 && counter2 == 0){
    timer2 = micros();
     //Serial.println(timer2);
    counter2++;
  }

//  Serial.print(sensorValue0);
//  Serial.print(",");
//  Serial.print(sensorValue1);
//  Serial.print(",");
//  Serial.println(sensorValue2);


  if (counter0 > 0 && counter2 > 0 && counter1 > 0 ){    //checks to make sure all three sensors have been tripped
    //timeElapsed1 = timer2 - timer1;    // Time used for velocity at sensor 3
    //timeElapsed2 = timer2 - timer0;    //Time used for acceleration between sensor 1 and sensor 3
    //  Serial.println(timeElapsed1);      //outputs time between sensor 2 and 3
    // Serial.println(timeElapsed2);      //outputs time between sensor 1 and 3
    /*timer0 = timer0/100;
    timer1 = timer1/100;
    timer2 = timer2/100;*/
    counter0 = 0;  
    counter1 = 0;              //resets counters and timers
    counter2 = 0;
    Serial.print(timer0);
    Serial.print(" , ");
    Serial.print(timer1);
    Serial.print(" , ");
    Serial.print(timer2);
    Serial.println();
    delay(5000); 
    int countend = 1; 
    timer0 = 0;
    timer1 = 0;
    timer2 = 0;  
}


  delay(.0001);
}

Processing code:

import processing.serial.*;

PrintWriter output;
Serial myPort;   
float[] serialInArray = new float[3];
float inByte = -1;  
float time0, time1, time2 = 0;
int serialCount = 0; 

void setup() {
  println(Serial.list());
  output = createWriter("time.csv"); 
  myPort = new Serial(this, Serial.list()[0], 115200); 
}

void draw () {              
  while (myPort.available () > 0) {
    float serialByte = myPort.read(/*float*/);
    processByte(serialByte);
    //output.println(time0 + "," + time1+ "," + time2);
    //output.println(norm(time0, 0, 1000000) + "," + norm(time1, 0, 1000000) + "," + norm(time2, 0, 1000000));
    //output.println(map(time0, 0, 9600, 0, 115200) + "," + map(time1, 0, 9600, 0, 115200) + "," + map(time2, 0, 9600, 0, 115200));

    //output.println(time0 + "," + time1 + "," + time2);    
    //output.println(map(time0, 0, 60, 0, 100000000) + "," + map(time1, 0, 60, 0, 100000000) + "," + map(time2, 0, 60, 0, 100000000));
    //long time0, time1, time2 = 0;
  }
}
/*void keyPressed() {
  output.flush(); 
  output.close();
  exit();
}*/

void processByte( float inByte) {  
  serialInArray[serialCount] = inByte;
  serialCount++;
  if (serialCount > 2 ) {
    time0 = serialInArray[0];
    time1 = serialInArray[1];
    time2 = serialInArray[2];
    output.println(time0 + "," + time1 + "," + time2);
    serialCount = 0;
  }
}
void keyPressed() {
  output.flush(); 
  output.close();
  exit();
}
Sign In or Register to comment.