Assignment: Sensing PART II: Force sensors and photocells
Collaborators:
Part I: Programming
My code simply extends the ball-drawing program to take force sensor data and droaw randomly-sized and different-colored balls and squares.
/*
* Arduino Ball Paint
* (Arduino Ball, modified 2008)
* ----------------------
*
* Draw balls and squares 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 random balls and squares.
*
* 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 <tod@todbot.com
* http://todbot.com/
*
* Adapted October 2008
* by Connor Riley
*
*/
import processing.serial.*;
// Change this to the portname your Arduino board
String portname = "/dev/tty.usbserial-A4001nLJ"; // 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() {
}
void keyPressed() {
if(key == ' ') {
background(40,40,40); // erase screen
}
else {
int x = int(random(0,width));
int y = int(random(0,height));
int rad = int(random(0,255));
if(rad>128){
drawball(x,y,rad); //draw circles for large values
}else{
drawrect(x,y,rad); // draw rectangles for small values
}
}
}
// draw balls
void drawball(int x, int y, int r) {
for (int i=0; i<100; i++ ) {
fill(255-i,i,r);
ellipse(x,y,r,r);
}
}
void drawrect(int x, int y, int r) {
for (int i=0; i<100; i++ ) {
fill(i,255-i,r);
rect(x,y,r,r);
}
}
// 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);
int x = int(random(0,width));
int y = int(random(0,height));
if(val>128){
drawball(x,y,val); //draw circles for large values
}else{
drawrect(x,y,val); // draw rectangles for small values
}
buf = "";
}
}
Part II: Mechanical
My concept for using the force sensor was to insert it into a bike pedal--sandwiched between felt and cardboard to dampen the force of pedaling slightly. One use for this would be to have a bike which downshifts automatically when it senses a large force being applied consistently as you labor up a hill.
Also, one could use the force data to calculate how hard you work and calculate the calories you burn riding more accurately!
I need to solder some long leads onto the force sensor to get some actual testing done (with actual stepping) which I plan to do tomorrow in lab.