//#include <Arduino.h>
#define MAX_TASKS 5
typedef void (*TaskFunction)();             // Task function type
typedef struct {
  TaskFunction taskFunction;                // Function to execute the task
  unsigned long interval;                   // Timing interval (ms)
  unsigned long lastRun;                    // Timestamp of the last execution
  bool isActive;                            // Task active state
} Task;
Task taskList[MAX_TASKS];                   // Array to store tasks
int taskCount = 0;
// Add a new task to the scheduler
void addTask(TaskFunction func, unsigned long interval) {
  if (taskCount < MAX_TASKS) {
    taskList[taskCount].taskFunction = func;
    taskList[taskCount].interval = interval;
    taskList[taskCount].lastRun = 0;
    taskList[taskCount].isActive = true;
    taskCount++;
  }
}
// Scheduler
void runScheduler() {
  unsigned long currentTime = micros();//millis();
  for (int i = 0; i < taskCount; i++) {
    if (taskList[i].isActive && (currentTime - taskList[i].lastRun >= taskList[i].interval)) {
      taskList[i].taskFunction();           // Execute the task
      taskList[i].lastRun = currentTime;    // Update timestamp
    }
  }
}
// Example tasks
void task1() {
  Serial.println("[Task1] Running...");
}
void task2() {
  Serial.println("[Task2] Running...");
}
void task3() {
  Serial.println("[Task3] Running...");
}
void setup() {
  Serial.begin(9600);
  // Register tasks
  addTask(task1, 1000000);     // Runs every 1 second
  addTask(task2, 2000000);     // Runs every 2 seconds
  addTask(task3, 50000);        // Runs every 0.05 seconds
}
void loop() {
  runScheduler();              // Execute the scheduler
}