Reading an infered Herts

Hello,
I am currently trying to create a program to read the herts of an infered light blinking at a constant frequency and have it dispaly the frequency in the serial window. I am having alot of trouble making this program and was wondering if anyone could help. I have tried using pulsein() and IF statements and everything I HAVE knowledge of and i cant figure it out. Can someone help me out.
P.S. The frequency is suppost to be around 8-12hz but i want an exact number.
Thanks,
Daniel

If we could see your code and schematic, it would be easier to help.

Measure the milliseconds or microseconds between two raising edges (last time you checked in loop() the receiver received LOW, now it receives HIGH). If yo have such en edge, subtract the current time from the time of the previous raising edge and convert the time into a frequency. The formula for that (assuming you used micros() to keep track of time) is:

static unsigned long lastedge;
unsigned long duration = micros() - lastedge;
lastedge += duration;
float freq = 1000000.0/duration;

Korman

Thanks for the quick replys. I would give the code but I didn't save it due to my frustration. All I could really tell you is that the ir sensor is in pin 13.

Korman I think I know what your saying if your talking about the last edge as being the current state of the sensor.

No, more something like this:
Signal: |----------------|__|-----------
Time: LastEdge--^ micros()--^

Current state of the sensor is HIGH, LastEdge is the time when your sensor went from LOW to HIGH the last time. For the next measurement, it will be the current time.

Korman

Ok I'm a bit confused. so I would find the last edge..... using a pulsein() statement?

No, it's generated in your loop() function. Assuming you get HIGH and LOW on your pin 13 at reasonably low frequencies, the code would look something like this (that code is untested):

void setup() {
    pinMode(13, INPUT);     
    Serial.begin(38400);
}

void loop() {
static unsigned long lastedge;
static int laststate;

// Read current state
int currstate = digitalRead(13);

// Check if we have a LOW to HIGH edge
if (laststate==LOW && currstate == HIGH) {
    // Calculate duration in microsecongs
    unsigned long duration = micros() - lastedge;
    // Save current time as start for next calculation
    lastedge += duration;
    // Calculate frequency
    float freq = 1000000.0/duration;

    // Do something with with freq that takes less than one cycle
    Serial.println (freq);
}
// Save current state of signal for next run
laststate = currstate;
}

Don't forget to set your serial monitor to the correct speed, otherwise you'll just see garbage.

Korman

Oh my God. I feel like and idiot. That makes so much more sense.
Thanks you. I will test it and make correction next time I'm at my computer and let you know how it goes.
Daniel

OK I tried your program and it compiled ok but it did not display anything in the serial window. I couldnt figure out why this was. That is fine though because I figured out I don't need to find the frequency. So I no longer need help. Thanks for all the help on this post though. The solution didnt come the way I expected but I guess I cant complain. I learned two new thing with this problem.
Thanks again,
Daniel