Flashing Three LED's

djsfantasi

Joined Apr 11, 2010
9,163
You have a list of “tasks” (you can be more specific by defining what YOU think a task is).

You also have a scheduler. It’s purpose is to run each task when appropriate. In your case, the scheduler is or is driven by the ISR. “Appropriate” in the example is defined by your table of start/stop times. With three LEDs, the task list consists of six entries (2 tasks x 3 LEDs)

When a task is scheduled to run, you execute its code. This is determined by a “scheduler “. In click_here’s example, the scheduler is the select/case construct testing if the tick count indicates whether or not to turn on/off a specific LED.

Otherwise, you wait for a scheduled event to occur. This is the while loop in the example.

Then, if it’s scheduled to run, you execute the tasks code. In the example, it’s just turning on/off an LED.

Thus, the example given on early IS a “cooperative multitasking program . There are different schemes for multi-tasking. There is cooperative multi-tasking in which a task runs until it returns control to the scheduler. There is multi-tasking by time slicing, in which a given tasks is only given so much time to complete before the scheduler is given back control. TRUE multitasking in implemented at the OS level and includes the control to spawn, determine state. and terminate individual programs.

In the example, the tasks are so simple, they are embedded in the scheduler (again, the select/case structure).

Note that in my description, if you replace “LED” with “device”, it has nothing to do with LEDs. A device could be an LED, servo, motor, sound player, sensor, etc…

I’ve written a cooperative multi-tasking program to implement run-time control of 12+ devices (RC servos) and other peripherals. It CAN support up to 255 tasks but in practice more than a dozen or two are only used. So I’m familiar with what you’re trying to do.
 
Last edited:

click_here

Joined Sep 22, 2020
548
Perhaps my biggest mistake is choosing an LED example for understanding multitasking that is creating confusion.
Actually I think that it is a great example for simple multitasking.

My example was using a technique that was introduced to me as "cooperative multitasking". It's where one thread gives up processing time for another, hence "cooperative". See here

You may ask "where are the threads?" - They are there as 3 separate state machines, one for each LED output

It *is* a minimal bare metal multitasking example :)

There is no magic, or complicated OS, it is as simple as it gets.

Admittedly there limitations to using this approach, but there are things you can do to overcome these by using more advanced strategies.

There are lots of ways to achieve multitasking, and it sounds to me like you are confusing a certain type of multitasking (presumably a type of "Preemptive multitasking"?) and multitasking as a broader concept.

As a side study, look up RTOS.
Agreed - If anything it might help clarify what you want to achieve :)
 

Thread Starter

Sparsh45

Joined Dec 6, 2021
143
I have written the code as you @click_here said using switch case for following requirements
look at Post #19, Table for reference

LED 1 change state every 100ms
LED 2 change state every 200ms
LED 3 change state every 300ms

Is it cooperative multitasking?

C:
LED1
LED2
LED3

enum LED_STATE ( STATE1 = 1, STATE2 = 2, STATE3 = 3)STATE;

