Shutter timing arduino code

This is a follow-up to last weekend’s video about using an arduino to time camera shutters.

Here’s the source, in its diminutive glory:

//Connect the photo diode to analog input 0
//Connect the timer output to digital pin 3 (interrupt 1)
#define PDINPUT 0
#define THRESH 512

volatile unsigned int pulseCt;

void setup(void)
{
  Serial.begin(9600);
}

void loop(void)
{
  int last;
  int cur;
  
  for(;;)
  {
    cur = inputBlock(PDINPUT);
    
    if( cur >= THRESH && last < THRESH )
    {
      pulseCt = 0;
      attachInterrupt(1, clock, RISING);
      while( inputBlock(PDINPUT) > THRESH );
      detachInterrupt(1);
      Serial.println(pulseCt);
    }

    last = cur;
  }
}

void clock(void)
{
  pulseCt++;
}

//Rudimentary approach to flattening sampling noise -- adapt as required.
inline int inputBlock(const int pin)
{
  int a;
  a = analogRead(pin);
  a += analogRead(pin);
  return a / 2; 
}
Advertisements
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s