Assignment: Sensing PART II: Force sensors and photocells
Collaborators: ljuba
Programming
I had a lot of trouble getting the force sensor to work properly. It activated with pressure, but the signal seemed to be interrupted several times per second. As a result, I wasn't able to get a smooth visualization. Also it wasn't clear to me how to incorporate the draw() function with the serial signal from the Ardolino board so that I could match the data. Instead, I practiced with many of the processing.org examples and have become familiar with how to draw with processing.
/*
* Arduino Ball Paint
* (Arduino Ball, modified 2008)
* ----------------------
*
* Draw balls randomly on the screen, size controlled by a device
* on a serial port. Press space bar to clear screen, or any
* other key to generate fixed-size random balls.
*
* Receives an ASCII number over the serial port,
* terminated with a carriage return (ascii 13) then newline (10).
*
* This matches what Arduino's " Serial.println(val)" function
* puts out.
*
* Created 25 October 2006
* copyleft 2006 Tod E. Kurt http://todbot.com/
*/
import processing.serial.*;
// Change this to the portname your Arduino board
String portname = "/dev/tty.usbserial-A70061C0"; // or "COM5"
Serial port;
String buf="";
int cr = 13; // ASCII return == 13
int lf = 10; // ASCII linefeed == 10
void setup() {
size(300,300);
frameRate(10);
smooth();
background(40,40,40);
noStroke();
port = new Serial(this, portname, 9600);
}
void draw() {
}
// draw balls
void drawball(int r) {
for (int i=0; i<100; i++ ) {
fill(255-i,i,240);
rect(r,r,30,30);
}
}
// called whenever serial data arrives
void serialEvent(Serial p) {
int c = port.read();
if (c != lf && c != cr) {
buf += char(c);
}
if (c == lf) {
int val = int(buf);
println("val="+val);
drawball(val);
buf = "";
background(40,40,40); // erase screen
}
}
Mechanical
One of the most interesting uses of the force sensor I came up with was to put it under my pillow to measure the restfulness of my sleep. I assume that restful sleep results in little head movement resulting in a consistent pressure reading over time. When I switch positions, my head would exert additional force on the pillow until I find a new comfortable position, changing the pressure sensor readings. This seems like a very easy way to track your sleep patterns over the course of the night.