Author Topic: Reseting Multiple Relays  (Read 7410 times)

Lorne Van Dusen

  • Jr. Member
  • Posts: 93
  • I'm a old guy
    • View Profile
Reseting Multiple Relays
« on: March 03, 2017, 09:00:29 AM »
Is there a simple way to reset a large group of relays in TBasic?
The manual describes how to reset single bits 0 to 15
An example would be I need to reset relays 273 through to 310
From what I have learned so far I think it should be something like this.

DO I USE FOR N = 273 TO 310 // 273 to 310 are the individual relays but the channel number would be 35 to 39
OR DO I USE N = 35 TO 39 // // so this is where I am confused
RELAY[35] = 0

Would this be the correct way?

Lorne Van Dusen

  • Jr. Member
  • Posts: 93
  • I'm a old guy
    • View Profile
Re:Reseting Multiple Relays
« Reply #1 on: March 03, 2017, 11:10:07 AM »
I guess I jumped the gun on this one as I figured it out on my own.
The main problem was I was dividing the numbers by 8 because of working with the FP4030MR when I should have been dividing by 16 to come up with the channel number so the actual formula worked out as follows.

FOR I = 18 TO 20
  RELAY[18] =0
NEXT
  ENDIF

garysdickinson

  • Hero Member
  • Posts: 502
  • Old PLC Coder
    • View Profile
Re:Reseting Multiple Relays
« Reply #2 on: March 04, 2017, 09:53:55 PM »
Lorne,

Your code will not clear relays 273..310.  As written it will only clear relays 273..289 because it only clears the 16 relays at RELAY[18] multiple times.

As written your code will not compile due the unmatched "ENDIF" statement.  

Perhaps you intended to write something like this:


' clear relays #273 to 321
for i = 18 to 20
    Relay = 0
next
[/size]

However, my code clears relays through 321 and not 310 as you seemed to want to do.

I would suggest that you name your RELAYS in the I/O table and use the names.  In this case I have named relays 1..3 as "Run", "Stop" and "Restart".
#Label Name
1Run
2Stop
2Restart

If you want to clear the relays in a custom function, just use ClrIO:

ClrIO Run
ClrIO Stop
ClrIO Restart
[/size]


I prefer that approach for the following reasons:
1. The code is easier to understand as the RELAYs have descriptive names.
2. If you insert or delete a RELAY in the I/O table and this results in the relay having a different number, your code will work as written. This is not the case with RELAY[n] as it always accesses the same relay numbers.
3. The use of the RELAY[n] notation affects 16 RELAYS.  The use of RELAY[n] requires you and the next guy that tries to fix your code to "know" what relays are affected by the use of RELAY[n].
4. The use of RELAY[n] notation requires you to be very careful and deliberate on how you name your RELAYs. If you delete something from the I/O Table and this results in your RELAYS moving up/down in the table, then your relays may not align on on the required 16 bit boundary.

I do use the RELAY[n] in my code BUT, it is very limited and I document it usage very carefully so that I (or the next guy) will be able to understand my intent.  

Gary D


« Last Edit: March 05, 2017, 08:13:48 AM by garysdickinson »

Lorne Van Dusen

  • Jr. Member
  • Posts: 93
  • I'm a old guy
    • View Profile
Re:Reseting Multiple Relays
« Reply #3 on: March 06, 2017, 09:33:38 AM »
Again thanks for the expert help
I must admit in all the years I have been programming PLC'S I really never used a FOR loop as 90% of my programming was done in Ladder Logic and the rest was done using the built in functions supplied by the PLC manufacturer so I was a little confused on a couple of things.
If I wand to clear an area of DM'S then I would write
FOR N=7 to 22 meaning DM[7] to DM[22]
 DM[N] =0 // the DM tells the program that it is a DM area and the [N] is what is used for the variable and the =0                tells the program to set all the locations to a value of 0

Is the correct so far?

Then if I want to clear a group of relays say from relay 273 through 321 then I would write
FOR I = 18 to 20 //  this should be relay 273 to 289, and 290 to 305, and also 306 to 321
 RELAY = 0  // the RELAY tells the program that it is the Relay area and the is a variable and the =0 means the value to set all the relays to

So if I am correct then the letter after the FOR can be any variable letter from A to Z  then the start and end address the the area DM, RELAY etc. then the Value to put into the area. Is this correct.

You may help me to become half decent TBASIC programmer one day.

 ;D

garysdickinson

  • Hero Member
  • Posts: 502
  • Old PLC Coder
    • View Profile