void main ()
{
    unit8_t Timer1 = 0 ;
    unit8_t Timer2 = 0 ;
    unit8_t Timer3 = 0 ;

    LED1 = ON ;
    LED2 = ON ;
    LED3 = ON ;

    STATE = STATE1;

    while (1)
    {
        if (Timer_Flag == 1)
        {                        // Timer_Flag set every 10ms
            Timer_Flag = 0; 
        
            //increment each timer every 10ms
            Timer1++ ;
            Timer2++ ;
            Timer3++ ;
    
            switch (STATE)
            {
                case STATE1 :
                            if ( Timer1 == 10 )   // check if time is 100ms
                               {
                                 LED1 =~ LED1 ;   // change state of LED1
                                 Timer1 = 0;      // clear Timer1 for next time
                                 STATE = STATE2 ; // Move to next STATE
                               }
                               break;
                case STATE2 :
                            if ( Timer2 == 20 )   // check if time is 200ms
                               {
                                 LED2 =~ LED2 ;   // change state of LED2
                                 Timer2 = 0;      // clear Timer2 for next time
                                 STATE = STATE3 ; // Move to next STATE
                               }
                              break
                case STATE3 :
                              if ( Timer3 == 30 )  // check if time is 300ms
                              {
                                LED3 =~ LED3 ;     // change state of LED3
                                Timer3 = 0;        // clear Timer3 for next time
                                STATE = STATE1 ;   // return to STATE1
                              }
                              break;           
            }

}

// interrupt every 10ms
void ISR_Timer_Interrupt ()
{
    if ( T1IF = SET )
    {
      T1IF = 0;
      Timer_Flag = 1;
    }
}
 
Last edited:

djsfantasi

Joined Apr 11, 2010
9,163
I have written the code as you @click_here said using switch case for following requirements
look at Post #19, Table for reference

LED 1 change state every 100ms
LED 2 change state every 200ms
LED 3 change state every 300ms

Is it cooperative multitasking?

C:
LED1
LED2
LED3

enum LED_STATE ( STATE1 = 1, STATE2 = 2, STATE3 = 3)STATE;

void main ()
{
    unit8_t Timer1 = 0 ;
    unit8_t Timer2 = 0 ;
    unit8_t Timer3 = 0 ;

    LED1 = ON ;
    LED2 = ON ;
    LED3 = ON ;

    STATE = STATE1;

    while (1)
    {
        if (Timer_Flag == 1)
        {                        // Timer_Flag set every 10ms
            Timer_Flag = 0;
       
            //increment each timer every 10ms
            Timer1++ ;
            Timer2++ ;
            Timer3++ ;
   
            switch (STATE)
            {
                case STATE1 :
                            if ( Timer1 == 10 )   // check if time is 100ms
                               {
                                 LED1 =~ LED1 ;   // change state of LED1
                                 Timer1 = 0;      // clear Timer1 for next time
                                 STATE = STATE2 ; // Move to next STATE
                               }
                               break;
                case STATE2 :
                            if ( Timer2 == 20 )   // check if time is 200ms
                               {
                                 LED2 =~ LED2 ;   // change state of LED2
                                 Timer2 = 0;      // clear Timer2 for next time
                                 STATE = STATE3 ; // Move to next STATE
                               }
                              break
                case STATE3 :
                              if ( Timer3 == 30 )  // check if time is 300ms
                              {
                                LED3 =~ LED3 ;     // change state of LED3
                                Timer3 = 0;        // clear Timer3 for next time
                                STATE = STATE1 ;   // return to STATE1
                              }
                              break;          
            }

}

// interrupt every 10ms
void ISR_Timer_Interrupt ()
{
    if ( T1IF = SET )
    {
      T1IF = 0;
      Timer_Flag = 1;
    }
}

Thus, the example given on early IS a “cooperative multitasking program .
 

Thread Starter

Sparsh45

Joined Dec 6, 2021
143
understood, it's cooperative multitasking.

When I was searching for Cooperative Multitasking I came across term Cooperative Scheduling. Is it also example of cooperative Scheduling?

It seems to me that it is co-operative scheduling as well as. But I'm not sure if I'm right or wrong
 

djsfantasi

Joined Apr 11, 2010
9,163
understood, it's cooperative multitasking.

When I was searching for Cooperative Multitasking I came across term Cooperative Scheduling. Is it also example of cooperative Scheduling?

It seems to me that it is co-operative scheduling as well as. But I'm not sure if I'm right or wrong
I’ve either forgotten or never heard of the term cooperative scheduling, so I had to look it up (click on the link).

After reading that article, do you think that the program is an example of cooperative scheduling? On one hand, since each task runs to completion, you can say that it is. On the other hand, the term includes a reference to on OS and multiple processes. The program is a single process and there is no OS on most micro controllers.

So, I’d invent my own term and call it cooperative scheduling-like. Or just a scheduler.
 

click_here

Joined Sep 22, 2020
548
I have written the code as you @click_here said
Well, nearly!

Each switch statement is supposed to be a separate thread, so you only have one thread.

To me it looks more like a loose state machine.

It might help as an exercise to write code using these states...
Code:
enum LedStates {waiting, processing, reset};
Any LED can fit into one of those 3 states.

Use a separate switch/case for each LED.

"waiting" is the state you are in before the elapsed time has occurred

"processing" is toggling the outputs

"reset" is clearing the timer
 
Top