2012-03-12 20 views
-1

我對Arduino非常陌生。我有更多的Java和ActionScript 3的使用經驗。我正在從Arduino UnoTAOS TSL235R光 - 頻轉換器中構建一個光表。對於基於Arduino Sketch的照度計,「循環」以外的功能不會被觸發/觸發

我只能找到使用不同傳感器的tuturial,所以我正在通過轉換我需要的所有方法來工作(也就是一些複製和粘貼,可恥,但我是新來的)。

有三個部分:這是該系列的第一個教程Arduino and the Taos TSL230R Light Sensor: Getting Started

攝影轉換:Arduino and the TSL230R: Photographic Conversions。首先,我可以返回由TSL235R傳感器創建的頻率值,但是一旦我嘗試添加攝影轉換代碼,我只返回零,並且沒有任何主環路以外的功能看起來像火災因爲我的Serial.Println()不會返回任何東西。

我更關心的是,如果我的數學是完美的,那麼使得函數失敗。在ActionScript和Java中,有函數的事件監聽器等,我是否需要聲明函數以在C/C++中觸發?

基本上,我怎樣才能確保我所有的函數在C編程語言中觸發?

我的Arduino Sketch

// 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 > pre_tm) { 
     tm_diff += cur_tm - pre_tm; 
    } 
    else 
     if(cur_tm < 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 >= 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 >= 2) { 
     // Deal with times less than or equal to half a second 

     if (exp_tm >= (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 >= 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"); 
} 

回答

2

這是一個很大的代碼來讀取,我應該從哪裏開始。

在你loop()要分配frequency但打印freq

// get our current frequency reading 
    frequency = get_tsl_freq(); 
-- snip -- 
Serial.println(freq); 
get_tsl_freq()

要創建一個本地unsigned int freq隱藏全球freq和使用,對於計算和返回值,也許這也是源爲你困惑。我沒有看到frequencyfreq在此代碼中是全局變量的原因。該函數還包含無法訪問的代碼,該控件將返回該函數,返回後的語句將永遠不會執行。

unsigned long get_tsl_freq() { 
    unsigned long freq = pulse_cnt * 100; <-- hides global variable freq 
    // re-set pulse counter 
    pulse_cnt = 0; 
    return(freq);   <-- () not needed 
    Serial.println("freq"); <-- Unreachable 
} 

讀多一點,我可以建議你拿起一個C++的書,讀了一下。雖然你的代碼編譯它不是技術上有效的C++,但由於Arduino軟件有一些改變,並且在聲明之前不允許使用函數,所以你可以放棄它。

您在您的計算中使用

float w_m2 = (uw_cm2/(float) 1000000) * (float) 100; 

可以寫成

float w_m2 = (uw_cm2/1000000.0f) * 100.0f; 

或常量,即使是這樣,因爲uw_cm2是一個浮動

float w_m2 = (uw_cm2/1000000) * 100; 

也似乎採取兩種等待的方法,你有代碼計算,只有它有運行自上次運行以來,已經超過1000毫秒,但在同一代碼中,您也是delay(1000),這可能無法按預期工作。

+0

謝謝。這使事情變得更加清晰。另外,我做了一些全局變量,因爲草圖會拋出一個「不在此範圍內定義」的錯誤,如果我沒有出於某種原因,即使我讀的教程讓他們當地 – 2012-03-12 07:33:21

+0

我不認爲我是新來的C++ 。我拿一本書,對於C++,我要在這個小實驗工作,併發布一份進展報告時,我已經取得了一些。感謝您的時間r_ahlskog。任何其他的建議是隨時歡迎 – 2012-03-12 07:54:15

+0

@JosephAaronCampbell你已經做得很好了沒多少C++背景,它不是語言的容易結束。而且有件事情我還沒有在我的回答感動了,如果你看一下#2我想你會發現一些建議閱讀資源,甚至可能是免費的。您可能遇到的問題是在內部範圍內聲明變量,並試圖在變量之外使用它們。我總是盡力幫助那些付出努力的人。 – 2012-03-12 08:44:42