We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I'm using Processing along with an Arduino sketch to help me save data from a sensor. However, I'm trying not to write that data to a text file (hold.txt) until another text file (record.txt) is created. I'm using exists() but it isn't working. Any help would be greatly appreciated! Here's parts of my code...
PrintWriter output;
File f = new File(dataPath("record.txt"));
.......
void setup() {
output = createWriter("hold.txt");
.......
void serialEvent(Serial port) {
while(port.available() > 0) {
int ch = port.read();
}
if (f.exists()) {
output.print((char)ch); //add to hold.txt
}
The files all get created, and I know the serial communication works. But for some reason, I never get the "I found it" message. Am I not using dataPath correctly? Or is it something with exists()? Also, when I create record.txt I do so in the same folder that the code is saved to.
Answers
http://forum.processing.org/two/discussion/8045/how-to-format-code-and-text
Sorry I didn't format it! Now it is legible.
The code is not legible. Use this link to format your code.
Anything that depends on sketch's path, like dataPath() or canvas' dimensions like width & height, should be after setup() starts! :-&
So instead of
File f = new File(dataPath("record.txt"));
outside setup()...Move the initialization part into setup() after size():
Thanks for the suggestion, but I've tried that and it still doesn't work. I made an attempt to switch it up a little bit (just because), and am still having no luck. Now the setup for f = new File(dataPath("record.txt")); is in the setup()
Show new code.
Note that the tight loop you show can use 100% CPU... Perhaps add a delay() call inside it.
I added a delay, still with no luck though. Here's my current code (minus a few variable definitions and the serial port stuff)...
Mmm, by delay, I meant a call to delay(), like
delay(100); // A tenth of second
But the reference of this function disappeared... I generally tell to avoid using it, but in some cases like here, it can make sense.
Your hand-made delay suffers of the same tight loop problem than your original code... It swamps the CPU and doesn't return control to the system (at least, in its thread).
That said, I don't even understand what you try to do in this loop. Is the file created by another program?
Note about your createWrite(): without a path, you don't know where the file will end... You should use dataPath() or sketchPath here too.
PhiLho, I have a MATLAB program that will be creating "record.txt". As for a call to delay, is that not what I'm doing?
With createWriter(), I know the path to the file will be wherever the sketch is saved. I can't find anything on that fully describes what dataPath and sketchPath do. Could you shed some light on them?
delay() is a built-in function of Processing, no need to define your own.
Should you supply it yourself, you better call Thread.sleep() (but you need to wrap it in a try / catch structure).
And yes, you are right, I mixed up createWriter() (Processing function that know about sketch path) and another Java function (that doesn't know about it).
dataPath / sketchPath: there used to be a JavaDoc for Processing, but I don't know if it is still available. Otherwise, the best information is in the source of the PApplet class...