again you try to save to file ?something?
in every draw loop ( 60 times per second )
without knowing that / what data have arrived.
only save to file if new data.
so move the
output.println(val);
out of draw and into serialEvent after
val = rx;
also that is the moment where you should print “val” content to console
so you see that/what data have arrived.
optional:
the concept to export to file with a header line
sounds more like you want/need to make a CSV file
so you should store the data into a
https://processing.org/reference/Table.html
and only on command ( keyboard?) do a onetime
https://processing.org/reference/saveTable_.html
and also this makes it possible to use the data ( from table ) inside the sketch
to draw something with it.