# Interupts using the millis function

Question. So I was reading the reference guide for the language and I was reading about interupts. I have a question. I'd like to know the time it takes between to interupts. I read somethign that said something like, millis() will not increment inside the interrupt function. Hence, if I call it at the beginning, it will just give me the approx. time that the program interrupted?

Thanks

What do you mean? Can you tell us the basic idea of your program? Because as long as your arduino is not going into sleep between interrupts, millis() should still advance.

Hence, if I call it at the beginning, it will just give me the approx. time that the program interrupted?

Yes. During an ISR, other interrupts are disabled. Since millis() relies on interrupts to know that time has elapsed, it does not return different values during your ISR. That shouldn't really matter, because your ISR should be done by the time millis() would return a different value, anyway.

I'm reading serial data from my EM-406A GPS just using interrupts. Bewdiful!

``````#define PERIOD      208
#define HALF_PERIOD 104
#define BYTE_PERIOD 2000

boolean polarity = true;
unsigned long previousTime = 0;
boolean wait = true;
int bitPosition = 0;
byte output = 0;

void doChar(char letter)
{
Serial.print(letter);
}

void buildByte(unsigned long interval)
{
polarity = !polarity;
interval += HALF_PERIOD;
while(interval > PERIOD)
{
output = output >> 1;              // right shift
if(polarity) output |= B10000000;  // set MSB = 1
interval -= PERIOD;
if(bitPosition++ > 9)
{
if(interval > BYTE_PERIOD) // big gap
{
bitPosition = 2;
polarity = true;
}
else
{
bitPosition = 0;
doChar(output);  // Do whatever when you have a character
}
break;
}
}
}

void interrupt()
{
unsigned long currentTime = micros();
unsigned long interval = currentTime - previousTime;
previousTime = currentTime;
buildByte(interval);
}

void setup()
{
Serial.begin(115200);
Serial.println("setup()");
attachInterrupt(1, interrupt, CHANGE);
}

void loop(){}
``````