// FreeRTOS with a software timer.
//
// A software timer blinks the green led.
// A second timer turns the red led on for 2 seconds,
// after entering a new value.
const int blueLedPin = 7;
const int greenLedPin = 6;
const int redLedPin = 5;
TimerHandle_t blinkTimer;
TimerHandle_t newNumberTimer;
void setup()
{
Serial.begin(115200);
pinMode(greenLedPin,OUTPUT);
pinMode(blueLedPin,OUTPUT);
pinMode(redLedPin,OUTPUT);
// Create a software timer for blinking the green led,
// with an initial value of 500ms for the interval.
// The timer is not started by default,
// so start it to make the led blink.
blinkTimer = xTimerCreate("Blink",500/portTICK_PERIOD_MS,pdTRUE,(void *)0,timerCallBack);
xTimerStart(blinkTimer,1000/portTICK_PERIOD_MS);
// Create a single-shot timer for the red led for 2 seconds.
newNumberTimer = xTimerCreate("newNumber",2000/portTICK_PERIOD_MS,pdFALSE,(void *)0,newNumberCallBack);
// Create a task.
xTaskCreate(ReadSerialTask,"ReadSerial",2000,NULL,1,NULL);
}
void loop()
{
// Do something with the task running the Arduino loop().
digitalWrite(blueLedPin,HIGH);
delay(200);
digitalWrite(blueLedPin,LOW);
delay(350);
}
void ReadSerialTask(void * pvParameters)
{
// A buffer of 20 characters should be enough.
// The 'index' is the first free index where the
// character can be stored.
char buffer[20];
int index = 0;
Serial.println();
Serial.println("Enter a number (it will be the delay/interval for the green LED).");
while(true)
{
if(Serial.available() > 0)
{
char newChar = (char) Serial.read();
if(newChar == '\n' or newChar == '\r')
{
// The end of the input is found.
// Put a zero terminator at the current location.
// Use the integer for the new interval.
buffer[index] = '\0';
int number = atoi(buffer);
xTimerChangePeriod(blinkTimer,number,1000/portTICK_PERIOD_MS);
// Turn the led on, let the timer callback function turn it off.
digitalWrite(redLedPin,HIGH);
xTimerStart(newNumberTimer,1000/portTICK_PERIOD_MS);
// Clear the buffer.
index = 0;
}
else
{
// Store the new character and advance the index.
buffer[index] = newChar;
index++;
// An array of 20 elements is [0] up to [19].
// If the buffer is filled up to [19], then
// the character at [19] will be overwritten
// by the zero-terminator.
if(index > 19)
index = 19;
}
}
else
{
// There was nothing available at the Serial input.
// The Arduino function Serial.available() has no timeout
// to work together with FreeRTOS.
// This task does not need to run at 100% checking the
// Serial input. Once every 1...100 ms is fast enoughy.
// Let's use something between 1 and 100: 10 ms
delay(10);
}
}
}
// The callback function for the led blinking timer.
void timerCallBack(TimerHandle_t xTimer)
{
// toggle the led.
// Using a static variable to remember if the led is on or off.
static bool On = false;
if(On)
digitalWrite(greenLedPin,HIGH);
else
digitalWrite(greenLedPin,LOW);
On = !On; // boolean invert
}
// The callback function for the single shot timer.
void newNumberCallBack(TimerHandle_t xTimer)
{
// turn the led off.
digitalWrite(redLedPin,LOW);
}
loop()
timerCallBack()
Demonstration of a software Timer in FreeRTOS.
new value set