News:

  • October 01, 2025, 08:44:45 AM

Login with username, password and session length

Author Topic: percentage  (Read 3594 times)

PLCGuy

  • Internal Dev
  • Hero Member
  • ****
  • Posts: 677
percentage
« on: October 07, 2018, 03:25:36 PM »
I am trying to take a percentage of a value and insert it into memory locations.

Take 50% of V1 and put it into V10
Take 75% of V1 and put it into V11
take 100% of V1 and put it in V12,V13

Trying to avoid a bunch of math boxes.

franji1

  • Bit Weenie
  • Host Moderator
  • Hero Member
  • *****
  • Posts: 3761
    • Host Engineering
Re: percentage
« Reply #1 on: October 07, 2018, 04:28:45 PM »
I am trying to take a percentage of a value and insert it into memory locations.

Take 50% of V1 and put it into V10
Take 75% of V1 and put it into V11
take 100% of V1 and put it in V12,V13

Trying to avoid a bunch of math boxes.
Use a Subroutine called CalcPercent, and CALL it from $Main.  You have a nice single box, well named, and another code block with the 3 MATH boxes that you never have to open - ever.  As far as $Main is concerned, you have a single box CALL CalcPercent!

This is what modular programming is all about.

b_carlton

  • Internal Dev
  • Hero Member
  • ****
  • Posts: 606
    • thePLCguy
Re: percentage
« Reply #2 on: October 07, 2018, 06:22:43 PM »
Ass long as you are comfortable with integer results.
An output is a PLC's way of getting its inputs to change.

PLCGuy

  • Internal Dev
  • Hero Member
  • ****
  • Posts: 677
Re: percentage
« Reply #3 on: October 08, 2018, 11:38:48 AM »
Sounds like a good idea. Exactly how do I write it? What will it look like? Actually I have to do the math for 8 Zones. So the three lines I wrote in my first request is what will be in the subroutine?

Yes, I do not need decimal point accuracy.

franji1

  • Bit Weenie
  • Host Moderator
  • Hero Member
  • *****
  • Posts: 3761
    • Host Engineering
Re: percentage
« Reply #4 on: October 08, 2018, 01:16:45 PM »
Are the 4 output value percentages all the same:
50%
75%
100%
100%

or do you need to generically pass in 4 different percentages in each CALL (along with the base value)?

In C++, you would do something like
void CalcPercentages(int BaseValue, int percent1, int percent2, int percent3, int percent4, int &out1, int &out2, int &out3, int &out4);
so you would have
CALL CalcPercentages(/* in parms */ V1, 50, 75, 100, 100, /* out parms*/ V10, V11, V12, V13);

Or are the 4 percentages unnecessary (they are ALWAYS 50/75/100/100?)

PLCGuy

  • Internal Dev
  • Hero Member
  • ****
  • Posts: 677
Re: percentage
« Reply #5 on: October 08, 2018, 03:09:48 PM »
No they are always the same.

franji1

  • Bit Weenie
  • Host Moderator
  • Hero Member
  • *****
  • Posts: 3761
    • Host Engineering
Re: percentage
« Reply #6 on: October 08, 2018, 03:35:27 PM »
No they are always the same.

Good.

I reserved D100 as the "Input" parameter to the subroutine, and D101 thru D104 as the 4 "Output" parameters to the subroutine (tweak these as necessary, or create a UDT heap item called CalcPercentParms with .Input and .Out1, .Out2, .Out3, .Out4 signed DWORD members and use that instead).

Type CALL and enter CalcPercentages for the name, and enter the one input parmeter and the 4 output parameters going into D100 and out of D101 thru D104 (see first attachment).

The second attachment is what the code-block logic looks like for CalcPercentages subroutine.

Every time you enter CALL - it will prompt you if you want to re-use the CalcPercentages subroutine signature for the Input and Output parameters and set it up accordingly - re-use becomes much easier.

PLCGuy

  • Internal Dev
  • Hero Member
  • ****
  • Posts: 677
Re: percentage
« Reply #7 on: October 08, 2018, 06:10:15 PM »
Thank you so much. You scared me with the C++ stuff. lol. I am not a programmer in that sense.
i only need to load these values if the set point is changed.
So basically, someone enters a setpoint. I want zone 1 to be at 50%, zone 2 at 75% and Zones 3 & 4 at 100%.
I shy away from subroutines, cause I never can get them to work correctly. Time to dive in.

PLCGuy

  • Internal Dev
  • Hero Member
  • ****
  • Posts: 677
Re: percentage
« Reply #8 on: October 08, 2018, 06:32:54 PM »
Works great! So to understand it, the D101 is used as a place holder?

franji1

  • Bit Weenie
  • Host Moderator
  • Hero Member
  • *****
  • Posts: 3761
    • Host Engineering
Re: percentage
« Reply #9 on: October 09, 2018, 07:04:06 AM »
Yes.  D100 thru D104 are all reserved placeholders and not be used anywhere else except for all the CALLs to that CalcPercentages subroutine.