write data from serial port to a text file

edited May 2015 in Arduino

Hi, I have to write data received from an accelerometer adxl337 connected to arduino to a text file using processing. I receive the right data on serial monitor from arduino and I succeed to receive them on processing console. Text file is created, but it contains only one value (just a single zero) but it has to contain many other values and I really don't know what to do or modify to receive more data. Here is my arduino code: int pinX = A0; int pinY = A1; int pinZ = A2;

int counterX=0; int counterY=0; int counterZ=0;

int active = 0;

int reads_nr = 0; unsigned long previousMillis = 0; long interval = 2000;//ms const int max_reads_nr = 10;

void setup() { Serial.begin(9600); }

void loop() { unsigned long currentMillis = millis();

if(currentMillis - previousMillis > interval){ previousMillis = currentMillis;

if(reads_nr <= max_reads_nr){
int valueX = analogRead(pinX);
int valueY = analogRead(pinY);
int valueZ = analogRead(pinZ);

if(valueX > 262)
  counterX++;

if(valueY > 332)
  counterY++;

if(valueZ > 351)
  counterZ++;

if((counterX >= 5) && (counterY >= 5) && (counterZ >= 5)){
  active = 1;
  Serial.print(active);
 //Serial.print(' ');

}
else{
  active = 0;
  Serial.print(active);
  //Serial.print(' ');
}

}

reads_nr++; } if(reads_nr > max_reads_nr){ active = 0; counterX = 0; counterY = 0; counterZ = 0;
reads_nr = 0; }

}

and this is my processing code:
import processing.serial.*;

Serial port; // The serial port object

PrintWriter output; void setup() { size(200,200); // In case you want to see the list of available ports //println(Serial.list());

// Using the first available port (might be different on your computer) port = new Serial(this, Serial.list()[0], 9600); }

void draw() {

}

// Called whenever there is something available to read void serialEvent(Serial port) {

String val = port.readString(); String[] valArray = splitTokens(val, " ");

saveStrings( "test.txt", valArray );

//debugging // println( "Raw Input:" + val); }

Thanks in advances!

Tagged:

Answers

Sign In or Register to comment.