# daily routine - state diagram [SOLVED]

#### Pushkar1

Joined Apr 5, 2021
324
I still feel like I'm not as good at drawing state pictures as I should be. So I thought I should try something simple first. I thought to myself that why not make a state diagram of daily routine I have decided to make a state diagram in which I want to show the complete status from waking up in the morning till going to sleep at night. Everything i do, it should be considering a state and the time i do it is considering the state of the input.

I think this is the method that will make it easier for me to create state diagrams for embedded systems. I would like to get advise from members I do not understand what can be my other inputs other than time

I would appreciate any suggestion, advice before making state diagram for daily routine.

Last edited:

#### Irving

Joined Jan 30, 2016
2,189
For every state there is a function that determines from a set of inputs, one of which could be 'time now' or 'time in state', what the followng state should be, the default outcome being 'no change of state'.

There are various ways to diagram this, but all show the conditions for transferring from one state to another. A common modelling method is UML, as described here.

#### click_here

Joined Sep 22, 2020
444
A state diagram is something that I have used for programming a few times and is really good when you are using a "state machine" architecture.

The more states you have, the more functionality you have.

You want to think about states that you might sometimes need, like picking up the toothpaste if you accidentally drop it.

It would be a great idea to make a small program that uses a state machine, because it is a strong architecture.

I suggest that you make a small program that runs off a state machine, something like: wait for button press, de-bounce delay, led on, wait for button to lift, led off. That way after you make your diagram you can test with some code.

