Lab4 Force Cell + Processing = Turtle Fun

Posted by icheung

icheung's picture

Materials: Plastic Clip, Felt, Force Sensor, Resistors, Capacitor

Outcome:

I created "game controller" like hand-piece out of the clip, felt, and force sensor to manipulate an image of a turtle generated using Processing.  Pressing the left side of the clip causes the turtle to move left, pressing the right side of the clip causes the turtle to move right, and pressing neither side leaves the turtle where he is at.

http://www.youtube.com/watch?v=aOk7kb7EOGw

Arduino code:

 

// Wiring / Arduino Code
// Code for sensing a switch status and writing the value to the serial port.
 
int switchPin = A2;                       // Switch connected to pin 4
 
void setup() {
  pinMode(switchPin, INPUT);             // Set pin 0 as an input
  Serial.begin(9600);                    // Start serial communication at 9600 bps
}
 
void loop() {
  if (analogRead(switchPin) < 100)       // If right button pressed
  {
    Serial.print(1, BYTE);               // send 1 to Processing
  } 
  else if (analogRead(switchPin) > 280)  // If left button is pressed
  {                              
    Serial.print(0, BYTE);               // send 0 to Processing
  }
  else
  {
    Serial.print(-1, BYTE);               // send -1 to Processing
  }
  delay(100);                            // Wait 100 milliseconds
}

Processing code:

 

/**
 * Simple Read
 * 
 * Read data from the serial port and change the color of a rectangle
 * when a switch connected to a Wiring or Arduino board is pressed and released.
 * This example works with the Wiring / Arduino program that follows below.
 */
 
// Declaring a variable of type PImage
PImage img;
 
import processing.serial.*;
 
Serial myPort;  // Create object from Serial class
int val;      // Data received from the serial port
int screenX = 256;
int screenY = 256;
int turtleW = 35;
int turtleH = 43;
int tCurrX = screenX-turtleW;
int tCurrY = screenY-turtleH;
int rCount = 0;
int lCount = 0;
int r=0;
int l=0;
boolean rightFlag = false;
boolean leftFlag = true;
 
void setup() 
{
  size(512, 512);
  img = loadImage("smudge_turtle.png");
  frameRate(10);
  // I know that the first port in the serial list on my mac
  // is always my  FTDI adaptor, so I open Serial.list()[0].
  // On Windows machines, this generally opens COM1.
  // Open whatever port is the one you're using.
  String portName = "/dev/cu.usbmodem621";
  myPort = new Serial(this, portName, 9600);
}
 
void draw()
{
  background(0);
 
  if ( myPort.available() > 0) {  // If data is available,
    val = myPort.read();         // read it and store it in val
  }
  background(255);             // Set background to white
  if (val == 0) {              // If the serial value is 0,
    fill(0);                   // set fill to black  LEFT BUTTON
    pushMatrix();
    translate(tCurrX,tCurrY+2*turtleH);
    rotate(radians(-90));
    image(img,0,0);
    background(255);
    translate(0,-lCount);
    image(img,0,0);
    lCount++;
    tCurrX-=lCount;
    popMatrix();
    leftFlag = true;
    //tCurrX++;
  } 
  else if (val == 1) {                       // If the serial value is not 0,
    fill(204);                 // set fill to light gray  RIGHT BUTTON
    pushMatrix();
    translate(tCurrX+2*turtleW,tCurrY);  // turtle center is 35,43
    rotate(radians(90));
    image(img,0,0);
    background(255);
    translate(0,-rCount);
    image(img,0,0);
    rCount++;
    tCurrX+=rCount;
    println(rCount);
    popMatrix();
    rightFlag = true;
  }
  else {
    /*
    if (rightFlag) {
      r++;
      rightFlag = false;
      //translate(tCurrX,tCurrY);
    }
    if (leftFlag) {
      l++;
      leftFlag = false;
      //translate(tCurrX,tCurrY);
    }
    */
    fill(230);                 // set to light light gray NOTHING
    pushMatrix();
    //println("l:");
    //println(l);
   // println("r:");
   // println(r);
   // rotate(radians(r*90-l*90+1));
    //background(255);
    image(img,tCurrX,tCurrY);
    popMatrix();
 
    //image(img,tCurrX,tCurrY);
  }
 // rect(50, 50, 100, 100);
 
}
 
 
/*
 
// Wiring / Arduino Code
// Code for sensing a switch status and writing the value to the serial port.
 
int switchPin = A2;                       // Switch connected to pin 4
 
void setup() {
  pinMode(switchPin, INPUT);             // Set pin 0 as an input
  Serial.begin(9600);                    // Start serial communication at 9600 bps
}
 
void loop() {
  if (analogRead(switchPin) > 100) {  // If switch is ON,
    Serial.print(1, BYTE);               // send 1 to Processing
  } else {                               // If the switch is not ON,
    Serial.print(0, BYTE);               // send 0 to Processing
  }
  delay(100);                            // Wait 100 milliseconds
}
*/
 
0
Your rating: None