This week’s assignment for Crafting with Data was
Use an Arduino to gather 500 samples of interesting data in two different conditions (1000 samples total). Post your data online so it can be downloaded.
Now, see, I did this assignment uncharacteristically promptly—days ago!—without having noticed the adjective interesting, so originally what I had was some profoundly dull numbers from a thermistor, measured (a) under my fluorescent proofreading lamp and (b) while I held the sensor between my fingers. You will thank me, therefore, for rereading the assignment, noting my error, and, at the last minute, using the Pong controller I made for Understanding Networks to gather some slightly less dull information from the x axis of an accelerometer: (a) walking on level floor, and (b) climbing up and down stairs.
Now, with charts!
The guts of my beautiful data collection device look like this:
(Click the photo for more details at Flickr.)
I used two Processing sketches—for the thermistor data, Rob’s code; for the accelerometer data, a quick-and-dirty mod of the almost wholly unoriginal code I used for the Understanding Networks assignment.
[java]
/*
Serial String Reader + Simple net Client
Language: Processing
Reads in a string of characters from a serial port until
it gets a linefeed (ASCII 10). Then splits the string into
sections separated by commas. Then converts the sections to ints,
and prints them out.
* Starts a network client that connects to a server on port 8080,
* sends any keystrokes pressed.
created 2 Jun 2005
modified 6 Aug 2008
by Tom Igoe
modified 24 September 2009 by India Amos
*/
import processing.serial.*; // import the Processing serial library
import processing.net.*; // import the Processing networking library
Serial myPort; // The serial port
Client myClient; // instance of the net Client
String data; // string to hold incoming data
String ipAddress = “128.122.151.178”; // address of the server
String myKey1; // the virtual keypress generated by the x-pin of the accelerometer
String myKey2; // the virtual keypress generated by the y-pin of the accelerometer
String myKey3; // the virtual keypress generated by the switch
String myKeyBuffer; // all the virtual keys pressed by a single bleat from the Arduino
float bgcolor; // Background color
void setup() {
// establish the background and foreground:
size(200, 200);
background(50);
fill(200);
// Connect to server on port 8080
myClient = new Client(this, ipAddress, 8080);
// List all the available serial ports
println(Serial.list());
// I know that the first port in the serial list on my mac
// is always my Arduino module, so I open Serial.list()[0].
// Change the 0 to the appropriate number of the serial port
// that your microcontroller is attached to.
myPort = new Serial(this, Serial.list()[0], 9600);
// read bytes into a buffer until you get a linefeed (ASCII 10):
myPort.bufferUntil(‘\n’);
}
void draw() {
}
// serialEvent method is run automatically by the Processing applet
// whenever the buffer reaches the byte value set in the bufferUntil()
// method in the setup():
void serialEvent(Serial myPort) {
// read the serial buffer:
String myString = myPort.readStringUntil(‘\n’);
// if you got any bytes other than the linefeed:
if (myString != null) {
myString = trim(myString);
// split the string at the commas
// and convert the sections into integers:
int sensors[] = int(split(myString, ‘\t’));
// print out the values you got:
for (int sensorNum = 0; sensorNum < sensors.length; sensorNum++) {
print("Sensor " + sensorNum + ": " + sensors[sensorNum] + "\t");
}
// add a linefeed after all the sensor values are printed:
println();
// Convert integers into behavior.
// A change of +/-50 on sensor 1 means a roll forward (down) or backward (up), respectively. Flat is 520.
// A change of +/-50 on sensor 2 means a tilt coounter-clockwise or clockwise, respectively. Flat is 505.
myKey1="";
myKey2="";
myKey3="";
if (sensors[0] > 570) // rolling forward, so paddle down
{
myKey1 = “d”;
}
if (sensors[0] < 470) // rolling backward, so paddle up
{
myKey1 = "u";
}
if (sensors[1] > 555) // rolling counter-clockwise, so paddle left
{
myKey2 = “l”;
}
if (sensors[1] < 455) // rolling clockwise, so paddle right
{
myKey2 = "r";
}
if (sensors[3] > 0 ) // button pressed
{
myKey3 = “x”;
}
myKeyBuffer = myKey1+myKey2+myKey3;
println(myKeyBuffer);
// If there’s incoming data from the client:
if (myClient.available() > 0) {
// get the data:
data = myClient.readString();
println(data);
}
if (myKeyBuffer != null) {
// send out anything that’s typed:
myClient.write(myKeyBuffer);
}
if (keyPressed) {
// send out anything that’s typed:
myClient.write(key);
}
}
}
[/java]