I have no idea why the serial connection is lost but I can tell you that what you call rpm is not sparks per minute or engine rpm for more than one reason.
A) millis() is time since the sketch started running. It will increase for about 2 months before rolling over at a rate of 1000/second. Your code time=0 does not reset millis().
B) With the delay(1000) there you are not measuring sparks per anything but chance and error.
Perhaps it has something to do with your actual wiring.