04-09-2022 04:46 PM
Hello, I'm working on a simple VI for a vending machine.
The problem I'm facing is that my loop that checks if a button is pressed keeps on running, even if it's only been clicked once,, it then does both cases when one clicks the button: True and False. (True: You buy, False: Either not enough money or not enough on "inventory")
I've tried setting them to all of the states possible (Mechanical action: Switch when released, switch when pressed, latch ...... etc).
How can I prevent my code to only run once when a product is selected?
I am having a hard time understanding what the easiest way to fix it is: Is it a matter of the mechanical action of the switch, or is it something to do with my logic to check if a button is pressed? --> (a simple for-loop that checks the old value and new value).
Any comments/suggestions/observations are welcome.
Thank you so much! 😋
Solved! Go to Solution.
04-09-2022 05:41 PM - edited 04-09-2022 05:44 PM
From your description, I'm going with "logic is wrong". This sounds like a CLD - type exercise. Check out the certification forum. I think you'll find it very useful.
04-10-2022 11:14 AM
I cannot see your VI. can you "save for previous" (LabVIEW 2020).
Most likely this will be trivial to solve.
04-10-2022 11:46 AM
Hello, I am not studying to do the Certified Developer Tests, I've started using LabVIEW a month ago. This is for me to understand how to work with the tools that exist and get a better understanding of state machines.
As I stated before, my problem is that when I input money and then click on a product (array of buttons), it does the transaction 2 times because the array of the buttons changes 2 times:
(On event structure)
OldVal --> [F, F, F, F, F, F]
NewVal (when pressed) --> [F, F, T, F, F, F]
NewVal(released) --> [F, F, F, F, F, F]
I've tried studying it using probe, and I understand the problem however, I don't know how to fix it.
So far I've tried using AND for array and OR for array.. I've tried also using a case structure , but failed at implementing it.
Thank you so much in advance for your time! 🐞
04-10-2022 12:48 PM - edited 04-10-2022 01:05 PM
You get two events because the buttons are "switch until released", which will create two events. Make them Switch when pressed and reset them inside the event, e.g. as follows (writing to a local does not fire an event):
(These array elements should not be typdefs. Makes no sense)
There are significant flaws, for example your search for a difference could be more easily done using search array.
You have way too many loops with way too many duplicate tunnels, e.g. compare:
Why is "amount" orange? Isn't that an integer?
Can you explain step-by-step how this is supposed to be used and what should happen at all stages of operations? I am sure all this could be done with less than half the code.
04-10-2022 01:44 PM - edited 04-10-2022 01:54 PM
I realized the changes you suggested, however it's still done two times.. After I put money and then click a button, it does the transaction the first time, but then again the values change on the array (OldVal & NewVal) and it does it again.
My idea is the following (whole code explained):
(I've done previously a vending machine with a state machine, but its main concept was around the buttons, so i would end up with a 10+ event cases (One for cola, one for fanta, etc..) So I decided to try another approach.
I built this time the array of buttons and I'm trying to write the code around this buttons-change because I want to make it in a way that it won't matter if it's 5,6,10 products, the code can remain the same. However, maybe my reasoning and architecture is wrong and any suggestions will be welcome.
On the front panel a cluster can be seen, it contains (by order):
- Its respective button
- Name
- LED that indicates if there is on inventory or not (red == empty)
- Price (last numeric control on the right)
I have then on the left:
- Numeric Control to insert money and a button (OK) to insert it into the machine
- Numeric Indicator with the money that's left from the transaction and a button to eject it
On top:
- A string
- A numeric Indicator that indicates the money you've put into the machine
I would like to keep this structure as it is because that's the way the machine makes sense in my mind (although I know the indicators can be put away and it can be displayed on the string box).
On the code:
(On case structure)
- loads cluster with product information (name, amount, price, availability)
- another cluster with:
1. money that's left from the transaction (rückgeld)
2. the money that's inserted (guthaben) already
3. money inside the machine (geld in der maschine)
1. Timeout
2. Insert Money: Takes the inserted money (geld einwurf) and puts it into the money used to make the transaction (guthaben). I set then Geld Einwurf to 0 because otherwise it retains the value on the numeric control.
3. Return: Puts remaining money from Guthaben (money already inside the machine) into Rückgeld (money to return) and sets Guthaben to 0.
4. Product Buttons:
I take the button that was pushed and make a case structure around the following condition:
If guthaben is bigger than the price and it's available, then (transaction can happen// another case structure):
Guthaben is substracted the price and placed into rückgeld.
I take the name to display selection on the screen,
I take the amount of the product that there is and substract 1.
..
On the outside of the case structure the loop is checking if the leds are available and displaying it.
Thank you for your reply and your time!🌞
04-10-2022 02:01 PM
Your buttons still have the wrong mechanical action (You need to take them out of the array container to change for some reason)
You still have duplicate input tunnels into the FOR loops you changed.
04-10-2022 03:44 PM
@Blancoys wrote:
Hello, I am not studying to do the Certified Developer Tests, I've started using LabVIEW a month ago. This is for me to understand how to work with the tools that exist and get a better understanding of state machines.
As I stated before, my problem is that when I input money and then click on a product (array of buttons), it does the transaction 2 times because the array of the buttons changes 2 times:
(On event structure)
OldVal --> [F, F, F, F, F, F]
NewVal (when pressed) --> [F, F, T, F, F, F]
NewVal(released) --> [F, F, F, F, F, F]
I've tried studying it using probe, and I understand the problem however, I don't know how to fix it.
So far I've tried using AND for array and OR for array.. I've tried also using a case structure , but failed at implementing it.
Thank you so much in advance for your time! 🐞
The reason I mentioned the certification board is because I believe there are good examples of what you are trying to do there! You don't have to be going for a certification to get use out of that forum.
04-10-2022 03:58 PM - edited 04-10-2022 03:59 PM
I agree that one month is not nearly long enough for the CLD, probably not even for the CLAD, so let's consider all this more like a "Fingerübungen". Overall, your code actually looks quite good for that level of experience.
For fun, I did a quick rewrite to give you some ideas. It probably won't pass the CLD but gives you something to play with and lets you consider alternatives to some of your approaches.
04-10-2022 04:24 PM
I did check and the examples are very useful! Thank you 😋