Skip to content

Instantly share code, notes, and snippets.

@bakercp
Created October 20, 2016 14:03
Show Gist options
  • Save bakercp/bc9a4e052624709266d8ae3ef1f3b0da to your computer and use it in GitHub Desktop.
Save bakercp/bc9a4e052624709266d8ae3ef1f3b0da to your computer and use it in GitHub Desktop.
A simple CSV data logger for Processing.
import processing.serial.*;
import java.text.SimpleDateFormat;
import java.util.Date;
Serial myPort;
int lineFeedChar = 10;
String lastRow = "";
int currentY = 0;
PrintWriter output;
void setup()
{
size(800, 800);
background(0);
String portName = Serial.list()[0];
myPort = new Serial(this, portName, 9600);
myPort.bufferUntil(lineFeedChar); // Call serialEvent only when marker is reached.
output = createWriter("data_" + getDateStamp() + ".csv");
}
void serialEvent(Serial p) {
String row = getDateStamp() + p.readString();
output.println(row);
lastRow = row;
}
void draw()
{
if (lastRow.length() > 0)
{
if (currentY > height)
{
background(0);
currentY = 0;
}
text(lastRow, 0, currentY);
lastRow = ""; // Clear because we rendered it.
currentY += 12;
}
}
String getDateStamp()
{
Date timestamp = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
return dateFormat.format(timestamp) + "," + timeFormat.format(timestamp);
}
void dispose()
{
println("Closing");
output.flush(); // Writes the remaining data to the file
output.close(); // Finishes the file
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment