Redefinition of void loop()

I'm trying to convert a square wave frequency to a voltage using PWM but when I compile I keep getting an error saying "In function 'void loop()':
error: redefinition of 'void loop()'" referring to the void loop() just after unsigned int tm_diff=0;

Here's the code I'm using:

#define TSL_FREQ_PIN 2 //output use digital pin2 for interrupt
int out = 3;

unsigned long pulse_cnt=0;

void setup() {

//attach interrupt to pin2, sound output pin of TSL230R to arduino2
//call handler on each rising pulse

attachInterrupt(0, add_pulse, RISING);

pinMode(TSL_FREQ_PIN, INPUT);
pinMode(out, OUTPUT);

}

void loop() {

}

void add_pulse() {

//increase pulse count
pulse_cnt++;
return;
}

//1000ms = 1s
#define READ_TM 1000;

//two variable 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;

void loop() {

// check the value of the light sensor every READ_TM ms

// 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 ) {

// re-set the ms counter
tm_diff = 0;

// get our current frequency reading
unsigned long frequency = get_tsl_freq();

}

}

unsigned long get_tsl_freq() {
// copy pulse counter and multiply.
// the multiplication is necessary for the current
// frequency scaling level. Please see the
// OUTPUT SCALING section below for more info

unsigned long freq = pulse_cnt * 100;

// re-set pulse counter
pulse_cnt = 0;

return(freq);
}

Any help would be great! :slight_smile:

Get rid of that empty first void loop() definition; you are essentially trying to define the loop twice in your code - that's a no-no...

:slight_smile:

Thank you! :slight_smile:

Unfortunately I'm now getting this error:

"In function 'void loop()':
error: expected `)' before ';' token"

in reference to if( tm_diff >= READ_TM ) {

#define READ_TM 1000[glow];[/glow]

That one. Get rid of it.

Thank you very much.

One last thing, the frequency that is being measured here, would it be possible to convert this into a voltage using PWM?

Yes, but you need external components for a low-pass filter.

The plan was to have this voltage going into a National Instruments DAQ (NI USB6008). Would a low pass filter still be necessary or is noise unavoidable?

Would a low pass filter still be necessary

Essential, I'd say.

#define READ_TM 1000;

That cost me 2h today in my project!
I guess you just do it one time in life...... :smiley:

You look so hard, and you look in the wrong place.