I changed my code to the following:
Easy.cpp:
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include "Easy.h"
Routine::Routine(){
go = true;
prev = 0;
prev2 = 0;
prev_us = 0;
prev_us2 = 0;
}
void Routine::begin(unsigned long interval, void (*g)()){
//static unsigned long prev = 0;
if (millis() - prev >= interval && go){
g();
prev = millis();
}
}
void Routine::begin(unsigned long interval, void (*g)(), word times){
//static unsigned long prev = 0;
static word counter = 0;
if (millis() - prev2 >= interval && go && counter < times){
g();
prev2 = millis();
counter++;
}
}
void Routine::end(){
go = false;
}
void Routine::begin_us(unsigned long interval, void (*g)()){
//static unsigned long prev = 0;
if (micros() - prev_us >= interval && go){
g();
prev_us = micros();
}
}
void Routine::begin_us(unsigned long interval, void (*g)(), unsigned long times){
//static unsigned long prev = 0;
static unsigned long counter = 0;
if (micros() - prev_us2 >= interval && go && counter < times){
g();
prev_us2 = micros();
counter++;
}
}
void Routine::restart(){
go = true;
}
Easy.h:
#ifndef Easy_h
#define Easy_h
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
class Routine{
public:
Routine();
void begin(unsigned long interval, void (*g)());
void begin(unsigned long interval, void (*g)(), word times);
void end();
void restart();
void begin_us(unsigned long interval, void (*g)());
void begin_us(unsigned long interval, void (*g)(), unsigned long times);
private:
boolean go;
unsigned long prev;
unsigned long prev2;
unsigned long prev_us;
unsigned long prev_us2;
};
I used 4 private variables for the begin functions. Now it will multitask!