gpt4 book ai didi

c++ - 对于基于 Arduino Sketch 的照度计, 'loop' 之外的功能不会被触发/触发

转载 作者:行者123 更新时间:2023-11-30 18:59:38 24 4
gpt4 key购买 nike

我对 Arduino 很陌生。我对 Java 和 ActionScript 3 有更多的经验。我正在使用 Arduino Uno 构建测光表。和一个TAOS TSL235R光频转换器。

我只能找到使用不同传感器的教程,因此我正在努力转换我需要的内容以使其全部正常工作(又名复制和粘贴,可耻的是,但我对此很陌生)。

共有三个部分:这是该系列的第一篇教程 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");
}

最佳答案

有很多代码需要阅读,我应该从哪里开始。

在您的loop()中,您分配频率但打印freq

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

get_tsl_freq()中,您正在创建一个隐藏全局freq的本地unsigned int freq,并使用它进行计算并返回值,也许这也是你困惑的根源。我没有看到 freqfreq 在此代码中成为全局变量的原因。该函数还包含无法访问的代码,控制权将在返回时离开该函数,返回后的语句将永远不会被执行。

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

float w_m2 = (uw_cm2 / 1000000) * 100;

您似乎也采用了两种等待方法,您的代码会计算并仅在自上次运行以来超过 1000 毫秒时才运行,但您也delay(1000)在相同的代码中,这可能根本无法按预期工作。

关于c++ - 对于基于 Arduino Sketch 的照度计, 'loop' 之外的功能不会被触发/触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9662476/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com