Source for a Slider-Controlled Servo

#phidgets#sensors

Here is the source for a sensor - in this case just a slider - connected to a servo, each phidget connected to plain USB.

import com.phidgets.*;
import com.phidgets.event.*;

InterfaceKitPhidget ikp;
AdvancedServoPhidget asp;

float sensorVal;

void setup() {
  size(400, 300);
  fill(255, 0, 0);

  setupServo();
  setupIK();
}

void draw() {

  readIK();

  // clear the previous frame,
  // draw a new background of greyscale value 200
  background(200);

  sensorVal = map (sensorVal, 0, 1000, 0, height);
  ellipse(width/2, sensorVal, 10, 10);
  println(sensorVal);

  try{
    asp.setPosition(0, sensorVal);
  } catch(Exception e) {
    println("ERROR");
    System.out.println(e);
  }
}

void readIK() {
  try {

    //First port on the sensor phidget board array
    sensorVal = (float)ikp.getSensorValue(0);//
  }
  catch (Exception e) {
    println(e.toString());
  }
}

void setupIK() {
  try {
    ikp = new InterfaceKitPhidget();
    ikp.openAny();
    println("Waiting for Interface Phidget");
    ikp.waitForAttachment();
    println("OK ready to go");
  }
  catch(Exception e) {
    println("ERROR");
    System.out.println(e);
  }
}

void setupServo(){
  try{
    // Create & Assign the servophidget
    asp = new AdvancedServoPhidget();
    asp.openAny();
    println("Waiting for Servo Phidget");
    asp.waitForAttachment();
    asp.setEngaged(0, true);
    // if you do not see this below in the console,
    //your phidget is not connected
    println("OK ready to go");
  }
  catch(Exception e){
    println("ERROR");
    System.out.println(e);
  }
}

all writing