Saving data each second

Hi,

I have the following code inside my draw method

record.loadFile("recording.xml");
	record.addValue("record:beatsPerMinute", heartRate);
	record.addValue("record:time", ofGetTimestampString());
	record.saveFile("recording.xml");

I want it to to save this information only once every second. Could anyone tell me how to do this?

Hi,
I think this code should go into the update loop, since there is nothing to draw:

deltaTime = ofGetElapsedTimeMillis() - recordTime;
if( deltaTime > 1000 ){
record.loadFile("recording.xml");
	record.addValue("record:beatsPerMinute", heartRate);
	record.addValue("record:time", ofGetTimestampString());
	record.saveFile("recording.xml");
	recordTime = ofGetElapsedTimeMillis();
}

Declare <int> recordTime; and <int> deltaTime; in header file, and Initialize to 0 in setup()

EDIT: you should use .setValue() instead of .addValue() if you don’t want to create a duplicate every second.
Also I think that record.loadFile(“recording.xml”); should be used to test if file is present and correctly encoded. See this:

deltaTime = ofGetElapsedTimeMillis() - recordTime;
if( deltaTime > 1000 ){
	if (record.loadFile("recording.xml")) {
		record.setValue("record:beatsPerMinute", heartRate);
		record.setValue("record:time", ofGetTimestampString());
		record.saveFile("recording.xml");
	}else{
		ofLog() << "\nrecording.xml file was not found in bin/data folder.";
	}
}