Created
October 20, 2016 14:03
-
-
Save bakercp/bc9a4e052624709266d8ae3ef1f3b0da to your computer and use it in GitHub Desktop.
A simple CSV data logger for Processing.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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