// KAP controller // Guy J Brown // www.guyjbrown.com // This software may be freely distributed and modified. // You use this software at your own risk. package kap; import com.sun.spot.sensorboard.EDemoBoard; import com.sun.spot.sensorboard.peripheral.ISwitch; import com.sun.spot.sensorboard.peripheral.ISwitchListener; import com.sun.spot.util.Utils; import java.io.IOException; import com.sun.spot.sensorboard.io.IOutputPin; import com.sun.spot.sensorboard.peripheral.Servo; import com.sun.spot.sensorboard.peripheral.ITriColorLED; import com.sun.spot.sensorboard.peripheral.LEDColor; import javax.microedition.midlet.MIDlet; import javax.microedition.midlet.MIDletStateChangeException; public class KAPcontroller extends MIDlet implements ISwitchListener { private ISwitch sw1; // variable for the switch private ITriColorLED[] leds = EDemoBoard.getInstance().getLEDs(); // define the pins that we use final int CONTROL_PIN = EDemoBoard.H0; final int CAMERA_PIN = EDemoBoard.H1; float SERVO_CENTER = 0.5f; float SERVO_MOVE = 0.6f; boolean running = false; Servo myServo; IOutputPin cpin; private void monitorSwitches() throws IOException { EDemoBoard edemo = EDemoBoard.getInstance(); sw1 = edemo.getSwitches()[EDemoBoard.SW1]; sw1.addISwitchListener(this); // set the LED colour and turn it on leds[0].setColor(LEDColor.BLUE); leds[0].setOn(); // instatiate the servo myServo = new Servo(edemo.getOutputPins()[CONTROL_PIN]); // set the servo bounds, found by trial and error myServo.setBounds(380, 2280); // the pin that controls the camera cpin = edemo.getOutputPins()[CAMERA_PIN]; // put the servo in its middle position myServo.setPosition(SERVO_CENTER); while (true) { if (running) { // move the servo a little myServo.setPosition(SERVO_MOVE); Utils.sleep(120); myServo.setPosition(SERVO_CENTER); // take the picture Utils.sleep(1000); triggerCamera(); Utils.sleep(8000); } } } private void triggerCamera() { // pulse < 5V on pin H1 for 100 ms to trigger camera cpin.setHigh(); Utils.sleep(100); cpin.setLow(); } public void switchPressed(ISwitch sw) { if (running) { running = false; leds[0].setColor(LEDColor.BLUE); } else { running = true; leds[0].setColor(LEDColor.RED); } } public void switchReleased(ISwitch sw) { } // startApp() is the MIDlet call that starts the application. protected void startApp() throws MIDletStateChangeException { // Listen for downloads/commands over USB connection new com.sun.spot.util.BootloaderListener().start(); try { monitorSwitches(); } catch (IOException ex) { //A problem in reading the switches ex.printStackTrace(); } } // Boilerplate - this will never be called protected void pauseApp() { } // Called if the MIDlet is terminated by the system. protected void destroyApp(boolean unconditional) throws MIDletStateChangeException { } }