I read a bunch of posts asking why millis doesn't start at zero and how to reset it to 0, and I guess I misunderstood what I was reading as you're right, it reads zero upon each start and with a delay I get usable values.
I'm almost there I guess. My DMM says 20hz, however I took another example to try and read the frequency on an input pin and I have not implemented it correctly because it prints out "Hz: inf" .
[edit] I've tried a few variations and the only thing I can surmise is that pulseIn blocks the loop, as the hz drop from 20 to 0 on my meter when I use pulseIn?
unsigned long currentTimeStamp;
unsigned long serialLastWriteTimeStamp;
int Htime; //integer for storing high time
int Ltime; //integer for storing low time
float Ttime; // integer for storing total time of a cycle
float f_frequency; //storing frequency
void setup()
{
Serial.begin(9600);
while (!Serial)
{
; // Wait for serial to connect
}
Serial.println("");
delay(2000);
Serial.println("Frequency Demo");
pinMode(7,INPUT);
pinMode(8, OUTPUT);
currentTimeStamp = millis();
serialLastWriteTimeStamp = millis();
}
void loop()
{
currentTimeStamp = millis();
digitalWrite(8, HIGH);
delay(25);
digitalWrite(8, LOW);
delay(25);
if (currentTimeStamp > serialLastWriteTimeStamp)
{
Htime=pulseIn(7,HIGH); //read high time
Ltime=pulseIn(7,LOW); //read low time
Ttime = Htime+Ltime;
f_frequency=1000000/Ttime; //getting frequency with Ttime is in Micro seconds
Serial.print("Hz: "); Serial.println(f_frequency);
serialLastWriteTimeStamp = (millis() + 1000);
}
}