Simple period measurement issues

I am trying to measure the period of a square wave of 20Hz frequency with an amplitude of 3V. ( Verified with a scope )

The setup is a UNO board with the frequency generator output fed to Pin 3 and GND.

Refer the attached code. For Period it prints -15430. What I was expecting is 50 milliseconds.

#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#include "Wire.h"
#include <FreqPeriodCounter.h>

//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
//================== DEFINES ======================
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
// Define the LCD pins to use and i
// Initialize an instance
 LiquidCrystal_I2C  lcd(0x27,2,1,0,4,5,6,7); // 0x27 is the I2C bus address 

const byte counterPin = 3; 
const byte counterInterrupt = 1; // = pin 3
FreqPeriodCounter counter(counterPin, micros);

//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
//================= SETUP ========================= 
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

void setup(void)
{
// START SERIAL FOR DEBUGGING
// Usage : Serial.println(variable);  
  Serial.begin(9600); 

// START THE LCD INTERFACE
  lcd.begin(16, 2);
  lcd.setBacklightPin(3,POSITIVE);
  lcd.setBacklight(HIGH);
  lcd.clear();
  lcd.write( "FREQUENCY COUNT." ); 
  lcd.setCursor(0,1);
  lcd.write(" Version : 1.00 ");
  delay(2000);
  lcd.clear();
  

 attachInterrupt(counterInterrupt, counterISR, CHANGE);
}


//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
//================ SCAN LOOP ======================
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

void loop(void) 

{ int period;
  if(counter.ready()) period = counter.period; 
  Serial.println(period); 
  delay(500);
  
}

void counterISR()
{ counter.poll();
}

I have still not started using the LCD segment and trying to read the period only on the serial port. I also have attached the whole library for FrequencyPeriod as a zip file.

FreqPeriodCounter.zip (3.13 KB)

What sort of variable do you get from counter.period ? Could the value returned be larger than you can fit in an int ?

The header file for the library contains:

  unsigned long period, pulseWidth, pulseWidthLow, elapsedTime;

So, why are you storing period in an int? 20 cycles per second is one cycle every 50 millisecond, or every 50000 microseconds. 50000 will not fit in an int.

Yes storing the period in an INT was a goof up.

I now changed it to unsigned long but the situation has gone from bad to worse ! All I am able to read is 15204352. This seems to be some magic digit as it is the same irrespective of the frequency I feed and even when I switch off the FG !!

The suspense continues. I wish I had taken my C++ tutions more seriously. Hmmm.

Instead of struggling with that library which I had enclosed, I just switched over to the FreqMeasure.zip from here : https://www.pjrc.com/teensy/td_libs_FreqMeasure.html#compare

Works like magic. Problem solved :)