Vending Machine Controller in Logisim-evolution: Coin Accumulator FSM Guide
Direct Answer: Building a Vending Machine Controller in Logisim-evolution
A Vending Machine Controller in Logisim-evolution is a synchronous sequential system that accumulates inserted coin values across clock cycles until credit equals or exceeds the item price (such as 15¢, 25¢, or 50¢), then asserts a 1-cycle Vend pulse, computes exact change, and resets the credit register to zero. For simple 15¢ Nickels/Dimes problems, a 4-state Moore FSM (0¢, 5¢, 10¢, 15¢+Vend) encoded in 2 flip-flops works directly; for realistic machines accepting Quarters (25¢) and multiple item prices, an 8-bit Balance Register + Adder + Comparator datapath eliminates state-table explosion while supporting edge-detected coin pushbuttons.
Recommended FSM Textbooks and Physical FPGA Hardware
The vending machine problem is a classic university exam and lab benchmark for comparing Mealy vs. Moore state minimization and arithmetic datapaths:
Digital Design & Computer Architecture
By David Harris & Sarah Harris. Covers Mealy/Moore state encoding, one-hot vs. binary tradeoffs, and synchronous datapath control.
Digilent Basys 3 Artix-7 FPGA
Map Nickel, Dime, Quarter, and Coin-Return to physical pushbuttons while displaying live inserted credit in cents on the 7-segment displays.
Comparing Pure FSM vs Accumulator Datapath Architectures
| Design Approach | Supported Coins & Prices | Hardware Complexity | Change Calculation |
|---|---|---|---|
| Pure Moore/Mealy FSM | Nickels (5¢) & Dimes (10¢) up to 15¢–25¢ | 2–3 Flip-Flops + Combinational Analysis | Hardcoded per transition (5¢ or 10¢ pulse) |
| Accumulator + Comparator | Any coin (5¢, 10¢, 25¢, $1) & multi-item prices | 8-bit Register + Adder + Subtractor + Comparator | Exact arithmetic: Change = Balance - Price |
Complete State Transition Table for a 15-Cent Vending Machine
Below is the complete transition table for a 15¢ vending machine accepting Nickels (N = 01) and Dimes (D = 10), including exact 5¢ change return when 10¢ is inserted at the 10¢ state:
| Current State (Q1 Q0) | Idle (00) | Nickel +5¢ (01) | Dime +10¢ (10) | Outputs (Vend, Chg_5c) |
|---|---|---|---|---|
| S0 (00) — 0¢ | S0 (00) | S1 (01) | S2 (10) | 0, 0 |
| S1 (01) — 5¢ | S1 (01) | S2 (10) | S3 (11) | 0, 0 |
| S2 (10) — 10¢ | S2 (10) | S3 (11) | S3 (11) [Chg=5¢] | 0, 0 |
| S3 (11) — Vend ≥15¢ | S0 (00) | S0 (00) | S0 (00) | Vend = 1 |
Four Stage Accumulator Datapath Assembly in Logisim
Phase 1: Rising-Edge Coin Pulse Detectors
For each coin pushbutton (Nickel, Dime, Quarter), build an edge detector using one D Flip-Flop and an AND gate computing Coin_Pulse = Btn & ~Q_prev. This converts a long human button press into a clean 1-clock-cycle pulse.
Phase 2: Coin Value Selector and 8-Bit Credit Adder
Feed the coin pulses into a 4-to-2 Priority Encoder driving a 4-to-1 8-bit Multiplexer loaded with constants 0, 5, 10, and 25. Add the selected coin value to the current 8-bit Balance Register output using an 8-bit Adder.
Phase 3: Price Comparison and Change Subtractor
Compare Balance against the selected Item_Price (e.g., 30) with an 8-bit Unsigned Comparator. When Balance ≥ Item_Price (and user presses Buy, or auto-vend triggers), assert Vend = 1 and compute Change = Balance - Item_Price.
Phase 4: Synchronous Balance Reset and Decimal Readout
Gate a 2-to-1 MUX in front of the Balance Register’s D input controlled by Vend | Coin_Return so the register resets synchronously to 0x00 on the cycle after dispensing. Convert the balance to BCD to drive two 7-segment displays.
Frequent Traps in Vending Machine Controllers
Trap 1: Multi-Cycle Coin Overcounting
Problem: Holding down a Nickel button for 4 clock ticks adds 20¢ instead of 5¢.
The Fix: Always pass coin buttons through rising-edge pulse detectors before the adder or FSM.
Trap 2: Losing Change Output on Immediate Clear
Problem: Clearing the Balance Register asynchronously the instant Vend goes high zeroes out Change = Balance - Price before anyone can see it.
The Fix: Latch Change into a separate Change Display Register on the Vend clock edge while clearing Balance.
Trap 3: Signed Comparator Bug on Prices Above 127¢
Problem: Leaving an 8-bit Comparator in default 2’s Complement mode treats 130¢ as negative (−126).
The Fix: Switch the Comparator’s Numeric Type attribute to Unsigned.
Trap 4: Missing Coin Return Cancel Path
Problem: Customers who insert 10¢ and change their mind get stuck forever.
The Fix: Add a Cancel input that copies current Balance to the Change Register and clears Balance to 0.
Curriculum Progression from Vending FSMs to Processor Control
Frequently Asked Questions About Vending Machine Controllers
Should I use a Mealy or Moore machine for a vending controller?
A Moore machine guarantees glitch-free vend solenoid outputs because outputs depend only on the registered state, while a Mealy machine saves one flip-flop state by emitting change directly on the final coin transition.
How do I display the coin balance in decimal cents?
Route the 8-bit binary balance into a Binary-to-BCD Double Dabble subcircuit (or a 256×8-bit BCD ROM) driving two Hex Digit Displays.
What happens if two coin buttons are pressed on the exact same clock edge?
A 4-to-2 Priority Encoder in front of the coin-value MUX ensures deterministic behavior by accepting the highest-denomination coin on that cycle rather than corrupting the adder.
Can I support multiple items at different prices (e.g., 25¢ and 40¢)?
Yes. Add an Item_Select switch feeding a 2-to-1 MUX that supplies either 25 or 40 to the Comparator and Change Subtractor.