Re:Reseting Multiple Relays
« Reply #4 on: March 06, 2017, 01:20:19 PM »
Lorne your understanding of the for..next loop is essentially correct.

TRI's for..next documentation is not correct. Their text is in green. I've struck out the things that are flat out wrong and have added text in red for some of the stuff that they omited:


    FOR variable = x TO y [STEP z]
         . . . .
    NEXT

where variable may be any integer variable only
including A to Z, DM[] and DM32 and is used as a counter. x, y and z are numeric expressions either a constant value such as 1 or a variable such as J. Other numeric expressions are NOT allowed. STEP z is an optional part of the statement. x is the initial value of the counter, y is the final value of the counter. Program lines following the FOR statement are executed until the NEXT statement is encountered. Then the counter is incremented by the amount specified by STEP. If STEP is not specified, the increment is assumed to be 1. A check is performed to see if the value of the counter is greater than the final value y if STEP is positive (or smaller than the y if STEP is negative). If it is not greater, the program branches back to the statement after the FOR statement, and the process is repeated. If it is greater, execution continues with the statement following the NEXT statement. This is called a FOR-NEXT loop. A run-time error will result if STEP is evaluated to be 0.

For PLCs that support floating point, the x, y and z values can be floating point constants or simple floating point variables such as A#..Z# and FP[1..1000].

If the x,y or z values are located in DM[], DM32[] or FP[] the compiler will allow limited arithmetic expressions that involve constants and variables.  The following sort of expressions will run under simulation:
DM[0+1] and DM[n+1].

Please note that the values of the control, start, end and start arguments are evaluated and stored when the "for" part of the for..next statement is executed. Additionally the control variable's address is stored.  The end result of this is that if you specify a for..next loop using variables, changing these variables within the body of the for..next loop will have no effect on the operation of the loop.


The following PLC code will work:


' integer variable, constant start and end values
'
print #3 "for i = 1 to 10: ";
for i = 1 to 10
   print #3 i;" ";
next
print #3 ""

' integer variable DM[1], constant start and end values
'
print #3 "for dm[1] = 1 to 10: ";
for dm[1] = 1 to 10
   print #3 dm[1];" ";
next
print #3 ""

' integer variable, constant start, variable end
'
k = 15
print #3 "for i = 10 to k: ";
for i = 10 to k
   print #3 i;" ";
next
print #3 ""

' integer variable, variables start and end values
'
j = 10
k = 15
print #3 "for i = j to k: ";
for i = j to k
   print #3 i;" ";
next
print #3 ""

' integer start, end and step values
'
print #3 "for i = 10 to 1 step -1: ";
for i = 10 to 1 step -1
   print #3 i;" ";
next
print #3 ""

' integer start and end, variable step value
'
j = -2
print #3 "for i = 10 to 0 step j: ";
for i = 10 to 0 step j
   print #3 i;" ";
next
print #3 ""

' float variable, constant start and end values
'
print #3 "for i# = 1.0 to 10.0: ";
for i# = 1.0 to 10.0
   print #3 i#;" ";
next
print #3 ""

' float variable, variable start and end values
'
j# = 10.0
k# = 15.0
print #3 "for i# = j# to k#: ";
for i# = j# to k#
   print #3 i#;" ";
next
print #3 ""
[/font]

When run under the simulator the code will write this to a window:

for i = 1 to 10: 1 2 3 4 5 6 7 8 9 10
for dm[1] = 1 to 10: 1 2 3 4 5 6 7 8 9 10
for i = 10 to k: 10 11 12 13 14 15
for i = j to k: 10 11 12 13 14 15
for i = 10 to 1 step -1: 10 9 8 7 6 5 4 3 2 1
for i = 10 to 0 step j: 10 8 6 4 2 0
for i# = 1.0 to 10.0: 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
for i# = j# to k#: 10.0 11.0 12.0 13.0 14.0 15.0

Lorne there are a lot of things that you can do with the for..next structure.  

Once you get this mastered, that TBASIC also supports another powerful looping structure: while..endwhile.  The other very useful control structure is if..elif..elif..else..endif.

No end of stuff to learn.

Gary D
« Last Edit: March 07, 2017, 11:31:58 AM by garysdickinson »

Lorne Van Dusen

  • Jr. Member
  • Posts: 93
  • I'm a old guy
    • View Profile
Re:Reseting Multiple Relays
« Reply #5 on: March 08, 2017, 06:52:11 AM »
Gary this is the most comprehensive and detailed answer I have ever received from any Tech Support line.
Your description of the For Next Loops is well written and documented.
I have printed it out and will spend some time going over all your examples.

Great response and thank you very much