Around the Bend (Sensor)
Tuesday Feb 26, 2008
So detecting when something bends can be important, right? Right. Don't ask me why, it just is. :-)
But how do you detect it? Well, with a bend sensor, or a flex sensor, depending on your point of view, of course!
So in preparation for JavaOne (ssssh ... it's a secret, but you might want to sign up for the Sun SPOTs Hands On Lab), I needed to play with a bend sensor and get it working. As we all know, hardware development is really tough with Sun SPOTs. :-)
So I got some of these bend sensors from Flexpoint to evaluate. They were inexpensive, and seemed like just what we needed. They arrived in yesterdays mail, and once I had been schooled by Arshan and one of our other Hardware geniuses, Del Peck, about what a voltage divider was, and what an op-amp was (turned out I didn't need the op-amp, even though I was able to pick up a couple at Radio Shack for next to nothing) I was able to get started.
Turns out "getting started" and "done" were only separated by about 2 minutes. Seriously. Once I understood what had to be done in terms of wiring the thing up, that is. I got a very helpful email from Del saying:
Great!
Let's try the 33K resistor and sensor only first.
We will use the +3V and GND on the eDEMO board.
Connect one side of the 33K to +3V.
Connect the other side of the 33K to the bend sensor.
Connect the other side of the bend sensor to GND.
Connect the mid-point of the network (33K to sensor joint) to an analog
input like A1.
You can use a volt meter to see what happens (measure at the analog input).
So that's exactly what I did. First with the Volt Meter, then with my breadboard. It looks like this (sorry about the picture, I took it with my iPhone because I can't find the cable to download pictures off my real camera):

Next was a little Java code. And I do mean a little Java code. I used NetBeans, and the Sun SPOT Modules for NetBeans, to create a new project. and added the following lines:
IScalarInput bendy = EDemoBoard.getInstance().getScalarInputs()[EDemoBoard.A3];
while (true) {
try {
System.out.println("Bendy: " + bendy.getValue());
} catch (IOException ex) {
ex.printStackTrace();
}
try {
Thread.sleep(250);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
When I ran this code, guess what? I got output every .25 seconds, and as I bent the sensor, the values changed depending on which way I bent the sensor.
That's it. 2 minutes. And I had to actually type 4 Java statements (NetBeans filled in the rest for me).
Next I'll pretty it up some, and make it more portable, and make the Java code do something more useful. But the basics of getting the bend sensor working was, as usual, alarmingly easy.
[ The road to hell is paved with good intentions. And littered with sloppy analysis! ]











