Buffer file from Arduino Serial bytes (total newbie).

edited May 2016 in Arduino

Hello,

I am trying to create a circular buffer (in Processing) from the Arduino serial data. The circular buffer (an int 1D array) is periodically saved to a text file (by Processing) which can be read with an other program (Matlab).

full workflow:

analog input --> Arduino's 10bits ADC -->Serial.write(1200) --> Processing creates a circular buffer with byte (array of bytes) --> Processing save the array into text file --> text file analysed by Matlab.

The system works poorly: The buffers files are saved way too slowly! It feels like there is a bottleneck somewhere.

When ever I change the input signal, it take a lot of time before my buffer text files are updated.

It is my first day coding in JAVA and I have been using Arduino for less than a week so I am a total noobie here! I would very much appreciate if some one could help me out. I am only used to Matlab and I have never done anything like this before so forgive my horrible coding!

Any advice would be welcome! I have also attached my codes bellow.

Thanks and have a great day!

Here are the codes:

Arduino code workflow

1)arduino receives an analog signal in pin A0 (a photoresistor for the moment).

2)arduino sends every bytes one by one to Processing.

int thebyte=0;
int pin=A0;

void setup() {
  Serial.begin(1200);
  pinMode(pin,INPUT);
}

void loop() {
thebyte=analogRead(pin)/4;
Serial.write(thebyte);
delay(1);
}

Processing code workflow

1)setup an array for buffer, open serial port to Arduino 2)save the last received byte to the buffer last position (circular buffer: when buffersize is reach, the new bytes are written at the beginning of the array).

3)when Processing receives 1200 bytes (0.5 buffer size) or 2400 (full buffer size), the array is saved.

Variable "tic" (goes from 1 to 5) is here to save the arrays in different files so my matlab can load the last one without interfering with the writing.

int bufferSize=2400;
int[] buffer;
int counter=0;
int tic=1;

void setup() {
  printArray(Serial.list());
  myPort= new Serial(this, Serial.list()[0], 1200);
  buffer = new int[bufferSize];
  println("start");
}


void draw() {
  if (counter==bufferSize) {counter=0;}

//add bytes to the array. 
  if (myPort.available()>0) {
    mybyte= myPort.read();
    buffer[counter]=mybyte;

  } 
  else {println("port error");}

//print the full array in "1,2,3,4,5.txt".
  if (counter==bufferSize-1 || counter==bufferSize/2){ //println(counter);
      tic++;  
  if (tic==6){tic = 1;} //Six different files, so matlab can load them without interfering.
String filename = new String (String.valueOf(tic) + ".txt");
PrintWriter output;
output = createWriter(filename); 

for (int i = 0; i < bufferSize; i++) {
output.println (buffer[i]);
}

output.flush(); // Writes the remaining data to the file
output.close(); // Finishes the file


}
  counter++;
  delay(1);
}

Matlab workflow

1)matlab loads the last modified text file

(among 1.txt, 2.txt, 3.txt, 4.txt, 5.txt);

2)matlab can do some computations on it

This discussion has been closed.