/*
*/
#include <Arduino.h>
#define BLINK_OUTP 32
#define RESET 0
int iScanCounter = 0;
char cBuff[30];
char cOutput[200];
bool bPrint = false;
bool bToggle = false;
unsigned long lLoopTimer;
unsigned long lLoopDelay = 2000;
/////////////////////////////////////////////////////////////////
void task_one_second();
// #define _TASK_SCHEDULING_OPTIONS
// #define _TASK_TIMECRITICAL
// #define _TASK_MICRO_RES
// #define _TASK_SLEEP_ON_IDLE_RUN
#include <TaskScheduler.h>
Scheduler userScheduler; // to control tasks
Task taskResetCount(TASK_SECOND, TASK_FOREVER, &task_one_second);
/////////////////////////////////////////////////////////////////
void task_one_second() {
// char * itoa ( int value, char * str, int base );
itoa(iScanCounter, cBuff, 10);
strcpy(cOutput, "Scan Count = ");
strcat(cOutput, cBuff);
iScanCounter = 0;
// if (iScanCounter < 500)
// lLoopDelay -= 5;
// if (iScanCounter > 500)
// lLoopDelay += 5;
bPrint = true;
bToggle = !bToggle;
if (bToggle == true)
digitalWrite(BLINK_OUTP, HIGH);
else
digitalWrite(BLINK_OUTP, LOW);
}
/*
/////////////////////////////////////////////////////////////////
bool timer_phasing_micros(unsigned long lTime) {
static unsigned long lTimeLast = 0L;
static unsigned long lStartTime = 0L;
if (lTimeLast != lTime) {
lTimeLast = lTime;
lStartTime = micros();
}
// subtraction method avoids problems with rollover
if (micros() - lStartTime >= lTime) {
return true;
}
return false;
}
*/
/////////////////////////////////////////////////////////////////
void setup() {
Serial.begin(115200);
while (!Serial)
delay(10); // wait for serial port to open
pinMode(BLINK_OUTP, OUTPUT);
Serial.println("Scheduler starts");
// taskResetCount.setSchedulingOption(TASK_SCHEDULE);
userScheduler.init();
userScheduler.addTask(taskResetCount);
taskResetCount.enable();
lLoopTimer = micros();
}
//////////////////////////////////////////////////////////////////////
void loop() {
userScheduler.execute();
/*
// until task_pwm_phasing() works on scheduler, use micro timer
if (timer_phasing_micros(lInterval)) {
timer_phasing_micros(RESET); // requires reset
task_pwm_phasing();
}
*/
if (bPrint) {
bPrint = false;
Serial.println(cOutput);
}
iScanCounter++;
while ((micros() - lLoopTimer) < lLoopDelay) // 2000 should equal 500 scans
;
lLoopTimer = micros(); // reset loop timer
}