state machine diagram for task switching

Thread Starter

rt694157

Joined Dec 15, 2019
78
I am learning to make state machine diagram. consider LED and Motor is connected to output pin of microcontroller. I want to turns on and off LED without using any delay function in program. Can I run motor at the same time without being interrupted by the LED?. I am sure it can done If this can happen, how will the diagram of Satay machine be made for this?

I made following one
1587989121544.png

Updated : consider LED and Motor is connected to output pins of microcontroller.
 
Last edited:

djsfantasi

Joined Apr 11, 2010
9,156
Apparently you don’t have to worry about running the motor. Wire it so one lead is left unconnected. The motor will always be stopped, per your diagram.

Your description implied that both the motor and LED are connected to one GPIO pin. Thus, of course the LED state will affect the motor state. Now, if they were on separate pins, it’s all up to what your code says.
 

Thread Starter

rt694157

Joined Dec 15, 2019
78
Apparently you don’t have to worry about running the motor. Wire it so one lead is left unconnected. The motor will always be stopped, per your diagram.

Your description implied that both the motor and LED are connected to one GPIO pin. Thus, of course the LED state will affect the motor state. Now, if they were on separate pins, it’s all up to what your code says.
I made a mistake, Both devices are connected with different pins. At the same time neither the LED can be turns on and off nor the motor can be start and stop. When LED is on It would wait for 30ms then it would turn off without using any delay function for 30ms in program. Can I run motor at the same time without being interrupted by the LED?
 

djsfantasi

Joined Apr 11, 2010
9,156
Yes, I believe you can. Follow this sequence:
  1. Loop
  2. Turn the LED On
  3. Calculate time when to turn LED off
  4. Motor control goes here
  5. If it’s time, turn the LED off
  6. End loop
I’m assuming you don’t have any code yet, so I can’t demonstrate with code. Do you have any code?

By the way, is this homework?
 
Last edited:

Papabravo

Joined Feb 24, 2006
21,159
Your diagram is faulty. It shows 2-states, which is fine, but the arcs are labeled with actions, which is not fine. Arcs need to be labeled with input conditions. There are two way to handle outputs. They can be a function of the state you are in ( a Moore machine), or they can be a function of the transition ( a Mealy machine). You need to be cognizant of the distintion between the two different formulations.

https://en.wikipedia.org/wiki/Moore_machine
https://en.wikipedia.org/wiki/Mealy_machine

If those are too deep for you, then just watch the YouTube videos. That is what millennials do, I hear tell.
 

Thread Starter

rt694157

Joined Dec 15, 2019
78
Is this homework? You didn’t answer my question...
It's not homework, I was looking example for multitasking I got example in the link, more then one task's may be run in multitasking. CPU only execute one task at one time

There are only two task ,Turn on and turn off LED and Run Motor, How they both task will complete without delay function using multitasking?
 

Thread Starter

rt694157

Joined Dec 15, 2019
78
Your diagram is faulty. It shows 2-states, which is fine, but the arcs are labeled with actions, which is not fine. Arcs need to be labeled with input conditions. There are two way to handle outputs. They can be a function of the state you are in ( a Moore machine), or they can be a function of the transition ( a Mealy machine). You need to be cognizant of the distintion between the two different formulations.
I copied the image from here https://en.wikipedia.org/wiki/State_diagram and then posted the image by editing it in paint.

Is there any difference between a state diagram and a state machine? I don't think so if there is any difference then which is best for multitasking
 

djsfantasi

Joined Apr 11, 2010
9,156
It's not homework, I was looking example for multitasking I got example in the link, more then one task's may be run in multitasking. CPU only execute one task at one time

There are only two task ,Turn on and turn off LED and Run Motor, How they both task will complete without delay function using multitasking?
You already have the answer. It’s on the article you posted. I provided you with a simple algorithm.

If after you re-read the article and have specific questions, ask them here. Otherwise, I’m a software engineer, not a mind reader, dammit! ;)
 

Papabravo

Joined Feb 24, 2006
21,159
I copied the image from here https://en.wikipedia.org/wiki/State_diagram and then posted the image by editing it in paint.

Is there any difference between a state diagram and a state machine? I don't think so if there is any difference then which is best for multitasking
There is a 1:1 correspondence between a state diagram and a machine that implements it. That is the whole purpose of the diagram, to correctly document what your intentions are. You have not done that. Furthermore I don't see any tasks in this diagram that are being apportioned execution based on their present state.

Imagine a set of tasks where each one of them is in one of the following states:

