at least I remember that we used pointers (in highschool) instead of global variables ...
For what?
For instance,
int vall, valr;
int perl, perr;
int prevperl = 0, prevperr = 0;
are all global variables that do not need to be. There are only referenced in loop().
How would you use pointers instead of global variables? I can't see that that makes sense. Just make these local variables.
Now, you do want the values in the last two to persist between calls to loop(), so they should be static.