Loading...
Logo
Processing Forum
Hey,

I'm trying to get my arduino (link) sent data to processing which logs the data. I do this because arduino can't log the data itself. The data that is sent has a resolution of 10bits, so I use an integer (16bits). I divide this integer into two bytes because of the protocol. Processing has to paste these two bytes to eachother to give me my correct data.

The problem I have in these programs is that processing creates a file, but it stays empty. I allready checked that arduino is sending data to processing so I think the problem is in processing. Does anybody see the problem?

ArduinoProgram:
Copy code
  1. int sensePin=0;
  2. int x;
  3. unsigned long current_time;
  4. unsigned long previous_time;
  5. int time_stamp;
  6. void setup()
  7. {
  8.   Serial.begin(115200);
  9.   previous_time = micros();
  10. }
  11. void loop()
  12. {
  13.   x=analogRead(sensePin);
  14.   current_time = micros();
  15.   time_stamp = int(current_time - previous_time);
  16. //  while(time_stamp<500) {
  17. //    current_time = micros();
  18. //    time_stamp = int(current_time - previous_time);
  19. //  }
  20.   previous_time = current_time;
  21.   Serial.write(255);
  22.   Serial.write(lowByte(x));
  23.   Serial.write(highByte(x));
  24.   Serial.write(lowByte(time_stamp));
  25.   Serial.write(highByte(time_stamp));
  26. }

ProcessingProgram:
Copy code
  1. import processing.serial.*;
  2. import java.util.Date;
  3. Serial myPort;
  4. FileWriter file;
  5. String filename;
  6. void setup()
  7. {
  8.  
  9.   myPort = new Serial(this, "COM4", 115200);
  10.   long thisDate = new Date().getTime();
  11.   filename =  "output-" + thisDate + ".txt";
  12.   
  13.   try {
  14.        String emptyStart = "";
  15.        file = new FileWriter(filename, false); // Wtrite empty string to file to clear it on start
  16.        file.write(emptyStart, 0, emptyStart.length()); //(string, start char, end char)
  17.        //file.close();
  18.      } 
  19.        catch(Exception e) 
  20.      { 
  21.        println("Error: Can't open file!");
  22.      }
  23.   }
  24. void draw()
  25. {
  26.   readSerial();
  27. }
  28. void readSerial()
  29. {
  30.   println(myPort.available());
  31.   if (myPort.available() > 5) {
  32.     int serial_in = myPort.read(); // read a byte from the Serial Port
  33.     while (serial_in != 255) {
  34.       serial_in = myPort.read(); // read until value 255 is read
  35.     }
  36.     int lowByte = myPort.read();
  37.     int highByte = myPort.read();
  38.     int sample = highByte * 256 + lowByte;
  39.     lowByte = myPort.read();
  40.     highByte = myPort.read();
  41.     int timestamp = highByte * 256 + lowByte;
  42.     myPort.clear();
  43.   }
  44. }
  45.  
  46.     void keyPressed(){
  47.       if ( key == 's' ){
  48.          try {
  49.            file.close();
  50.          } catch (IOException e) {
  51.            System.err.println(e);
  52.          }
  53.          exit();
  54.       }
  55.     }
Thanks a lot for helping me,
It's about my first experience with this stuff

Replies(2)

i don't have an arduino at hand right now to fully test this, but for one you don't actually write any data to the file in readSerial().
You're right, we fixed the program. Thanks for the tip :) I didn't understand every line