Hi inka,
in die Richtung hab ich auch mal was gebastelt, um die Sketches mit mehreren gezeiteten Aufgaben übersichtlich zu halten:
Code:
#include "SimpleTimerMillis.hpp"
using Timer = SimpleTimerMillis;
#define SEC *1000
void taskA() {
static Timer t(2 SEC, Timer::FOREVER, Timer::START_IMMEDIATELY);
if(t.elapsed()) Serial.println("TaskA");
}
void taskB() {
static Timer t(0.4 SEC, Timer::FOREVER, Timer::START_IMMEDIATELY);
if(t.elapsed()) Serial.println("TaskB");
}
void setup() {
Serial.begin(9600);
}
void loop() {
taskA();
taskB();
}
Die Mini-Bibliothek dazu:
Code:
#ifndef __SIMPLETIMER__
#define __SIMPLETIMER__
class SimpleTimerMillis {
using millis_t = unsigned long;
public:
static const bool ONCE = false;
static const bool FOREVER = true;
static const bool START_IMMEDIATELY = true;
SimpleTimerMillis() = default;
SimpleTimerMillis(millis_t timeSpan, bool forever, bool start=false)
: timeSpan(timeSpan), isContinous(forever) {
if(start) this->start();
}
void start() {
startTime = millis();
}
void start(millis_t timeSpan) {
this->timeSpan = timeSpan;
start();
}
bool elapsed() {
bool elapsed = millis()-startTime > timeSpan;
if(elapsed && isContinous) start();
return elapsed;
}
bool isCountious() {
return isContinous;
}
void once() { isContinous = false; }
void forever() { isContinous = true; }
private:
bool isContinous = false;
millis_t startTime = 0;
millis_t timeSpan = 0;
};
#endif //__SIMPLETIMER__
Lesezeichen