PaulS:
but they would be declared over and over
So? It doesn't matter. Make them static, so that doesn't happen, if it makes you feel better. (It won't make the Arduino feel any better.)
yep got the point...
static int perl, perr;
but this
int prevperl = 0, prevperr = 0;
needs to be declared only the first time, will static also solve this problem??