Are you talking about this isr?
void stateInv(){
y = 1;
}
How fast is this gear turning. Have your tried to put a timed lock out in this interrupt like in the countingballs()?
Have you tried any RC hardware filtering on this interrupt input pin?
If the gear is slow enough, and you eliminate the delays from your program so it is not blocking, you may be able to replace the interrupt with noise resistant code which expects to see an input in a certain state for a period of time.