Model a Vending Machine by Using Mealy Semantics
This example shows how to use Mealy semantics to model a vending machine. Mealy charts compute outputs only in transitions, not in states. For more information, see Design Considerations for Mealy Charts.
Logic of the Mealy Vending Machine
In this example, the vending machine requires 15 cents to release a can of soda. The purchaser can insert a nickel or a dime, one at a time, to purchase the soda. The chart behaves like a Mealy machine because its output soda
depends on both the input coin and current state:
got_0
— No coin has been received or no coins are left.
If a nickel is received (
coin == 1
), outputsoda
remains 0, but stategot_nickel
becomes active.If a dime is received (
coin == 2
), outputsoda
remains 0, but stategot_dime
becomes active.If input coin is not a dime or a nickel, state
got_0
stays active and no soda is released (outputsoda = 0
).
got_nickel
— A nickel was received.
If another nickel is received (
coin == 1
), stategot_dime
becomes active, but no can is released (soda
remains at 0).If a dime is received (
coin == 2
), a can is released (soda = 1
), the coins are banked, and the active state becomesgot_0
because no coins are left.If input coin is not a dime or a nickel, state
got_nickel
stays active and no can is released (outputsoda = 0
).
got_dime
— A dime was received.
If a nickel is received (
coin == 1
), a can is released (soda = 1
), the coins are banked, and the active state becomesgot_0
because no coins are left.If a dime is received (
coin == 2
), a can is released (soda = 1
), 15 cents are banked, and the active state becomesgot_nickel
because a nickel (change) is left.If input coin is not a dime or a nickel, state
got_dime
stays active and no can is released (outputsoda = 0
).
Design Rules in Mealy Vending Machine
This example of a Mealy vending machine illustrates these Mealy design rules:
The chart computes outputs in condition actions.
There are no state actions or transition actions.
The value of the input
coin
determines the value of the outputsoda
.