A vending machine controller
The problem
Design a finite state machine for a machine that sells one item for 15 cents and accepts nickels (5) and dimes (10), with no change given, and build a program that simulates it.
Requirements
- Draw the state diagram, with a state for the money inserted so far (0, 5 and 10 cents)
- Write the state table with the next state and the output for every state and input
- Output a 'dispense' signal when 15 cents or more have been inserted, and go back to the start state
- Run the table in a program on a list of coins, and print the state and output after each coin
Technologies
- Python
- State tables
- Mealy machines
Step by step
- List the states and the inputs (nickel, dime), and decide whether the machine is Moore or Mealy.
- Fill in the state table completely, for every state and every input.
- Code the table as a dictionary and write the loop that follows it.
- Test with sequences such as nickel, nickel, nickel and dime, nickel, and check the outputs by hand.
- Write the table as truth tables for the next-state bits and simplify them with Karnaugh maps.
Expected result
A simulator that dispenses exactly when 15 cents or more are inserted, with a state table and a K-map for each next-state bit.
How it is evaluated
- Every state has a next state for every input
- The outputs agree with hand-traced sequences
- The K-map groups are correct and checked against the truth tables
Ways to improve it
- Return change for an overpayment of 5 cents
- Accept quarters (25 cents) too
- Count the number of items sold with a counter