The trick with making a state machine with code is to never use "blocking code" - That way you can introduce multitasking later on. (If you want to do that I'll explain when you get there)

#### Yaakov

Joined Jan 27, 2019
3,603
For every state there is a function that determines from a set of inputs, one of which could be 'time now' or 'time in state', what the followng state should be, the default outcome being 'no change of state'.

There are various ways to diagram this, but all show the conditions for transferring from one state to another. A common modelling method is UML, as described here.
UML is really great stuff but it is huge. I used it extensively because of its capacity for dealing with parallel and blocking operations at the same time which simple flowcharts can't do. But, I only used a subset of it. It has some very good tools.

#### Pushkar1

Joined Apr 5, 2021
324
I made a state diagram showing my daily activities. I look displayed time on my clock and I do following activity over time

This state diagram is not looking as I wanted to show. There are two drawback in it.

1. In any state diagram, there should be an incoming and outgoing arrow between two states. But it has only one outgoing arrow between two state. ?

2. Secondly, I do not understand that the time that I have shows in AM and PM, if I want to write it in code, then what will I have to write in condition for state?

I would write code, as in the following example:

C:
enum DailyRoutine {State1, State2, State3, State4, State5, State6, State7, State8, State9} StateFlag;

int main(void)
{
StateFlag = State1;
while(1)
{
switch ( StateFlag)
{
case State1 :
// I wake UP at 6.00 AM morning
case State2 :
// I have break fast
case State3 :
// I go to college
case State4 :
// I have lunch
case State5 :
// I go to Home
case State6 :
// I Play Games
case State7 :
// I do my homework
case State8 :
// I have dinner
case State9 :
// I go to bed
}
}

return (0);
}
Timer interrupts can also be used for non-blocking code but will be discussed later

Last edited:

#### ericgibbs

Joined Jan 29, 2010
13,873
hi P,
What IF your phone rings or someone knocks on your door, while you are running your daily routine.?

For the Time, the 24 hour clock format works well.
E

#### Pushkar1

Joined Apr 5, 2021
324
hi P,
What IF your phone rings or someone knocks on your door, while you are running your daily routine.?
If I'm doing some work and suddenly I get a call, I'll leave work and attend the phone call first.

If I'm doing some work and suddenly someone rings my doorbell, I'll leave my work and I'll go to open the door

#### ericgibbs

Joined Jan 29, 2010
13,873
Morning P,
So how would you classify that phone/door activity.?

Ref your Code, how do you change State for the activity in the Switch Case.?

E

#### click_here

Joined Sep 22, 2020
444
"Go to college"
Surely this could be broken into different states.

You need to put "events" in.

Events are the reason you go from one states to another.

State1 - At home
Event1 - Clock was >= 9am
State2 - Walk to car
Event2 - Arrived at car
State3 - Located outside car
...

You could break this down to painfully small states, but you need to decide what is not a state and what is an event - Is walking to the car a state, or is it the "event" that gets you to the next state?

#### Pushkar1

Joined Apr 5, 2021
324
Morning P,
So how would you classify that phone/door activity.?

Ref your Code, how do you change State for the activity in the Switch Case.?
This code show how state change from one state to other. When I enter into any state I get activity in state
C:
#include <stdio.h>

int main()
{
enum Routine { State1, State2, State3 }StateFlag;

printf("state number : (0. State1, 1. State2, 2. State3, wrong input):");
scanf("%d", &StateFlag);

switch (StateFlag)
{
case State1:
printf("I wake Up");
break;
case State2:
printf("I eat break fast");
break;
case State3:
printf("I go to college");
break;
default:
printf("Something went wrong ");
}

return 0;
}

#### Irving

Joined Jan 30, 2016
2,189
UML is really great stuff but it is huge. I used it extensively because of its capacity for dealing with parallel and blocking operations at the same time which simple flowcharts can't do. But, I only used a subset of it. It has some very good tools.
Agreed, UML is heavyweight, but as a discipline to guide in the technique it works. As you suggest a limited subset serves 90% of whats needed and the more esoteric stuff can be learnt/used when necessary.

#### click_here

Joined Sep 22, 2020
444
This code show how state change from one state to other. When I enter into any state I get activity in state
C:
#include <stdio.h>

int main()
{
enum Routine { State1, State2, State3 }StateFlag;

printf("state number : (0. State1, 1. State2, 2. State3, wrong input):");
scanf("%d", &StateFlag);

switch (StateFlag)
{
case State1:
printf("I wake Up");
break;
case State2:
printf("I eat break fast");
break;
case State3:
printf("I go to college");
break;
default:
printf("Something went wrong ");
}

return 0;
}
Your code should change the states, not input from the user.

I'm guessing that the names "StateN" are just for these examples, but make sure that the states have more descriptive names.

Looking at my example from an earlier, you'd want it to behave more like this...
Code:
    while(1)
{
switch (SystemState)
{
case WaitForButton:
{
if(isButtonPressed())
{
SystemState = WaitForDebounce;
debounceCount = 0;
}

break;
}

case WaitForDebounce:
{
debounceCount++;

if(debounceCount == THRESHOLD)
{
if(isButtonPressed())
{
SystemState = WaitForRelease;
}
else
{
SystemState = WaitForButton;
}
}

break;
}

case WaitForRelease:
{
if(!isButtonPressed())
{
SystemState = WaitForButton;
}

break;
}

default:
{
SystemState = WaitForButton;
}
}

}
Note how each state has an "event" that causes the state to change

#### Irving

Joined Jan 30, 2016
2,189
Here's another (nearly complete) example. The state diagram was drawn (badly by me) using Visual Paradigm (community edition) (good package if you take some time to learn it properly; free download, register for free educational use).

 Pedestrian Crossing: enum { OFF, ON, FLASH }; enum States { Green, GreenWait, Yellow, Red, RedEnding, RedFinal, RedYellow }; States state = Green; //initial state uint8_t time_in_state = 0; void loop() { switch(state) { case Green: { redMan(ON); beeper(OFF); greenMan(OFF); wait_sign(OFF); greenLight(ON); redLight(OFF); yellowLight(OFF); if(button_pressed){ state = GreenWait; } break; }// end Green case GreenWait: { wait_sign(ON); if(detect_gap_in_traffic) { state = Yellow; time_in_state = 0; } break; }// end Green_wait case Yellow: { greenLight(OFF); yellowLight(ON); time_in_state++; if(time_in_state == 5) { state = Red; time_in_state = 0; } break; }// end Yellow case Red: { yellowLight(OFF); redLight(ON); wait_sign(OFF); redMan(OFF); greenMan(ON); beeper(ON); time_in_state++; if(time_in_state == 20) { state = RedEnding; time_in_state = 0; } break; }// end Red case RedEnding:{ greenMan(FLASH); time_in_state++; if(time_in_state == 10) { state = RedFinal; time_in_state = 0; } break; }//end RedEnding case RedFinal:{ greenMan(OFF); redMan(ON); beeper(OFF); time_in_state++; if(time_in_state == 5) { state = RedYellow; time_in_state = 0; } break; }// end RedFinal case RedYellow:{ yellowLight(ON); time_in_state++; if(time_in_state == 10) { state = Green; time_in_state = 0; } break; }// end Red-Yellow }// end switch delay(1000); // bad but simple... }// end loop

Last edited: