A vending machine FSM (Finite State Machine) helps control the steps of accepting coins and dispensing items in a clear, organized way.
Vending machine FSM example in VHDL
library IEEE; use IEEE.STD_LOGIC_1164.ALL; entity vending_machine is Port ( clk : in STD_LOGIC; reset : in STD_LOGIC; coin_5 : in STD_LOGIC; -- 5 cents coin input coin_10 : in STD_LOGIC; -- 10 cents coin input dispense : out STD_LOGIC -- output to dispense item ); end vending_machine; architecture Behavioral of vending_machine is type state_type is (S0, S5, S10, S15, DISPENSE); signal state, next_state : state_type; begin process(clk, reset) begin if reset = '1' then state <= S0; elsif rising_edge(clk) then state <= next_state; end if; end process; process(state, coin_5, coin_10) begin dispense <= '0'; case state is when S0 => if coin_5 = '1' then next_state <= S5; elsif coin_10 = '1' then next_state <= S10; else next_state <= S0; end if; when S5 => if coin_5 = '1' then next_state <= S10; elsif coin_10 = '1' then next_state <= S15; else next_state <= S5; end if; when S10 => if coin_5 = '1' then next_state <= S15; elsif coin_10 = '1' then next_state <= DISPENSE; else next_state <= S10; end if; when S15 => dispense <= '1'; next_state <= S0; when DISPENSE => dispense <= '1'; next_state <= S0; when others => next_state <= S0; end case; end process; end Behavioral;
This example uses a simple FSM with states representing the amount of money inserted.
Inputs are coins of 5 and 10 cents, and output signals when to dispense the item.
type state_type is (S0, S5, S10, S15, DISPENSE); signal state, next_state : state_type;
process(clk, reset) begin if reset = '1' then state <= S0; elsif rising_edge(clk) then state <= next_state; end if; end process;
process(state, coin_5, coin_10) begin case state is when S0 => if coin_5 = '1' then next_state <= S5; elsif coin_10 = '1' then next_state <= S10; else next_state <= S0; end if; -- other states handled similarly end case; end process;
This program models a vending machine that accepts 5 and 10 cent coins. It moves through states representing the total inserted money. When 15 cents or more is reached, it sets the dispense output to '1' to release the item, then resets.
library IEEE; use IEEE.STD_LOGIC_1164.ALL; entity vending_machine is Port ( clk : in STD_LOGIC; reset : in STD_LOGIC; coin_5 : in STD_LOGIC; coin_10 : in STD_LOGIC; dispense : out STD_LOGIC ); end vending_machine; architecture Behavioral of vending_machine is type state_type is (S0, S5, S10, S15, DISPENSE); signal state, next_state : state_type; begin process(clk, reset) begin if reset = '1' then state <= S0; elsif rising_edge(clk) then state <= next_state; end if; end process; process(state, coin_5, coin_10) begin dispense <= '0'; case state is when S0 => if coin_5 = '1' then next_state <= S5; elsif coin_10 = '1' then next_state <= S10; else next_state <= S0; end if; when S5 => if coin_5 = '1' then next_state <= S10; elsif coin_10 = '1' then next_state <= S15; else next_state <= S5; end if; when S10 => if coin_5 = '1' then next_state <= S15; elsif coin_10 = '1' then next_state <= DISPENSE; else next_state <= S10; end if; when S15 => dispense <= '1'; next_state <= S0; when DISPENSE => dispense <= '1'; next_state <= S0; when others => next_state <= S0; end case; end process; end Behavioral;
Make sure to reset the machine to start from the initial state.
Only one coin input should be '1' at a time to avoid confusion.
The dispense signal is '1' only briefly when enough money is inserted.
A vending machine FSM uses states to track inserted money.
Inputs trigger state changes, and output signals when to dispense.
VHDL processes handle state updates and next state logic.
Practice
Solution
Step 1: Understand FSM role in vending machine
The FSM (Finite State Machine) controls the order of operations like accepting coins and dispensing items.Step 2: Compare options to FSM purpose
Only To control the sequence of accepting coins and dispensing items describes controlling the sequence of actions, which matches FSM's role.Final Answer:
To control the sequence of accepting coins and dispensing items -> Option BQuick Check:
FSM controls sequence = To control the sequence of accepting coins and dispensing items [OK]
- Confusing FSM with memory storage
- Thinking FSM does math or graphics
- Ignoring the sequence control aspect
Solution
Step 1: Identify correct state type declaration
States in FSM are usually declared as an enumerated type listing all states.Step 2: Check options for enumerated type
type state_type is (IDLE, COIN_INSERTED, DISPENSE); declares a type with named states, which is correct for FSM states.Final Answer:
type state_type is (IDLE, COIN_INSERTED, DISPENSE); -> Option CQuick Check:
State type enum = type state_type is (IDLE, COIN_INSERTED, DISPENSE); [OK]
- Using integer or boolean instead of enumerated type
- Confusing signal and type declarations
- Using variable instead of type for states
case state is
when IDLE =>
if coin = '1' then
next_state <= COIN_INSERTED;
else
next_state <= IDLE;
end if;
when COIN_INSERTED =>
next_state <= DISPENSE;
when DISPENSE =>
next_state <= IDLE;
end case;What will be the next state if current state is IDLE and coin = '0'?
Solution
Step 1: Analyze the IDLE state condition
If coin = '1', next_state is COIN_INSERTED; else next_state remains IDLE.Step 2: Apply coin = '0' condition
Since coin = '0', the else branch applies, so next_state stays IDLE.Final Answer:
IDLE -> Option DQuick Check:
coin '0' in IDLE = next_state IDLE [OK]
- Assuming coin '0' moves to COIN_INSERTED
- Confusing next_state with current state
- Ignoring else branch in if statement
process(clk)
begin
if rising_edge(clk) then
case state is
when IDLE =>
if coin = '1' then
state <= COIN_INSERTED;
end if;
when COIN_INSERTED =>
state <= DISPENSE;
when DISPENSE =>
state <= IDLE;
end case;
end if;
end process;Solution
Step 1: Check for reset condition in FSM process
FSM processes usually include a reset to initialize the state at start.Step 2: Identify missing reset
This code lacks any reset signal or condition to set initial state, which is an error.Final Answer:
No reset condition for state initialization -> Option AQuick Check:
Missing reset = No reset condition for state initialization [OK]
- Thinking else branch missing causes latch in synchronous process
- Confusing signal and variable usage
- Ignoring importance of reset in FSM
Solution
Step 1: Understand requirement to track coin values
We must track total coin value inserted, not just count coins.Step 2: Choose approach to accumulate coin values
Add a coin_value input and a coin_total signal; update coin_total on each coin and transition to DISPENSE when coin_total >= 15 uses a coin_value input and coin_total signal to sum coins and trigger dispense when total >= 15, which fits requirement.Final Answer:
Add a coin_value input and a coin_total signal; update coin_total on each coin and transition to DISPENSE when coin_total >= 15 -> Option AQuick Check:
Track coin values and total = Add coin_value and coin_total, dispense at 15+ [OK]
- Using separate FSMs instead of one with coin total
- Counting coins instead of summing values
- Dispensing by time, not coin total
Ready to prove it?
Pass the test and this pattern moves unknown
