This program has only one function which to mimic very simple calculator with addition, substraction, multiplication and division operation (with bonus square root and percent). But instead of using keyboard, it will recieve input from front panel button to insert number and instruction. Then shows input number and calculation result in Indicator. Maybe some of you think that this program requirement is so simple just like 5 minutes worth of coding but for me this project took my entire week.
Specification
Development Environment :
ASUS VivoBook A442U with Intel Core i5-8250U, 8GB of memory
Windows 10 Version 1903
LabVIEW NXG 3.1 (stock install, no additional package)
Microsoft Calculator 10.1910.0.0 as Expected tool
Test Environment :
Using the same computer for development, the only computer I have currently.
Front Panel Design
This oversimple front panel is only consist of 4 parts as shown in picture above.
1. Number Display : showing inputted number and calculation result. it capable to show 13 number (including comma) with 6 digit precision.
2. Number Button : input number to be calculate. it has "latch when released" mechanical action.
3. Instruction Button : input button for calculation instruction. it has same mechanical action with Number Button. For 2 digit operation (add, minus, multiply and divide) handle second number input and Equal Button for outputing result. While single digit operation (square root and percent) can drive output in one click.
4. Stop Button : Stop LabVIEW from running.
Block Diagram Design
This program mainly consist of 1 Main Loop. Main Loop make the system continuously running until user press the Stop Button. while calculation process handled by Event structure. In simple way, when input button pressed, it activate Event structure related to button to do something. Inside the block diagram, mainly only use 4 data wire.
1. Positive/Negative Flag : Data type Boolean. This wire indicating that inputted data is positive or negative.
2. Input Number Buffer : Data type String. This wire contain inputted data from button.
3. Second Number Buffer : Data type String. This wire buffer the inputted data when 2 number operation.
4. Instruction State Enum : Data type Enum. This enum will remember what kind of operation should do when instruction button pressed.
As simple as it see, every single button in front panel has their own event process handle by Event Structure.
Number Button Behaviour (0~9)
Number button behaviour just to insert number and display them into indicator. Maybe the is a question, why I use string data instead of integer or double? because string is simpler to replicate calculator display. When you press a number, it will appear in right side of current number. For replicating that behaviour, I am using concenate string function. There is also Zero handling function. That function is to replace 0 value when first number inputted.
2 Number Calculation Button (+, -. /, x)
Equal Button
Equal button behaviour is where the magic happen in this circuit. because we can not do arithmetic operation in string, both buffer and inputted number string must be convert to Double precision data (since I need decimal operation also. I can use fix point data but I'm not fans of fix point at this rate). After conversion, both data calculate based on Calculation state Enum value. Calculation result is convert back to string, through some decimal handling, then send out to display. Based on Enum value, inside Case Structure we can place add, substract, multiply and divide function (or whatever we want).
Single Number Calculation Button (%, √)
Same as Equal button behaviour, single number operation handle string-number conversion and calculation. But instead using Enum value and Case Structure, it using direct function such Square root or Percentage.
Decimal Point Button
As its name, decimal point button just add point (or comma) to the number. But it's not as simple as that. When button is pressed, we must check that current number already has fractional number or not (it will be silly if we can insert 2 or more decimal point). Then add point if the number doesn't has any fractional number or retain the number when it already had fractional part.
Positive/Negative Button
Positive/Negative button has simple function to indicate that inputted number is negative or positive number. but it was the most trickiest behaviour in the code (excluding the zero-remover sub vi). When button pressed, it must check if current number is positive or negative. When positive, it must add negative sign or remove it when number is negative.
Zero Remover Sub-VI
Zero remover actually has function to handle fractional number. it was the trickiest function. as we already know that I need to do some Number to String conversion after calculation. For example, if the Double Precision Number value is 7.15, the string value also must be 7.15. But guess what? it is not as easy as pie because Double to String function has fix decimal precision point. Then when we set precision value by 6, Double value is 7.15, then the string value is 7.150000. We need to get rid that unused zero from calculator display. So my anger created the Zero Remover Sub VI.
Basically, this sub VI take the same precision value with Double to String function, then check the entire fractional part of the number. When there are 0 and there is no significant number after that, it will remove the zero. Not the fanciest way, maybe there is a better function inside NXG for doing this, but I am just newbie here, ande it works actually. since it is a subVI, I don't think so much about the front panel because it won't show anywhere.
This calculator has unlimited ways to explore just for LabVIEW NXG learning in the future. We can add more calculation function and other. Please reach me if there are some feedback, critics or if you want the code. Thank Your very much.