Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
You want to create a file containing information received over the serial port from Arduino. For example, you want to save the values of the digital and analog pins at regular intervals to a logfile.
We covered sending information from Arduino to your computer in previous recipes. This solution uses the same Arduino code explained in Section 4.10. The Processing sketch that handles file logging is based on the Processing sketch also described in that recipe.
This Processing sketch creates a file (using the current date and time as the filename) in the same directory as the Processing sketch. Messages received from Arduino are added to the file. Pressing any key saves the file and exits the program:
/*
* ReceiveMultipleFieldsBinaryToFile_P
*
* portIndex must be set to the port connected to the Arduino
* based on ReceiveMultipleFieldsBinary, this version saves data to file
* Press any key to stop logging and save file
*/
import processing.serial.*;
PrintWriter output;
DateFormat fnameFormat= new SimpleDateFormat("yyMMdd_HHmm");
DateFormat timeFormat = new SimpleDateFormat("hh:mm:ss");
String fileName;
Serial myPort; // Create object from Serial class
short portIndex = 0; // select the com port, 0 is the first port
char HEADER = 'H';
void setup()
{
size(200, 200);
// Open whatever serial port is connected to Arduino.
String portName = Serial.list()[portIndex];
println(Serial.list());
println(" Connecting to -> " + Serial.list()[portIndex]);
myPort = new Serial(this, portName, 9600);
Date now = new Date();
fileName = fnameFormat.format(now);
output = createWriter(fileName + ".txt"); // save the file in the sketch folder
}
void draw()
{
int val;
String time;
if ( myPort.available() >= 15) // wait for the entire message to arrive
{
if( myPort.read() == HEADER) // is this the header
{
String timeString = timeFormat.format(new Date());
println("Message received at " + timeString);
output.println(timeString);
// header found
// get the integer containing the bit values
val = readArduinoInt();
// print the value of each bit
for(int pin=2, bit=1; pin <= 13; pin++){
print("digital pin " + pin + " = " );
output.print("digital pin " + pin + " = " );
int isSet = (val & bit);
if( isSet == 0){
println("0");
output.println("0");
}
else {
println("1");
output.println("0");
}
bit = bit * 2; // shift the bit
}
// print the six analog values
for(int i=0; i < 6; i ++){
val = readArduinoInt();
println("analog port " + i + "= " + val);
output.println("analog port " + i + "= " + val);
}
println("----");
output.println("----");
}
}
}
void keyPressed() {
output.flush(); // Writes the remaining data to the file
output.close(); // Finishes the file
exit(); // Stops the program
}
// return the integer value from bytes received on the serial port (in low,high
order)
int readArduinoInt()
{
int val; // Data received from the serial port
val = myPort.read(); // read the least significant byte
val = myPort.read() * 256 + val; // add the most significant byte
return val;
}
Don’t forget that you need to set portIndex to the serial port connected to Arduino.
The base name for the logfile is formed using the DateFormat function in Processing:
DateFormat fnameFormat= new SimpleDateFormat("yyMMdd_HHmm");The full filename is created with code that adds a directory and file extension:
output = createWriter(fileName + ".txt");
The file will be created in the same directory as the Processing sketch (the sketch needs to be saved at least once to ensure that the directory exists). createWriter is the Processing function that opens the file; this creates an object (a unit of runtime functionality) called output that handles the actual file output. The text written to the file is the same as what is printed to the console in Section 4.10, but you can format the file contents as required by using the standard string handling capabilities of Processing. For example, the following variation on the draw routine produces a comma-separated file that can be read by a spreadsheet or database. The rest of the Processing sketch can be the same, although you may want to change the extension from .txt to .csv:
void draw()
{
int val;
String time;
if ( myPort.available() >= 15) // wait for the entire message to arrive
{
if( myPort.read() == HEADER) // is this the header
{
String timeString = timeFormat.format(new Date());
output.print(timeString);
val = readArduinoInt(); // read but don't output the digital values
// output the six analog values delimited by a comma
for(int i=0; i < 6; i ++){
val = readArduinoInt();
output.print("," + val);
}
output.println();
}
}
}For more on createWriter, see http://processing.org/reference/createWriter_.html.