How to use a Timer

Hey Guys,

I am new to this whole arduino thing, so I would appreciate a push in the right direction.

I am wanting to time how long the input is high in ms. Ultimitly I am wanting to time this value for the input digitalRead(13, HIGH)

So if pin13 is high for 250ms, then I will have a block of code that tuns for the if statement.

Thanks in advance.

I am wanting to time how long the input is high in ms.

The pulseIn function was designed for just this purpose.

Thanks for the help Paul. Could you let me know if I have the gist of this with the sample below?

int pin = 7; int pin2 = 8;

void setup() { pinMode(pin, INPUT); }

void loop() { duration1 = pulseIn(pin, HIGH); duration2 = pulseln(pin2, HIGH); if (duration1 == 2000) { digitalWrite(13, HIGH); } If (duration2 == 2000) { digitalWrite(14, HIGH); } }

int pin = 7;
int pin2 = 8;

If you are counting things, do you count "a sock, 2 socks, 3 socks, ..." or do you count "1 sock, 2 socks, 3 socks, ..."?

If you number one variable, you should number the other one, too.

  pinMode(pin, INPUT);

If you declare one pin as input, you should declare the other as input, too.

  duration1 = pulseIn(pin, HIGH);

duration1 and duration2 are not declared/typed anywhere.

if (duration1 == 2000)

I don't know what you are timing, but it is unlikely to take EXACTLY 2000 microseconds, whatever it is.

digitalWrite(13, HIGH);

You'll be disappointed with the results of setting an INPUT pin HIGH. Pins are INPUT by default. If you want to write to a pin, you should set it as an OUTPUT pin.

If you number one variable, you should number the other one, too.

Personally I don't a problem with this, but if they were both numbered I'd start from zero, then at a future point the code could be generalised to an array.

Thanks for the input Paul. I am wanting a certain function to run after pin7 has been high for 2ms.

int pin1 = 7;
int pin2 = 8;
int ledPin = 13;
int duration1;
int duration2;

void setup()
{
  pinMode(pin1, INPUT);
  pinMode(pin2, INPUT);
  pinMode(ledPin, OUTPUT);
}

void loop()
{
  duration1 = pulseIn(pin, HIGH);
  duration2 = pulseln(pin2, HIGH);
if (duration1 == 2000)
{
digitalWrite(13, HIGH);
}
If (duration2 == 2000)
{
digitalWrite(14, HIGH);
}
}

I think I'm beginning to see this a bit better. Is there a different way to differentiate between 2 seperate inputs on the pulse function?

The pulseIn function returns the time in microseconds, not milliseconds. The type of duration1 and duration2 needs to be unsigned long, not int.

Try compiling your code before posting it. Try posting the code properly, using the # button.

Proper indenting makes code easier to read.

Having said all that, I don't think this approach will work for you. The pulseIn function waits for a HIGH pin to go LOW then HIGH again, or for a LOW pin to go HIGH and then LOW again. It times how long the pin stays at the requested state.

It does not return until the third transition. So, you won't be able to turn the LED on after 2ms, unless the pin switches state exactly at that time (highly unlikely).

What is generating these pulses you are trying to measure and react to.

Is there a different way to differentiate between 2 seperate inputs on the pulse function?

The pulseIn function monitors the pin you tell it to. So, yes, it easy to keep the inputs separate.

I am trying to build a scoring machine for fencing that requires an led to turn on if the input on line A goes high after the line has gone high for 2ms. According to the information I have gone through, I am completing the circuit on this instance.

christopherwms: I am trying to build a scoring machine for fencing that requires an led to turn on if the input on line A goes high after the line has gone high for 2ms. According to the information I have gone through, I am completing the circuit on this instance.

Interrupts will probably do it. But can you clarify the above statement? You want to turn on an LED if input A goes high, and stays high for 2 mS? Is that it? I don't quite understand this: "if the input on line A goes high after the line has gone high" - it can't go high if it is already high.

And you want to do this for another input? Or are there two inputs involved for the one action?

christopherwms: I am trying to build a scoring machine for fencing

Paging forum member Crossroads! 8)

Sounds like a good usage of the bounce library, where you ignore a high going input unless it lasts at least 2 milliseconds? just like ignoring contact bounce for a switch contact.

Lefty

I think you need use if (duration1 >= 2000) {}, so there might be a chance for the arduino the execute codes in the block. Arduino or any other mcu could hardly execute precisely that consume just 2000 microseconds, the actual value might be 1999, 2001 or any other value around 2000.

Thanks for that input.

Retrolefty could you point me in the direction for the proper usage for bounce library.

Sounds like a good usage of the bounce library, where you ignore a high going input unless it lasts at least 2 milliseconds? just like ignoring contact bounce for a switch contact.

You mean "an input going high"? I had to read that several times to make it parse.

ardypro: I think you need use

if (duration1 >= 2000) 
{
}

, so there might be a chance for the arduino executes codes in the block. Arduino or any other mcu could hardly execute precisely that consume just 2000 microseconds, the actual value might be 1999, 2001 or any other value around 2000.