Lab 4 - Cactus Dance

Submitted by fchasen on Tue, 02/26/2013 - 21:34

Description

I placed the cactus above the Force sensor. It is sensitive enough to detect even light touches to the leaves. Each leaf has a slightly different amount of pressure. I visualized these touches by constructing a L-system depiction of the cactus flowering. The virtual cactus gets more wild the harder one presses. Continually pressing the cactus causes it to dance around. 

 

Components Used

  • Arduino Uno
  • Breadboard
  • 10 ohm resistor
  • Plastic Cactus
  • Felt pads
  • Force Sensing Resistor

 

Code


// Fred Chasen: Cactus
// Based on - http://www.openprocessing.org/sketch/7150 : Alasdair Turner 2007
// And - Arduino Ball Paint: Tod E. Kurt
 
import processing.serial.*;
// Change this to the portname your Arduino board
//String portname = "/dev/tty.usbserial-A4001nLJ"; // or "COM5"
String portname = "/dev/tty.usbmodemfd121";
Serial port;
String buf="";
int cr = 13;  // ASCII return   == 13
int lf = 10;  // ASCII linefeed == 10
 
float wildness = 6.0;
float max = 25;
 
void setup()
{
  size(1200,750);
  smooth();
  noLoop();
  port = new Serial(this, portname, 9600); 
}
 
void draw()
{
  background(232,232,232);  //221,218,208
  strokeWeight(60);
  
  //-- Round caps
  strokeCap(ROUND);
  
  //-- offset top
  translate(width/2,height + 212);
  branch(0);
  
  //-- draw pot
  strokeWeight(75);
  stroke(189,175,145);
  //strokeCap(SQUARE);
  line( -width/2 + 400, height - 1025, width/2 - 400, height - 1025);
  
  fill(189,175,145);
  noStroke();
  rect( -width/2 + 400, height - 1050, 400, 200);
}
 
void branch(int depth)
{
  
  //-- Fade colors  
  stroke(92 + (depth * 2), 117 - (depth * 2), 94 + (depth * 3));
 
    if (depth < 13) {
      
      //-- Added to create leaves
      if(depth == 12){
        noStroke();
        fill(92 + (depth * 3), 117 - (depth * 2), 94 + (depth * 4));
        ellipse(10, 10, 100, -height/3);
      }else{
        line(0,0,0,-height/3 );
      }
      
      pushMatrix();
      {
        translate(0,-height/5);
        rotate(random(-PI/wildness,PI/wildness));
        scale(0.88);
        branch(depth + 1);
      }
      popMatrix();
      pushMatrix();
      {
        translate(0,-height/3);
        rotate(random(-PI/wildness,PI/wildness));
        scale(0.7);
        branch(depth + 1);
      }
      popMatrix();
    }
}
 
void mouseClicked()
{
  
  redraw();
}
 
void keyReleased() {
  if (key == CODED) {
    if (keyCode == UP) {
      if(wildness > 3){
        wildness -= .1;
      }
      redraw();    
    } else if (keyCode == DOWN) {
      if(wildness < 10){
        wildness += .1;
      }
      redraw();
    } 
  }
  
}
 
// 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); 
    
    // 7 - 3
    if(val > 0){
      wildness = 7 - norm(val, 0, max) * 4;
      redraw();
    }
    
    buf = "";
  }
}
 

 

 

cactus-sensor
cactus.png
0
Your rating: None
Drupal theme by Kiwi Themes.