Description
For this homework, I created a force-sensing trivet that can tell if something is on top of it. As the Trivet sends out a value over the serial port, the trivet can be used to run a processing-based application that is based on the example code given, but has minor elements of originality such as two ellipses, variable color and variable backgrounds based on the serial input.
Hardware used
- Arduino Board and breadboard configured for in-class FSR Lab
- 1 Force Sensor
- 2 Alligator Clips
- 3 Wooden Dowel Hole Covers
- 1 4"x6" sheet of glass
- 1 Trivet
Portrait of Trivet
Processing Code
/*
* Arduino Ball Paint
* (Arduino Ball, modified)
* ----------------------
*
* Draw balls randomly on the screen, size and color 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 <tod@todbot.com
* http://todbot.com/
*
* Modified 25 Sept 2007
* MWCS
*
*/
import processing.serial.*;
String portname = "/dev/tty.usbserial-A4001nWq";
Serial port;
String buf="";
int cr = 13; // ASCII return == 13
int lf = 10; // ASCII linefeed == 10
void setup() {
size(800,600);
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));
drawball(x,y, 50);
}
}
// draw balls
void drawball(int x, int y, int r) {
for (int i=0; i<1000; i++ ) {
int cRed = (r+i)*3 % 256;
int cGreen = (2*r+i)*4 % 256;
int cBlue = (r+i)*5 % 256;
//change the color of the circles based on a combination
fill(color(cRed, cGreen,cBlue));
stroke(4);
ellipse(x,y,r/3,r/3);
ellipse(width-x, height-y, r/2, r/2);
// text(r,x,y);
// clear the background every 20 seconds
if((second()%20)== 0) {
//vary the background based on the incoming value
background((r*4)%256,(r*4)%256,(r*4)%256);
}
}
}
// 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));
drawball(x,y,val);
buf = "";
}
}
Comments
matt, that's a nice idea to
matt, that's a nice idea to equip a trivet with a force sensor. Barring any programming limitations, can you come up with other ways this information can be useful? Ellipses are nice and all...