0

I am trying to draw a FSM diagram for a vending machine. The machine accepts nickles,dimes,quarters, half dollars, and dollar bills. There are 4 selections you can choose from. 3 are $1.15 and 1 is $1.50. Change is to be given if the person inserts more than the item is valued at.

FSM inputs

The cash receiver produces a 3-bit encoded value indicating no-coin/nickel/dime/quarter/half-dollar/dollar. The comparator produces a 2-bit encoded signal indicating the result of comparing its input to the "cash box" value.

The item selector produces a 3-bit encoded value representing a selection to be purchased (dispensed).

My question is, will I need to have a state for each dollar value there can be? Right now I am trying it and I have close to 50 states and I am not even at the item selection part of the diagram. Is there a simpler way?

  • You are way over-complicating this problem. The vending machine only cares about a few things: what item you selected, how much it costs, and how much money has been inserted. When you insert money, it doesn't necessarily change the state of the vending machine. If an action changes how the vending machine behaves, then you have a transition to a new state. – mstbaum Apr 21 '15 at 20:45
  • So when money is inserted, I dont change states? How do I keep track of how much money is in the machine to know if an item can be bought? If I am at the beginning state, and I insert four quarters, a dime and nickle, do i not change states at all until I am ready to select an item? When do I know i have enough money to move to the next state – user3370950 Apr 21 '15 at 21:08
  • Correct, because the amount of money inserted does not change how the machine behaves. You could also think of it as transitioning to the same state. Then once you make the selection, you can transition to the next state. You can think of the states as tied to a set of operations the machine is allowed to perform. In State A, the machine can only accept money. In state B, the machine can only dispense items. In state C, the machine can only give change. Something like that. Your transition conditions are based on the comparison between cost and money inserted and the item selected – mstbaum Apr 21 '15 at 21:14

1 Answers1

1

As FSMs do not provide means for quantitative values you will have to model these by states. This will result in an explosion of the number of states. Thats the resin why most reactive systems are modeled using extended strata machine concepts like Harel statecharts. These allow to use variables within a state machine which make it much simpler. A statechart for your case may look like this:

vending state machine find a larger version here: vending machine statechart

I hope, that the meaning of the state machine is self explaining... This state chart defines a state machine that differs from FSM in various important aspects.

  1. First you can define variables (left side) to holde quantitative values like number of coins price etc.
  2. Choices (small rhombus) let you to structure transitions and thus refuse transition complexity
  3. The rectangles named 'payment' and 'article supply' are parallel regions. Each region has its own active state. so the overall state is the combination of both active states. An FSM would require to define the cross product of the states of all parallel regions.

Using these mechanism is the only approach to maintain an meaningful number of states. By the way the example is build using the open source Yakindu Statechart Tools (on statecharts.org). It allows you to model and interactively simulate the models as well as generating state machine code.

Axel T.
  • 166
  • 5