Examples - HowTo - Software Timer Class
This class shows how to execute function in a certain interval.
/************************************************************************* * CLASS SoftwareTimer / Time event helper * Written By Christoph Wartmann CAAD / ETH Zürich 2010 * Description: Returns boolean true if special time interval is reached * * * PUBLIC METHODS ********************************************************* * Description: setup(int ms ) - Initialisation of softwareTimer w * Parameter: ms - Interval in milliseconds * * Description: getMillis() - returns millis since start * * Description: getSeconds() - returns seconds since start * * Description: getTimerEvent() - returns true if time interval is reached * **************************************************************************/ class SoftwareTimer { private: long prevMillis; // milliseconds counter int interval; // delay interval public: void setup(int ms) { prevMillis = 0; interval = ms; } public: unsigned long getMillis() { return millis(); } public: unsigned long getSeconds() { return millis()/1000; } public: boolean getTimerEvent() { if( millis()%interval == 0 ) { prevMillis = millis(); return true; }else{ return false; } } }; /* SoftwareTimer Example without delay function / and busy waiting Example: Alternative delay() function without busy waiting */ SoftwareTimer sT1; SoftwareTimer sT2; void setup() { Serial.begin(9600); sT1.setup(1000); sT2.setup(2010); } void loop() { if(sT1.getTimerEvent() == true) { Serial.print("sT1 Event: "); Serial.println(sT1.getMillis()); } if(sT2.getTimerEvent() == true) { Serial.print("sT2 Event: "); Serial.println(sT2.getSeconds()); } }