title: For an Arduino Sketch based light meter, functions outside of 'loop' are not being set off/firing
body: <p>I'm very new to Arduino. I have much more experience with Java and ActionScript 3. I'm working on building a light meter out of an <a href="http://arduino.cc/en/Main/ArduinoBoardUno" rel="nofollow">Arduino Uno</a> and a <a href="http://www.taosinc.com/ProductDetails.aspx?id=32" rel="nofollow">TAOS TSL235R</a> light-to-frequency converter.</p>
  
  <p>I can only find a tuturial using a different sensor, so I am working my way through converting what I need to get it all to work (AKA some copy and paste, shamefully, but I'm new to this).</p>
  
  <p>There are three parts: this is the first tutorial of the series <em><a href="http://dronecolony.com/wp/?p=87" rel="nofollow">Arduino and the Taos TSL230R Light Sensor: Getting Started</a></em>.</p>
  
  <p>The photographic conversion: <em><a href="http://roamingdrone.wordpress.com/2008/11/28/arduino-and-the-tsl230r-photographic-conversions/" rel="nofollow">Arduino and the TSL230R: Photographic Conversions</a></em>.</p>
  
  <p>At first, I could return values for the frequency created by the TSL235R sensor, but once I tried to add the code for photographic conversions I only get zero returned, and none of the funcions outside of the main loop seem to fire being that my <code>Serial.Println()</code> doesn't return anything.</p>
  
  <p><strong>I am more concerned with making the functions fire than if my math is perfect.</strong> In ActionScript and Java there are event listeners for functions and such, do I need to declare the function for it to fire in C/C++?</p>
  
  <p>Basically, how can I make sure all my functions fire in the C programming language?</p>
  
  <p>My Arduino <a href="http://www.arduino.cc/en/Tutorial/Sketch" rel="nofollow">Sketch</a>:</p>
  
  <pre><code>// TSL230R Pin Definitions
  #define TSL_FREQ_PIN 2
  
  // Our pulse counter for our interrupt
  unsigned long pulse_cnt = 0;
  
  // How often to calculate frequency
  // 1000 ms = 1 second
  #define READ_TM 1000
  
  // Two variables used to track time
  unsigned long cur_tm = millis();
  unsigned long pre_tm = cur_tm;
  
  // We'll need to access the amount of time passed
  unsigned int tm_diff = 0;
  
  unsigned long frequency;
  unsigned long freq;
  float lux;
  float Bv;
  float Sv;
  // Set our frequency multiplier to a default of 1
  // which maps to output frequency scaling of 100x.
  int freq_mult = 100;
  
  // We need to measure what to divide the frequency by:
  //   1x sensitivity =   10,
  //  10x sensitivity =  100,
  // 100x sensitivity = 1000
  int calc_sensitivity = 10;
  
  
  void setup() {
      attachInterrupt(0, add_pulse, RISING);  // Attach interrupt to pin2.
      pinMode(TSL_FREQ_PIN, INPUT); //Send output pin to Arduino
      Serial.begin(9600);  //Start the serial connection with the copmuter.
  }//setup
  
  
  void loop(){
      // Check the value of the light sensor every READ_TM ms and
      // calculate how much time has passed.
      pre_tm   = cur_tm;
      cur_tm   = millis();
  
      if( cur_tm &gt; pre_tm ) {
          tm_diff += cur_tm - pre_tm;
      }
      else
          if( cur_tm &lt; pre_tm ) {
              // Handle overflow and rollover (Arduino 011)
              tm_diff += ( cur_tm + ( 34359737 - pre_tm ));
          }
  
      // If enough time has passed to do a new reading...
      if (tm_diff &gt;= READ_TM ) {
          // Reset the ms counter
          tm_diff = 0;
  
          // Get our current frequency reading
          frequency = get_tsl_freq();
  
          // Calculate radiant energy
          float uw_cm2 = calc_uwatt_cm2( frequency );
  
          // Calculate illuminance
          float lux = calc_lux_single( uw_cm2, 0.175 );
      }
      Serial.println(freq);
      delay(1000);
  } //Loop
  
  
  unsigned long  get_tsl_freq() {
      // We have to scale out the frequency --
      // Scaling on the TSL230R requires us to multiply by a factor
      // to get actual frequency.
      unsigned long freq = pulse_cnt * 100;
      // Reset pulse counter
      pulse_cnt = 0;
      return(freq);
      Serial.println("freq");
  } //get_tsl_freq
  
  
  void add_pulse() {
     // Increase pulse count
     pulse_cnt++;
     return;
     Serial.println("Pulse");
  }//pulse
  
  
  float calc_lux_single(float uw_cm2, float efficiency) {
      // Calculate lux (lm/m^2), using standard formula
      //     Xv = Xl * V(l) * Km
  
      // where Xl is W/m^2 (calculate actual received uW/cm^2, extrapolate from sensor size
      // to whole cm size, then convert uW to W),
      // V(l) = efficiency function (provided via argument) and
      // Km = constant, lm/W @ 555 nm = 683 (555 nm has efficiency function of nearly 1.0).
      //
      // Only a single wavelength is calculated - you'd better make sure that your
      // source is of a single wavelength...  Otherwise, you should be using
      // calc_lux_gauss() for multiple wavelengths.
  
      // Convert to w_m2
      float w_m2 = (uw_cm2 / (float) 1000000) * (float) 100;
      // Calculate lux
      float lux  = w_m2 * efficiency * (float) 683;
      return(lux);
      Serial.println("Get lux");
  } //lux_single
  
  
  float calc_uwatt_cm2(unsigned long freq) {
      // Get uW observed - assume 640 nm wavelength.
      // Note the divide-by factor of ten -
      // maps to a sensitivity of 1x.
      float uw_cm2 = (float) freq / (float) 10;
      // Extrapolate into the entire cm2 area
      uw_cm2 *= ( (float) 1 / (float) 0.0136 );
      return(uw_cm2);
      Serial.println("Get uw_cm2");
  } //calc_uwatt
  
  
  float calc_ev( float lux, int iso ) {
      // Calculate EV using the APEX method:
      //
      // Ev = Av + Tv = Bv + Sv
      //
      // We'll use the right-hand side for this operation:
      //
      // Bv = log2( B/NK )
      // Sv = log2( NSx )
  
      float Sv = log( (float) 0.3 * (float) iso ) / log(2);
  
      float Bv = log( lux / ( (float) 0.3 * (float) 14 ) ) / log(2);
  
      return( Bv + Sv );
      Serial.println("get Bv+Sv");
  }
  
  
  float calc_exp_tm ( float ev, float aperture  ) {
      // Ev = Av + Tv = Bv + Sv
      // need to determine Tv value, so Ev - Av = Tv
      // Av = log2(Aperture^2)
      // Tv = log2( 1/T ) = log2(T) = 2^(Ev - Av)
  
      float exp_tm = ev - ( log( pow(aperture, 2) ) / log(2) );
  
      float exp_log = pow(2, exp_tm);
  
      return( exp_log  );
      Serial.println("get exp_log");
  }
  
  
  unsigned int calc_exp_ms( float exp_tm ) {
      unsigned int cur_exp_tm = 0;
  
      // Calculate mS of exposure, given a divisor exposure time.
  
      if (exp_tm &gt;= 2 ) {
          // Deal with times less than or equal to half a second
  
          if (exp_tm &gt;= (float) int(exp_tm) + (float) 0.5 ) {
              // Round up
              exp_tm = int(exp_tm) + 1;
          }
          else {
              // Round down
              exp_tm = int(exp_tm);
          }
          cur_exp_tm = 1000 / exp_tm;
      }
      else if( exp_tm &gt;= 1 ) {
          // Deal with times larger than 1/2 second
  
          float disp_v = 1 / exp_tm;
          // Get first significant digit
          disp_v       = int( disp_v * 10 );
          cur_exp_tm = ( 1000 * disp_v ) / 10;
      }
      else {
         // Times larger than 1 second
         int disp_v = int( (float) 1 / exp_tm);
         cur_exp_tm = 1000 * disp_v;
      }
      return(cur_exp_tm);
      Serial.println("get cur_exp_tm");
  }
  
  
  float calc_exp_aperture( float ev, float exp_tm ) {
      float exp_apt = ev - ( log( (float) 1 / exp_tm ) / log(2) );
      float apt_log = pow(2, exp_apt);
  
      return( apt_log );
      Serial.println("get apt_log");
  }
  </code></pre>