Hi all,
I know this question pops up on the web a lot but so fa I havent’ been able to find one solution than works for me. I am a complete newbie to Java and Processing, so please be patient with me.
I have an Arduino Sketch that writes on the serial monitor a series on numbers (later I will need these to be readings from sensors but for the moment I’m practising on this)
void setup() {
// put your setup code here, to run once:
#include <Wire.h>
Serial.begin(9600);
Serial.println(“Arduino-Processing Communication Test”); Serial.println("");
}
void loop() {
// put your main code here, to run repeatedly:
int x = 0;
while(x<=20)
{
Serial.println(x);
x++;
delay(1000);
}
while(1){}
}
And this is my processing sketch that should be creating a .txt file with the data from Arduino:
//From Arduino to Processing to Txt.
//import
import processing.serial.*;
//declare
PrintWriter output;
Serial udSerial;
void setup() {
udSerial = new Serial(this, Serial.list()[0], 9600);
String filePath=“C:/Users/Beatrice/Documents/Tesi Magistrale/prova.txt”;//
output = createWriter (filePath);
}
void draw() {
// printArray(Serial.list()); // to check serial
if (udSerial.available() > 0) {
if(Serial.list()[0] == “COM3”) {
String SenVal = udSerial.readStringUntil(’\n’);
if (SenVal != null) {
output.println(SenVal);
}
}
}
}
void keyPressed(){
output.flush();
output.close();
exit();
}
My procedure is : plug in Arduino (the sketch is already one) and then run the Processing code for about 1 minute, then stop.
The file is correctly created in the specified location but remains empty. Using println(SenVal) as debugger I find out that actually the values from Arduino are never passed to Processing apparently (ie the console remains empty).
I also double checked my serial number, but apparently I only have COM3 as possibile option so there is no actual possibility to mistake that. The port is correct
Can somebody please help? Thanks