When running your code, I am not getting the error. Are you sure you posted exactly the code that produced the error?
BTW, you should usewarnings. They will tell you that $readcounter is used only once (you probably wanted $readcounter++? It does not apease the warning, but works.)