Data Logging on event only

Start with the state change detection example - this shows you how to detect when the switch input has changed state.

Each time it changes state, write a message to the log. If you need to know the interval since the previous change you can calculate that by recording the time of the previous change and subtracting it from the current time, and include that in the message; remember to update the saved time to 'now' afterwards, ready for the next event. If you want to include a wallclock time in the log, use the usual techniques to get this from the RTC. However, don't use the RTC to calculate intervals and elapsed time and so on over short timescales - just use it to generate a timestamp for the data at the point of logging.

If you wrote the logged data directly to the serial port and stored it on a PC, you could do all this timestamping on the PC e.g. using the timestamping option in Realterm.