User Tools

Site Tools


courses:intro.prototyping.spring.2012.apr10.daniel_example

Daniel presented this example already:

// make sure you download the phidget21.jar file 
// from http://www.phidgets.com/programming_resources.php
// then add it to your sketch by going to Sketch > Add File
// you need to use the Phidget Interface Kit together with
// a sensor on the first connector
// now you're ready to go
 
import com.phidgets.*;
import com.phidgets.event.*;
InterfaceKitPhidget IK;// swap these labels for the actual sensors you have
 
float mot;   // different sensor values, read comments at bottom of this file
float temp;
float hum;
float light;
int xPos;
float back;
float datamot;
 
void setup() {
  size(400, 300);
  setupIK();
  smooth();
}
 
void draw() {
  // the main interfaces for data input / output
  readIK();
 
  // clear the previous frame,
  // draw a new background of greyscale value 200
  background(200);
 
  // draws an ellipse that moves from left to right
  // height depending on your sensor input 
  // tested with light sensor, slider and rotation sensor
  mot = map (mot, 0, 1000, 0, height); 
  fill(255, 0, 0);
  noStroke();
  ellipse(xPos, (height-mot), 10, 10);  
  println(mot);
 
  // this controls the graph position onscreen,
  // it animates the graph and refreshes to the 0 position
  // when the graph goes offscreen
  if (xPos >= width) {
    xPos = 0;
  }
  else {
    xPos++;
  }
}
 
void setupIK() {
  try {
    IK = new InterfaceKitPhidget();
    IK.openAny();
    println("Waiting for Phidget");
    IK.waitForAttachment();
    println("OK ready to go");
  }
  catch(Exception e) {
    println("ERROR");
    System.out.println(e);
  }
}
 
void readIK() {
  try {
    // the ports are considered an array, with 8 ports in total
    // the first port is array number 1, but it is addressed as port 0
    // since it is 0 elements away from the start of the array.
    // make sure your 'motion' sensor is located at sensor port 1 (number 0)
    // and your 'light' sensor is at sensor port 2 (number 1)
    mot = (float)IK.getSensorValue(0);//
    light = (float)IK.getSensorValue(1);//
    hum = (float)IK.getSensorValue(2);//
    temp = (float)IK.getSensorValue(3);
  }
  catch (Exception e) {
    println(e.toString());
  }
}
courses/intro.prototyping.spring.2012.apr10.daniel_example.txt · Last modified: 2012/04/04 07:08 by 130.239.234.49