Hello!i am a new member of the forum,and new user of an arduino mega.I want to measure pulses from a uv sensor,UVtron.I measure the uv from a candle.I have already measure the output pulses in a basic stamp 2.But i changed to arduino,for many reasons.I wantr to measure the pulses of the sensor in 2 seconds,so i took the idea from a tutorial for arduino and the tslr light sensor(i cant post the link beacause its my first post here,just refer it for the credits),and i use it for UVtron.I can get some correct measures,but there are wrong measures too.The strange is that in the basic stamp there weren't wrong measures,so something in the program is not right.
unsigned long pulse_cnt = 0;
// 1000ms = 1s
const int READ_TM=2000;
const int uvtron_pin=2;
// 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;
int i=0;
void setup()
{
Serial.begin(9600);
// attach interrupt to pin2, send output pin of TSL230R to arduino 2
// call handler on each rising pulse
attachInterrupt(0, add_pulse, RISING);
pinMode(uvtron_pin, INPUT);
// check the value of the light sensor every READ_TM ms
// calculate how much time has passed
// for (i=0;i<100;i++)
// {
// }
}
void loop()
{
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 ) {
Serial.print("tm_diff=");
Serial.println(tm_diff);
// re-set the ms counter
tm_diff = 0;
// get our current frequency reading
Serial.print("pulse_cnt=");
Serial.println(pulse_cnt);
pulse_cnt = 0;
}
}
void add_pulse()
{
//increase pulse count
pulse_cnt++;
return;
}
and this is the output:
tm_diff=2000
pulse_cnt=13
tm_diff=2000
pulse_cnt=2621
tm_diff=2000
pulse_cnt=541
tm_diff=2000
pulse_cnt=1318
tm_diff=2000
pulse_cnt=146
tm_diff=2000
pulse_cnt=13
tm_diff=2000
pulse_cnt=13
tm_diff=2000
pulse_cnt=13
tm_diff=2000
pulse_cnt=219
tm_diff=2000
pulse_cnt=12
tm_diff=2000
pulse_cnt=13
tm_diff=2000
pulse_cnt=13
tm_diff=2000
pulse_cnt=13
tm_diff=2000
pulse_cnt=7986
tm_diff=2000
pulse_cnt=6379
tm_diff=2000
pulse_cnt=3617
tm_diff=2000
pulse_cnt=12
tm_diff=2000
pulse_cnt=13
There is no problem with the tm_difference,its always 2 sec.The correct pulses are 12 and 13.I can fix the problem with an if,i know that the correct pulses are less than 16.But i'd like to find a better solution!Thanks in advance!