user3343: I'm working with the Arduino Due ans I used the following code:
// period of pulse accumulation and serial output, milliseconds
#define MainPeriod 100
long previousMillis = 0; // will store last time of the cycle end
volatile unsigned long duration=0; // accumulates pulse width
volatile unsigned int pulsecount=0;
volatile unsigned long previousMicros=0;

void setup()
{
  Serial.begin(19200); 
  attachInterrupt(6, myinthandler, RISING);
}

void loop()
{
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= MainPeriod) 
  {
    previousMillis = currentMillis;   
    // need to bufferize to avoid glitches
    unsigned long _duration = duration;
    unsigned long _pulsecount = pulsecount;
    duration = 0; // clear counters
    pulsecount = 0;
    float Freq = 1e6 / float(_duration);
    Freq *= _pulsecount; // calculate F
    // output time and frequency data to RS232
    Serial.print(currentMillis);
    Serial.print(" "); // separator!
    Serial.print(Freq);
    Serial.print(" "); 
    Serial.print(_pulsecount);
    Serial.print(" ");
    Serial.println(_duration);
  }
}

void myinthandler() // interrupt handler
{
  unsigned long currentMicros = micros();
  duration += currentMicros - previousMicros;
  previousMicros = currentMicros;
  pulsecount++;
}

to measure the frequency.
I can measure frequencies higher than 1.5kHz accurately, but I can't seem to measure lower frequencies.
How can I measure lower frequencies, say 400Hz and above?
Thanks.

user84: It seems you have a problem of concurrent access to duration and pulsecount variables which are inter-related; with your code, it is possible that (and I guarantee that it will):

loop() reads duration value (in line unsigned long _duration = _duration;) but gets interrupted before executing the next line
myinthandler() changes both duration and pulsecount (it cannot be interrupted as it is already triggered by an interrupt)
loop() proceeds to where it was interrupted (after myinthandler() returns) and executes line unsigned long _pulsecount = pulsecount;: the values in _duration and _pulsecount do not match anymore!

To protect the access to pulsecount and duration variables, you have to disable interrupts inside loop() just for the time you get their values:
void loop() {
    ...
    // Disable interrupts while we get and reset duration and pulsecount
    cli();
    unsigned long _duration = duration;
    unsigned long _pulsecount = pulsecount;
    duration = 0; // clear counters
    pulsecount = 0;
    // Now we can re-enable interrupts again
    sei();
    ...
}


3343: <p>I'm working with the Arduino Due ans I used the following code:</p>

<pre><code>// period of pulse accumulation and serial output, milliseconds
#define MainPeriod 100
long previousMillis = 0; // will store last time of the cycle end
volatile unsigned long duration=0; // accumulates pulse width
volatile unsigned int pulsecount=0;
volatile unsigned long previousMicros=0;

void setup()
{
  Serial.begin(19200); 
  attachInterrupt(6, myinthandler, RISING);
}

void loop()
{
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis &gt;= MainPeriod) 
  {
    previousMillis = currentMillis;   
    // need to bufferize to avoid glitches
    unsigned long _duration = duration;
    unsigned long _pulsecount = pulsecount;
    duration = 0; // clear counters
    pulsecount = 0;
    float Freq = 1e6 / float(_duration);
    Freq *= _pulsecount; // calculate F
    // output time and frequency data to RS232
    Serial.print(currentMillis);
    Serial.print(" "); // separator!
    Serial.print(Freq);
    Serial.print(" "); 
    Serial.print(_pulsecount);
    Serial.print(" ");
    Serial.println(_duration);
  }
}

void myinthandler() // interrupt handler
{
  unsigned long currentMicros = micros();
  duration += currentMicros - previousMicros;
  previousMicros = currentMicros;
  pulsecount++;
}
</code></pre>

<p>to measure the frequency.</p>

<p>I can measure frequencies higher than 1.5kHz accurately, but I can't seem to measure lower frequencies.</p>

<p>How can I measure lower frequencies, say 400Hz and above?</p>

<p>Thanks.</p>
 84: <p>It seems you have a problem of concurrent access to <code>duration</code> and <code>pulsecount</code> variables which are inter-related; with your code, it is possible that (and I guarantee that it will):</p>

<ol>
<li><code>loop()</code> reads <code>duration</code> value (in line <code>unsigned long _duration = _duration;</code>) but gets interrupted before executing the next line</li>
<li><code>myinthandler()</code> changes both <code>duration</code> and <code>pulsecount</code> (it cannot be interrupted as it is already triggered by an interrupt)</li>
<li><code>loop()</code> proceeds to where it was interrupted (after <code>myinthandler()</code> returns) and executes line <code>unsigned long _pulsecount = pulsecount;</code>: the values in <code>_duration</code> and <code>_pulsecount</code> do not match anymore!</li>
</ol>

<p>To protect the access to <code>pulsecount</code> and <code>duration</code> variables, you have to disable interrupts inside <code>loop()</code> just for the time you get their values:</p>

<pre><code>void loop() {
    ...
    // Disable interrupts while we get and reset duration and pulsecount
    cli();
    unsigned long _duration = duration;
    unsigned long _pulsecount = pulsecount;
    duration = 0; // clear counters
    pulsecount = 0;
    // Now we can re-enable interrupts again
    sei();
    ...
}
</code></pre>
