/*  remote-key.pde
    Eric Ayars
    Remote laser key-switch program. Given a logic-level signal, this
    program (with the associated circuit and a servo) uses the servo
    to turn the key-switch on a laser interlock thus allowing remote
    operation of the system.

    Uses Ilya Brutman's Servo8Bit library:
    Written for use with the MIT High-Low-Tech ATtiny45/85 Arduino cores
    and the Arduino IDE. See

#include "Servo8Bit.h"

#define SIG 0       // signal from computer at PB0
#define SERVO 1     // servo is attached to PB1
#define LED 4       // indicator LED attached to PB4

// These definitions will have to be changed to match the actual 
// key positions needed. Numbers roughly correspond to degrees.
#define ON_ACT 5
#define ON_REST 10
#define OFF_ACT 105
#define OFF_REST 100

// The following is somewhat arbitrary: it should be longer than the time
// it takes the servo to move from one position to the other.
#define CLICK_DELAY 1000

byte oldSignal, newSignal;  // used to track changes in signal line.
Servo8Bit servo;            // create a servo object

void turnOn() {
    /*  Both turnOn() and turnOff() turn the key PAST the rest position,
        wait a moment, then turn back to the "rest" position for that
        state. The idea here is to ensure that the key reaches the desired
        position without leaving the servo in a position of continued 
    digitalWrite(LED, HIGH);
}   // end turnOn()

void turnOff() {                    // See comment in turnOn()
    digitalWrite(LED, LOW);
}   // end turnOff()

void setup() {
    /* Only 3 lines are actually used:
        one for the signal --- high turns servo to on position
        one for the LED --- LED turns on/off echoing position of servo
        one for the servo itself.
    pinMode(SIG, INPUT);            // Signal line is an input
    pinMode(LED, OUTPUT);           // LED line is an output
    oldSignal = digitalRead(SIG);   // Figure out initial state
    if (oldSignal) {                // Ensure that servo is in the
        turnOn();                   // position that the computer
    } else {                        // believes it to be.
}   // end setup()

void loop() {
    newSignal = digitalRead(SIG);   // Read the signal now
    if (newSignal != oldSignal) {   // If it's different than it was...
        if (newSignal) {            // ...then make the servo state 
            turnOn();               // match the signal state.
        } else {
        oldSignal = newSignal;      // The circle of life...

    // Assume we don't need to change the switch more than 5x/second.

}   // end loop()

Generated by GNU enscript 1.6.4.