using pulseIn, how can/should interrupts be used?

I have used the included code to read pulse length. The input to pin-7 is square waves from a multivibrator at approximately 80 pulses/ second. The code puts out readings every 1000ms.
There are two things that I need to ask about this code.
(1) Will there be any benefit to using interrupts? This code is a practice run for me, since I’m quite new at this. I understand the concept of interrupts, but I don’t know how to use them. I tried putting the pulseIn command in during the time counting procedure - “time = millis()” (as part of a while statement) but it didn’t work, so I put it before “time = millis();” . Would interrupts make it possible to do other things while the timing is proceeding?

(2) If there is no input to pin-7 before upoading to board, everything locks up, computer and all. I had a lot of problems getting things back in order after this happened. How can I prevent such a lockup?

unsigned long time;
unsigned long oldtime = 0;
int x;
int pin = 7;
unsigned long hpulse;

void setup(){
Serial.begin(9600);
pinMode(pin, INPUT);
}
void loop(){

hpulse = pulseIn(pin, HIGH,8000);

time = millis();
x = (time - oldtime);
if (x >= 997){ //OTHER PROCEDURES ADDED TIME RESULTING IN X > 1000
Serial.print("x: ");
Serial.println(x);
Serial.print("PULSE: ");
Serial.println(hpulse);

Serial.print("time: ");
Serial.println(time);
//prints time since program started
oldtime = time;
}

}

I’m using this code as a base to learn more about arduino code by
adding more code and procedures: adding an LCD, performing math procedures; things that will take more time in between pulse readings.

Thank you
P.W.

Sorry I'm not posting an answer to your questions... But I am interested in knowing if you ever got an answer regarding your interrupt question. If so, would you mind posting your findings? Like you, I'm new to this (micro-controllers, electronics & programming!) but I'm finding it really interesting so I'm following my curiosity to see where it leads.

sam

When you call pulseIn() it doesn't return until it has received the pulse or timed out. If your application can afford to wait and do nothing until a pulse is received, that's just fine. If your code has other things to do until a pulse is received you should use an interrupt handler, so the main loop is not tied up waiting for each pulse to come in.

I'm not too clear about the lockup you experienced, but pulseIn() has one potential vulnerability: once a pulse begins it will wait forever for it to end, even if you have set a timeout. The timeout only applies to waiting for the start of a pulse. You can see that by taking a look at the source code, which is posted on Google:

http://code.google.com/p/arduino/source/browse/trunk/hardware/cores/arduino/wiring_pulse.c

If you decide to use interrupts you will have to switch to a different pin. The Arduino only supports hardware interrupts on digital pins 2 and 3. You can read about that here:

http://arduino.cc/en/Reference/AttachInterrupt

[u]Posted by: billh[/u]

If you decide to use interrupts you will have to switch to a different pin. The Arduino only supports hardware interrupts on digital pins 2 and 3. You can read about that here:

http://arduino.cc/en/Reference/AttachInterrupt

I went to the address you give, and after the interrupt explanation, this example is given. But I don't see where pins 2 or 3 fit in

Example

int pin = 13;
volatile int state = LOW;

void setup()
{
  pinMode(pin, OUTPUT);
  attachInterrupt(0, blink, CHANGE);
}
void loop()
{
  digitalWrite(pin, state);
}

void blink()
{
  state = !state;
}

How is pin 2 or pin 3 affected? How is one or the other connected? Thanks for the help PW

The Arduino allows two external user interrupts but must be wired to either pins 2 or 3 only.

The function: attachInterrupt(interrupt, function, mode) is where interrupt 0 means input pin 2 and interrupt 1 means input pin 3. You have no other option on which pins can be used. Note that you don't have to set the pin mode for 2 or 3 as the interrupt function does that internally. For the sketch you showed there must be a wire jumper used between pin 13 and pin 2 so that when pin 13 changes it generates a interrupt and the interrupt code switches the value for the next pin 13 write and on and on....

That make sense?

Lefty

That's almost right. The example sketch for attachInterrupt() assumes you have an encoder or some other device attached to pin 2 and you want to blink the LED on pin 13 every time the encoder clicks. You don't jumper pins 13 and 2 together; pin 13 is for the LED and pin 2 is for your external device.

Retrolefty is right that pins 2 and 3 correspond to interrupts 0 and 1. This designation is determined by the AVR hardware. To quote the attachInterrupt() documentation:

Specifies a function to call when external interrupt 0 or 1 occurs, on digital pin 2 or 3, respectively. Replaces any previous function that was attached to the interrupt.

There's nothing directly in the sketch about pin 2. It is implicit in the call to attachInterrupt(). The first argument specifies that the handler is being attached to interrupt 0, which corresponds to pin 2.

Thanks everybody. I believe I've got it. What I would like to do, is attach the output of a multivibrator to pin 2, or 3, then count the pulses per a chosen time period, for instance pulses per second. If I can use the method of interrupts to do so.

Thanks