From chipKIT
Jump to: navigation, search


int createTask(taskFunc task, unsigned long period, unsigned short stat, void * var)


task  : Pointer to the task function
period  : Scheduling interval in milliseconds
state  : Task initial enable state
var  : Initial value for task variable


This function creates a new task. The function specified by task will be scheduled for execution period milliseconds from the time the function was called and then repeated every period milliseconds afterwards. The initial task execution enable state is specified by state. This can have the following values:

TASK_ENABLE The task is enabled and will be run at the specified intervals.
TASK_DISABLE The task is disabled and will not be run.
TASK_RUN_ONCE The task is enabled to run once and then will become disabled after running.
Number ‘n’ The task is enabled to run and will run ‘n’ times. After the ‘n’th execution, the task becomes disabled.

The initial value of the task variable is specified by var. The task variable is a 32-bit quantity stored with the task but not used by the task manager. This can be used for any purpose desired, but can in particular be used to store a pointer to a data structure containing parameters for the task function.

Return Value

If the task is successfully created, the task identifier is returned. If the task can’t be created, the value -1 is returned.

Conforming To

The Task Management Functions are specific to ChipKit and exceed the Arduino 1.6.x specification.


int led = 13;
int blink_id;
unsigned long blink_var;
void blink_task(int id, void * tptr) {
  digitalWrite(led, !digitalRead(led); // Toggle pin state
void setup() {
  pinMode(led, OUTPUT);
  blink_id = createTask(blink_task, 200, TASK_ENABLE, &blink_var);

See Also

createTask(), destroyTask(), getTaskId(), getTaskNextExec(), getTaskPeriod(), getTaskState(), getTaskVar(), setTaskPeriod(), setTaskState(), setTaskVar(), startTaskAt(),