Author Topic: Programming Help  (Read 12224 times)

mace123s

  • Newbie
  • *
  • Posts: 8
  • I'm a llama!
    • View Profile
Programming Help
« on: December 10, 2015, 06:11:48 AM »
Can anyone suggest a more efficient way to write this statement.
It is used 5 times in the program with D=0 thur 4
The way it is currently written uses up way to much memory.
IF D=0 AND DM[2] = DM[41] AND DM[647]=1                'PROD1 BAY1 SELECTED        
DM[20]=A                                                                       'BAY1 HSC SET POINT  
DM[430]=1                                                                            
ELSEIF D=0 AND DM[2]= DM[41] AND DM[647]=2            'PROD1 BAY2 SELECTED          
DM[20]=F                                                                            'BAY2 HSC SET POINT
DM[430]=2
ELSEIF D=0 AND DM[2]= DM[41] AND DM[647]=3            'PROD1 BAY3 SELECTED
DM[20]=B                                                                            'BAY3 HSC SET POINT
DM[430]=3
ELSEIF D=0 AND DM[2]= DM[41] AND DM[647]=4            'PROD1 BAY4 SELECTED
DM[20]=G                                                                           'BAY4 HSC SET POINT
DM[430]=4
ELSEIF D=0 AND DM[2]= DM[41] AND DM[647]=5            'PROD1 BAY5 SELECTED
DM[20]=C                                                                           'BAY5 HSC SET POINT
DM[430]=5
ELSEIF D=0 AND DM[2]= DM[41] AND DM[647]=6            'PROD1 BAY6 SELECTED
DM[20]=E                                                                          'BAY6 HSC SET POINT
DM[430]=6
ELSEIF D=0 AND DM[2]= DM[41] AND DM[647]=7            'PROD1 BAY7 SELECTED
DM[20]=H                                                                          'BAY7 HSC SET POINT
DM[430]=7
ELSEIF D=0 AND DM[2]= DM[41] AND DM[647]=8            'PROD1 BAY8 SELECTED
DM[20]=I                                                                          'BAY8 HSC SET POINT
DM[430]=8
ELSE DM[430]=0
DM32[375]=0
ENDIF
IF D=0 AND DM[22] = DM[42] AND DM[487]=1           'PROD2 BAY1 SELECTED
DM[20]=A                                                                   'BAY1 HSC SET POINT
DM[431]=1                                            
ELSEIF D=0 AND DM[22]= DM[42] AND DM[487]=2            'PROD2 BAY2 SELECTED          
DM[20]=F                                                                               'BAY2 HSC SET POINT
 DM[431]=2                          
ELSEIF D=0 AND DM[22]= DM[42] AND DM[487]=3            'PROD2 BAY3 SELECTED
DM[20]=B                                                                              'BAY3 HSC SET POINT
DM[431]=3
ELSEIF D=0 AND DM[22]= DM[42] AND DM[487]=4            'PROD2 BAY4 SELECTED
DM[20]=G                                                                            'BAY4 HSC SET POINT
DM[431]=4
ELSEIF D=0 AND DM[22]= DM[42] AND DM[487]=5            'PROD2 BAY5 SELECTED
DM[20]=C                                                                            'BAY5 HSC SET POINT
DM[431]=5
ELSEIF D=0 AND DM[22]= DM[42] AND DM[487]=6            'PROD2 BAY6 SELECTED
DM[20]=E                                                                              'BAY6 HSC SET POINT
DM[431]=6
ELSEIF D=0 AND DM[22]= DM[42] AND DM[487]=7            'PROD2 BAY7 SELECTED
DM[20]=H                                                                              'BAY7 HSC SET POINT
DM[431]=7
ELSEIF D=0 AND DM[22]= DM[42] AND DM[487]=8            'PROD2 BAY8 SELECTED
DM[20]=I                                                                               'BAY8 HSC SET POINT
DM[431]=8
ELSE DM[431]=0
ENDIF
It continues on for a total of 13 Products

garysdickinson

  • Hero Member
  • Posts: 502
  • Old PLC Coder
    • View Profile
Re:Programming Help
« Reply #1 on: December 10, 2015, 01:18:03 PM »
I can probably help you, but I need a bit of clarification on what you mean by "effient".

If by efficient, you are worried about how much time it takes for the PLC to execute the code, there are ways to minimize the number of tests made.  This is sometimes called "common subexpression elimination".  I doubt that the TBASIC compiler is smart enough to do this for you.  I notice that the subexpression, "D=0 AND DM[2] = DM[41]” is repeated 8 times. You could made this test once. Some thing like this:

dm[430] = 0
if d=0 and dm[2]=dm[41] and dm[647]>=1 and dm[647]<=8
   dm[430]=dm[647]
   if dm[647] = 1
      dm[20] = a
   elif dm[647] = 2
      dm[20] = f
   elif dm[647] = 3
      dm[20] = b
   elif dm[647] = 4
      dm[20] = g
   elif dm[647] = 5
      dm[20] = c
   elif dm[647] = 6
      dm[20] = e
   elif dm[647] = 7
      dm[20] = h
   else
      dm[20] = i
   endif
endif
[/font][/color]

The code could be simplified more if there was a pattern to which variable is assigned to dm[647]. This whole section of code could be a simple if statement with two assignments. If the values were stored in an array such as dm[1001] through dm[1008], then the code could be further simplified:

dm[430]=0
if d=0 and dm[2]=dm[41] and dm[647]>=1 and dm[647]<=8
   dm[430]=dm[647]
   dm[20] = dm[1000+dm[647]]]
endif
[/font][/color]

If by efficient you want code that was less prone to errors and is easier to write and maintain. I have suggestions about how to organize your data usage and how to use the #define mechanism.

These suggestions are a bit too complicated to do in a reply to a question. If you email me and send me your PLC Program, I could take a whack at re-write.

A question that I need to ask is about your programming experience. If you are a real novice, then my approach will not help.  If you are comfortable with modern programming languages that support data structures and arrays of data structures, then you will understand the approach that I take.

Gary D*ickinson. My email address is in my profile.  I cannot put it in the body of the email as the Malaysian forum sensors do not approve of my family name.


« Last Edit: December 10, 2015, 07:11:02 PM by garysdickinson »

mace123s

  • Newbie
  • *
  • Posts: 8
  • I'm a llama!
    • View Profile
Re:Programming Help
« Reply #2 on: December 11, 2015, 11:07:13 AM »
Thank you Gary this information was very helpful.