# Seeking Help on Project

My project is to make a piggy bank where each time you insert a coin there’ll be a beeping sound by the buzzer grove. The piggy bank is only accessible by using a password “123456”. Then there will be a door that unlocks and lets you access the bank. By using the weights of the coins it will show the value on the LCD Screen always, and I’d like to set a maximum value whereby when it reaches the max value the grove LED bar will show red, whereas when it’s 0 to 2/3 of the max value it’ll be at the green level.

My problem here is that I don’t know how to indicate the weight stuff into my Arduino project and I’d like to seek some help over here.

Included is my Finite State Machine to help understand what my project is, as well as my program itself.

Piggy Bank.txt (15.5 KB)

There's some funny business in your state diagram. States should be described by adjectives and arrows by events. You appear to have gone a bit fuzzy on that. For example LED,RED,GREEN is a curious collection of states. There should be three LED states if I understand your system... LEDisRED, LEDisGREEN, LEDisOFF. Then events or conditions to transition between them.

aarg:
There's some funny business in your state diagram. States should be described by adjectives and arrows by events. You appear to have gone a bit fuzzy on that. For example LED,RED,GREEN is a curious collection of states. There should be three LED states if I understand your system... LEDisRED, LEDisGREEN, LEDisOFF. Then events or conditions to transition between them.

Right, have taken note on that, yeah it should be LED_RED, LED_GREEN instead, having only 2 states since GROVE_LED is already on in the initial state. Thanks for the help on the diagram!

We do appreciate that you provide the level of detail that you do. It is rare around here.

What part do you need help with, exactly? You mention the weight... well, what about it? You weigh the coins go get the value, with it you know the total weight as well, and set the LEDs based on it. That's the idea, right?

The only real problem I see is that when someone makes a withdrawal from the piggy bank, how do you know what's left?

wvmarle:
What part do you need help with, exactly? You mention the weight... well, what about it? You weigh the coins go get the value, with it you know the total weight as well, and set the LEDs based on it. That's the idea, right?

The only real problem I see is that when someone makes a withdrawal from the piggy bank, how do you know what's left?

Haha, my real problem is i'm terrible at coding and telling whether my states are correct, so I'd need help on the coding for weight and whether i'm on the right track for my states

I saw this in your code:

``````#define mSTATE_0    0     //S0 - Initial State
#define mSTATE_1    1     //S1 - Lock
#define mSTATE_2    2     //S2 - Unlock
#define mSTATE_3    3     //S3 - Wait Door Open
#define mSTATE_4    4     //S4 - Alarm
#define mSTATE_5    5     //S5 - Coin Insertion
#define mSTATE_6    6     //S6 - LCD Screen
#define mSTATE_7    7     //S7 - LED Screen
``````

That's asking for trouble. Start by giving them proper names, or you WILL lose track of what is what.

Aso wondering what LCD screen and LED screen states are for, isn't the screen drawn at all times, regardless of the state the machine is in?
Coin insertion is an event, not a state. It can happen at any time, even when the door is open. Alarm is probably also an event rather than a state, but could be both.
States include door locked, door unlocked, door open, etc.

#define ML_LOCK 1 // De-energize Magnetic Lock (Lock Position)
#define ML_UNLOCK 0 // Energize Magnetic Lock (Unlock Position)

#define DOOR_OPEN 0 // Door Sensor is in Open Position
#define DOOR_CLOSE 1 // Door Sensor is in Close Posotion

#define WAIT_PASSWORD 0 // Wait for User to Input Password

Door locked/unlocked I'd do as bool, set it to true or false. Same for open/close.

wait_password could be a state - but I would expect you're ready to enter the password at all times. When the user starts entering the password you record those events, and if it's entered correctly, you unlock the door (doorLocked = false).

So you're partly on the right track but have to make a difference between events (button presses) and machine states (the results of those presses). It may be very helpful to start writing down a bit of a scheme of what has to be done in which state.

For a safe (that's the part you implemented) it would be something like this:

Start up state: idle.
Display: off.
Keypad: listening for user entry.
Upon keypress: change to keypad active state.

Showing value state:
Show value on display.
Time out: after 2 minutes of no keypress and no coin inserted go back to idle state.

switch on display, show instructions on what to do.
User presses #: expect digits for password until another # is received.
Correct password entered: change state to "unlocked".
User presses *: change state to "showing value"
Time out: after 2 minutes of no keypress and no coin inserted go back to idle state.

Showing value state:
Display current value on the display.

When a coin is inserted: go to coin inserting state.
Display: show current value and latest deposit.

Unlocked state: unlock the door.
If after two minutes the door is still not opened: lock it, back to idle.
If door opened, then closed again: lock it right away, back to idle.
(or: ask user to enter a new password, as hotel safes would do).