Yep, it's a scope problem. In the beginning of your main()
function, add this:
global firstevent
That should done it. Any variable that is not defined inside a function, is a global. You can access it straightly from any function. However, to modify the variable you'll need to write global var
in your function.
Example
This creates a local variable "firstevent" on the function:
firstevent = 0
def modify():
firstevent = 1
And this modifies the global variable 'firstevent'
firstevent = 0
def modify():
global firstevent
firstevent = 1