{RUNNING, READY_TO_RUN, WAITING, BLOCKED_FOR_I/O, BLOCKED_FOR_EVENT}

Periodically you have to prempt the task that is running, and select one of the other tasks to run. How do you do that?
 

Thread Starter

rt694157

Joined Dec 15, 2019
78
You already have the answer. It’s on the article you posted. I provided you with a simple algorithm.

If after you re-read the article and have specific questions, ask them here. Otherwise, I’m a software engineer, not a mind reader, dammit! ;)
I do not know arduino programming and There is nothing for second task when motor will run and when motor will stop.
sequence
  1. Loop
  2. Turn the LED On
  3. Delay time ( Run motor without interrupted by the LED )
  4. turn the LED off
  5. End loop

Is it multitasking ?

C:
 int main ()
{
    LED ON;
    return 0;   
}

void ISR ()  // set interrupt for 1 seconds
{
   LED OFF;
   Motor RUN;
   }
 
Last edited:

Thread Starter

rt694157

Joined Dec 15, 2019
78
Imagine a set of tasks where each one of them is in one of the following states:

{RUNNING, READY_TO_RUN, WAITING, BLOCKED_FOR_I/O, BLOCKED_FOR_EVENT}

Periodically you have to prempt the task that is running, and select one of the other tasks to run. How do you do that?
I have drawn simple diagram How to convert it into state diagram?
1588046298768.png
 

Papabravo

Joined Feb 24, 2006
21,159
Sorry, you are missing too much background material to try and explain this in a forum post. A state machine is not like a flowchart. Did you read the Wikipedia material I pointed to?
 

djsfantasi

Joined Apr 11, 2010
9,156
I do not know arduino programming and There is nothing for second task when motor will run and when motor will stop.
sequence
  1. Loop
  2. Turn the LED On
  3. Delay time ( Run motor without interrupted by the LED )
  4. turn the LED off
  5. End loop
That isn’t what I posted. You’re missing my step 4...
 

Thread Starter

rt694157

Joined Dec 15, 2019
78
Sorry, you are missing too much background material to try and explain this in a forum post. A state machine is not like a flowchart. Did you read the Wikipedia material I pointed to?
int main ()
{
int N;
for (N=0; N<256; N++)
}

When we write a delay loop in a program, it only waste the processor time. I have to use this time for other motor task without without affecting led task Right now I have only two tasks, but when I have more than two tasks, it will difficult to arrange them to save time

That's something i want to do but I am struggling between two tasks. Now my biggest question is what do I need flow chart or state diagram or finite state machine ?
 

djsfantasi

Joined Apr 11, 2010
9,156
You don’t really need this, but I wrote it anyway. This little sample code snippet controls an LED and a motor independent of each other. The LED will turn on for 30ms and while it’s on, you can control the motor. The 30ms delay doesn’t block any other code.

I haven’t compiled this, so there might be typographical errors. And I haven’t tested it, so it is possible but not likely :rolleyes: there is an error.

At the least, it is an example of multi-tasking code. I changed the delay to three seconds, to give you an opportunity to see the code in action.

C:
// Multi-Tasking
// define constants here
const int statusLEDPin 2
const int statusMotorPin 3
const int pinMotor 7
const int pinLED 8
const int myDelay 3000 // ms

// global variables used to
// control motor and LED
unsigned long int LEDOn=0;
boolean Motor=false

// use interrupts to change
// device status. Pushbuttons
// connect to pins that can
// generate an interrupt
// (2&3 on an Arduino)


Setup() {
   // initialize pins
   pinMode(statusLEDPin, INPUT_PULLUP); // push button
  pinMode(statusMotorPin, INPUT_PULLUP); // push button
   pinMode(pinLED,OUTPUT);
   pinMode(pinMotor,OUTPUT);

   attachInterrupt(statusLEDpin, myISR_L, FALLING);
   attachInterrupt(statusMotorPin, myISR_M, FALLING);
}



// interrupt routine to toggle
// motor on and off
void myISR_M() {
   Motor = ! Motor;
   }


// interrupt routine to signal
// LED to turn on for 30ms
voidmyISR_L() {
   LEDOff = millis()+myDelay;
   // calculate turn off time
   }


// main routine
void main () {
  while (true)
  // if it’s time to turn off LED
    If (millis()>LEDOff) {
         digitalWrite(myPin_L, LOW)
     } else {
         //  turn LED on
         digitalWrite(myPin_LED, HIGH);
         }


// control motor
    If (Motor) {
        // turn motor on
        } else {
        // turn motor off
        }
    }
}
 
Last edited:
Top