Think we'll need to see the code to get further progress/inspiration...
Can you determine if the system is reseting or not?