Sensing Force and Programming

/*
 * Arduino Canvas Paint
 * (Arduino Ball, modified 2011)
 * ----------------------
 *
 * Draw rectangles with colors based on the value registered from the potemtiometer.
 *
 * 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 in September 2011 by Sebastian Fuenzalida
 *
 */
import processing.serial.*;
// Change this to the portname your Arduino board
String portname = "/dev/tty.usbmodemfd121"; // or "COM5"
Serial port;
String buf="";
int cr = 13;  // ASCII return   == 13
int lf = 10;  // ASCII linefeed == 10
void setup() {
  size(300,255);
  colorMode(HSB,300,300, 200);
  frameRate(10);
  smooth();
  background(40,40,40);
  noStroke();
  port = new Serial(this, portname, 9600);
}
void draw() {
}

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);

    fill(val, val, 300);
    rect(val,0,10,300);
    buf = "";
 
  }
}

Screen shot 2011-09-27 at 6.36.01 PM.png
photo-7.jpg
0
Your rating: None