# use of time.avg() in a function

**URL:** <https://forums.openmv.io/t/use-of-time-avg-in-a-function/1013>\
**Category:** OpenMV Boards\
**Created:** [November 22, 2018, 3:08pm UTC](https://forums.openmv.io/t/use-of-time-avg-in-a-function/1013 "2018-11-22T15:08:06Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![chrisPyres66](https://yyz1.discourse-cdn.com/flex029/user_avatar/forums.openmv.io/chrispyres66/32/39_2.png) [@chrisPyres66](https://forums.openmv.io/u/chrisPyres66)\
**Post date:** [November 22, 2018, 3:08pm UTC](https://forums.openmv.io/t/use-of-time-avg-in-a-function/1013/1 "2018-11-22T15:08:06Z")

</div>

Dear all,

I try to get the time between two interrupts from a push on a button with the code :

```python
import time, pyb

testClock = time.clock()

def test(line) :
   print( testClock.avg() )
   testClock.tick()
   return

testClock.tick()
pyb.ExtInt('P7', pyb.ExtInt.IRQ_FALLING, pyb.Pin.PULL_UP, test)

while(True):
   time.sleep(10)

```

In openmvIDE, i have the error :  
Uncaught exception in ExtInt interrupt handler line 12  
MemoryError:

Any help could be pleased, Thanks,

---

<div class="post-metadata">

**Author:** ![iabdalkader](https://yyz1.discourse-cdn.com/flex029/user_avatar/forums.openmv.io/iabdalkader/32/1169_2.png) [@iabdalkader](https://forums.openmv.io/u/iabdalkader)\
**Post date:** [November 23, 2018, 8:34pm UTC](https://forums.openmv.io/t/use-of-time-avg-in-a-function/1013/2 "2018-11-23T20:34:32Z")

</div>

You can’t call any functions that allocate memory in an interrupt handler. Try something like this:

```python
import time, pyb

def test(arg):    
    global ticks_passed, last_tick, extint, switch
    switch = True
    extint.disable()
    ticks_passed = time.ticks() - last_tick
    last_tick = time.ticks()
    
switch = False
last_tick = time.ticks()
ticks_passed = 0
extint = pyb.ExtInt('P7', pyb.ExtInt.IRQ_FALLING, pyb.Pin.PULL_UP, test)

while(True):
    if (switch):
        switch = False
        print(ticks_passed)
        extint.enable()

```
