Comparing against time?

Hi, I'm going to write a sketch for a project soon that involves measuring how long a pin is pulled HIGH for and I have no idea how to do this. I am going to do something like this.

if (pin1 is pulled HIGH for less than 500ms) {
  do this
}
else if {
  (pin1 is pulled HIGH longer than 500ms) {
    do this
  }
}

I don't know if this is possible so any help is appreciated!

Should be no problem. The ATmega328 runs at 16MHz and you can just check the state of the pin in a loop and save the time when it changes state.

Use micros() (-> http://arduino.cc/en/Reference/Micros) or millis() (-> http://arduino.cc/en/Reference/Millis) depending on your needs.

That seems helpful, but does not fully answer my question. Just upon reading the description of those two functions it only tells you the time since the start of the program, not specific pin state changes.

The two functions only give you a timestamp with different precision. What you need to do is record the timestamp when the input pin changes its state from low to high and reverse. The time difference is what you are looking for.

Exactly, so how would this be done? I haven't the slightest clue as to how this could be done.

Sorry, but my last post already contained a description what the program should do. The task of transforming this into C/C++ code is yours. On the main site there is a category "Reference" listing all the keywords of the language. The Arduino software includes examples in the file menu. Use digitalRead() for getting the state of a pin. And if your program is not doing what you intend you always can post it here (use code tags, button # above the smilies) and describe what you think your code should do and what you get instead.

Ok, thanks for the help.

zdillman: Exactly, so how would this be done? I haven't the slightest clue as to how this could be done.

If you have a watch on your arm, and you want to know whether or not the next bus arrives within 10 minutes, how would you do that? That watch doesn't tell you the time between now and when the bus arrives directly, but there is a way of working it out.