First of all, excuse me if this isn't the right site to post this question but I didn't know where to ask this.
I need to implement a FSM to represent a counter of people in a room, there are two possibilities,people can enter the room or leave the room. An empty room is guaranteed at the beginning and only one person can go through the door at one time.
To know wheter a person enters or leaves, there are 2 sensors A and B, which depending on the order of activation indicate if a person is entering or leaving. A then B indicates a person enters the room, B then A indicates a person exits the room.
Now, I have to implement this on arduino and make the FSM diagram but I'm having problems figuring out the diagram, even though I think I already got the C program right (haven't tested it)
So, basically here's part of my C/Arduino program
int currentPeople = 0;
PeopleState currentState = None;
enum PeopleState {
A,
B,
None
};
Now these are two functions that get called for their respective sensors
//Gets called every time sensor A is activated
void countPeopleA(){
if (currentState == None){
currentState = A;
} else if (currentState == B){
if(currentPeople > 0){
currentPeople--;
}
currentState = None;
}
}
//Gets called every time sensor B is activated
void countPeopleB(){
if (currentState == None){
currentState = B;
} else if (currentState == A){
currentPeople++;
currentState = None;
}
}
My concerns are these:
Are the states I defined correct for the problem? I mean, defining a state for whenever a sensor is activated is right or should the states be Entering and Leaving?
I think your states are fine. Do not confuse their names with what they represent. You need one state representing the case that the sensors, combined, have been activated an even number of times (this is also the initial state), one for an odd number of prior activations with the last having been A, and one for an odd number of prior activations with the last having been B.
How can I diagram the states?
You have three states. Write names or labels for them, draw circles around those, and draw labelled arrows for the transitions that occur with the different sensor activations. (See also below.)
I'm just learning about FSM so I have problems figuring it out, like how can you guarantee that if the room is empty a person doesn't leave? I can check for that condition in code but I don't know how to represent it.
You cannot represent that condition with a finite state machine unless you put an occupancy limit on the room. (The above comments suppose that you do not do so, and therefore that the count of room occupants is not part of the FSM state.) If you want to go that route then you need a different approach, with separate states for each occupant count. Since this is an introductory exercise, I'm inclined to think that that is not what you're intended to do.
Note also that whether you model an occupancy limit or not, there are some transitions that you have neither explicitly forbidden nor assigned behavior. Specifically, what happens if, starting from the initial state, one of the two sensors is triggered twice in a row? Imagine, for instance, that they are far enough apart to accommodate a person triggering one, then turning back in the doorway and triggering it again. If they are allowed (and maybe even if not) then they should be represented on your diagram. Whether allowed or not, your code should account for them.