I'm not sure how you'd use this code, as it seems incomplete. For example, I don't see any reset of the ticks value. I'd suggest putting together an SSCCE that demonstrates exactly how it would be...