How can I modify the state machine logic?

I'm working on this RTOS-based application for a vending machine controller with C and a state machine approach that manages various vending machine states as Idle,CoinInserted, SelectionMade & Dispensing, but I'm encountering an error message in the console: StateMachine Error: Invalid transition from CoinInserted to Dispensing. This occurs when a user inserts enough coins to trigger dispensing, but the selection hasn't been confirmed yet.
typedef enum {
STATE_IDLE,
STATE_COIN_INSERTED,
STATE_SELECTION_MADE,
STATE_DISPENSING
} VendingMachineState;

typedef struct {
VendingMachineState currentState;
} VendingMachine;

extern VendingMachineState GetCurrentState(VendingMachine *machine);
extern void SetNextState(VendingMachine *machine, VendingMachineState nextState);
typedef enum {
STATE_IDLE,
STATE_COIN_INSERTED,
STATE_SELECTION_MADE,
STATE_DISPENSING
} VendingMachineState;

typedef struct {
VendingMachineState currentState;
} VendingMachine;

extern VendingMachineState GetCurrentState(VendingMachine *machine);
extern void SetNextState(VendingMachine *machine, VendingMachineState nextState);
How can I modify the state machine logic to prevent this invalid transition and ensure a valid sequence of states Idle -> CoinInserted -> SelectionMade -> Dispensing ?
2 Replies
ZacckOsiemo
ZacckOsiemo6mo ago
I think you would need to show your logic for any opinions
Boss lady
Boss lady6mo ago
Hi @Marvee Amasi to prevent the invalid transition from CoinInserted to Dispensing when the selection hasn't been made yet, you can modify the state machine logic to introduce an intermediate state between CoinInserted and SelectionMade. your code for the VendingMachineState enum should be
typedef enum {
STATE_IDLE,
STATE_COIN_INSERTED,
STATE_SELECTION_IN_PROGRESS,
STATE_SELECTION_MADE,
STATE_DISPENSING
} VendingMachineState;
typedef enum {
STATE_IDLE,
STATE_COIN_INSERTED,
STATE_SELECTION_IN_PROGRESS,
STATE_SELECTION_MADE,
STATE_DISPENSING
} VendingMachineState;
Want results from more Discord servers?
Add your server