/* stereocam.pde
 * Eric Ayars
 * 6/23/11
 * 
 * Control of zoom and shoot on a Canon A590is
 * using timed pulses on the USB power wire.
 *
 * Code intended for use on an ATtiny* chip. 
 * (ATtiny85, since that's what I have available currently...)
 * 
 */

#define SHOOT 0
#define ZOOMIN 1
#define ZOOMOUT 2
#define CAMERA_A 3
#define CAMERA_B 4
#define DEBOUNCE 20
#define SLOWDOWN 50
#define SHOOT_PULSE 5
#define ZOOMIN_PULSE 40
#define ZOOMOUT_PULSE 80

boolean oldShotState = true;    // button logic is inverted. true=unpressed

void pulse(byte duration) {
    digitalWrite(CAMERA_A, HIGH);
    digitalWrite(CAMERA_B, HIGH);
    delay(duration);
    digitalWrite(CAMERA_A, LOW);
    digitalWrite(CAMERA_B, LOW);
}

boolean newShot() {
    // shoot button is debounced and triggered on initial press.
    boolean result = true;
    boolean shotState = digitalRead(SHOOT);
    if (oldShotState && !shotState) {
        // transition just occured.
        result = false;
    }
    oldShotState = shotState;
    delay(DEBOUNCE);
    return result;
}
    
void setup() {
    
    // Set up pins 
    pinMode(CAMERA_A, OUTPUT);
    pinMode(CAMERA_B, OUTPUT);
    pinMode(SHOOT, INPUT);
    pinMode(ZOOMIN, INPUT);
    pinMode(ZOOMOUT, INPUT);

    // Turn on pull-up resistors for the input pins
    digitalWrite(SHOOT, HIGH);
    digitalWrite(ZOOMIN, HIGH);
    digitalWrite(ZOOMOUT, HIGH);
}

void loop() {

    // Just look at inputs and send pulses accordingly.
    if (!newShot()) pulse(SHOOT_PULSE);
    if (!digitalRead(ZOOMIN)) pulse(ZOOMIN_PULSE);
    if (!digitalRead(ZOOMOUT)) pulse(ZOOMOUT_PULSE);

    // Slow things down a bit to keep it manageable
    delay(SLOWDOWN);
}


Generated by GNU enscript 1.6.4.