UVtron on arduino

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!

 tm_diff += ( cur_tm + ( 34359737 - pre_tm ));

Magic numbers, like 34359737, unless they are clearly recognized quantities (like 3.14159) are never a good idea, unless fully commented. This one isn't.

Numbers without qualifiers are treated as integers. This value is NOT in int. It needs some qualifier (probably UL).

You can now post links, so post a link to the device you have.

Magic numbers, like 34359737, unless they are clearly recognized quantities (like 3.14159) are never a good idea, unless fully commented. This one isn't.

Apparently that number is "max millis() value"?:

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1215338347/15

Following the code on that thread, and making it a define, creates "self-documenting" code. I do agree with the sentiment, though: Comment your code or use a form of "self-documentation", so that others know what the numbers mean...

:slight_smile:

Apparently that number is "max millis() value"?:

No, it's not. The millis() function returns an unsigned long, with a maximum value of 4,294,967,295.

Thanks for your quick response.

Here is where i took the code for the counter,so its not my code,i didn't uderstand why this number was there,the comment say // handle overflow and rollover (Arduino 011) so i just trusted the comment.But its not a problem,i can remove it from the code,bacause millis will overflow after 9 hours(brian w.evans arduino programming notebook),and i won't use it for more than 50 minutes.
http://sales.hamamatsu.com/assets/applications/ETD/How-to-Use-UVtron_TPT9001E02.pdf
this is the sensor manual,i use the Uvtron drive circuit.After page 3 you can find the useful information.

bacause millis will overflow after 9 hours(brian w.evans arduino programming notebook),and i won't use it for more than 50 minutes.

Maybe you ought to toss Mr. Evans' book. The millis() function uses an unsigned long, which overflows every, approximately, 49 days.

I have to add that i just observe that there are strange measurements when the is no power supply for the sensor.

I have to add that i just observe that there are strange measurements when the is no power supply for the sensor.

And that surprises you? Why?

Ok i found the problem.I was giving 5V supply to the sensor from a battery,but arduino wasn't on the battery but on USB supply.The ground wasn't the same.I put the arduino on the battery and everything is ok.