Data Logging on event only

chrissybing:
The problem i have is the number of times the switch is thrown (1000+). As it records the switch state every 5000 milli seconds (over a period of up to 7 days (n = 1,209,600 readings), i then have to aggregate all the highs and lows when i collect the data before i can process it. If it just recorded the state every time the switch was triggered and then gave me the time it spent in each state, that would give me data that could be processed immediately.

That seems a very different requirement from your original post.

Would it be sufficient just to have two unsigned long variables (one for ON time and one for OFF time) and whenever the switch state changes just add the elapsed time to the relevant variable. You could also keep a count of the events in another variable. At the end of 7 days you have three values to work with.

And rather than test the switch position every 5 seconds, just do stuff when the switch changes.

If I have misunderstood your requirement please explain it in more detail.

...R