Author Topic: T-basic new user issues...  (Read 14905 times)

jonathan.kaye

  • Newbie
  • Posts: 16
  • I'm a llama!
    • View Profile
T-basic new user issues...
« on: November 10, 2014, 06:26:28 PM »
I've been trying out all the t-basic keywords but some seem to really stump me. Specifically the while loop, for loop, and goto.  

I've linked 3 small .pc6 files with what I have tried and the issues I am having.

https://onedrive.live.com/embed?cid=5BC17F733268BD97&resid=5BC17F733268BD97%211293&authkey=AGYTNauw3TtZd8g

For loop - It seems to write over my variable.

N = DM[10]

for N = 5 to 10
   setio light_1
next

setlcd 3,1, "N=" + str$(N)

DM[10] counts up from another cusfn. This "for loop" writes 11 to N and not sure why.


While loop - Freezes when it gets to variable

N = DM[10]

While N > 5  
   setio light_1
Endwhile

setlcd 3,1, "N=" + str$(N)

DM[10] counts up from another cusfn. This "while loop" freezes when dm=5

GOTO - as if it's not even there

If dm[10] = 2
   goto @10
elseif dm[10] > 5
   goto @5
else
endif

@10 setlcd 2,1, "DM is 2"

@5 setlcd 3,1, "DM is greater than 5"

DM[10] counts up from another cusfn. The if statement works fine, but the GOTO's just fire off. I know I can put the statement in the "if" but I'm just playing with it

Thanks in advance for any help!


support

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3174
    • View Profile
    • Internet Programmable PLCs
Re:T-basic new user issues...
« Reply #1 on: November 11, 2014, 12:25:13 PM »
FOR LOOP:

The FOR loop will increment N from 5 to 10 and run the loop code each time, then it increments N to 11 and exits the loop because it is outside the range of 5-10. The loop counter will always end up being incremented by the step value (1 in this case) once more at the end of the loop.

WHILE LOOP:

The WHILE loop operates such that the loop code continuously executes when the loop condition is true. This means the setio light_1 code will run repeatedly when N is 6 and the loop will be exited; however, the output will never turn on because the program scan is stuck in the loop and the next I/O scan/update will never be reached. You can add an EXIT command as the second line in your WHILE loop to exit the loop after running the setio command once and continue with the program scan.

GOTO:

The code at @10 and @5 will execute every time because the program scan will execute it after the IF statement. You should add a RETURN statement between the ELSE and ENDIF lines if you want to bypass those lines of code unless the conditions of the IF statement are true.
Email: support@triplc.com
Tel: 1-877-TRI-PLCS

jonathan.kaye

  • Newbie
  • Posts: 16
  • I'm a llama!
    • View Profile
Re:T-basic new user issues...
« Reply #2 on: November 11, 2014, 03:10:57 PM »
I think I got it now... Thanks!

I'm trying to jam most commands into custom functions so I can build libraries. This is to control a furnace with a max duty cycle of 30%. I know there is a lot of ways to skin the cat but the while loops seem to work well for me in my rookie programming.

**************Heat Loop Custom Function**************
'This will "roughly" count in minutes how long this custom function/latch has been running.  

If testio(Latch) = 1
   DM[10]= DM[10]+1
ENDIF

'There are three 5 minute "heat on" blocks which are triggered by a "heat demand" custom function.
'A time count is in place to ensure only 15 minutes of 50 minutes will have heat (total 30% duty cycle).
'The first 5 minute block is automatic. The other blocks are checked with while loops at 6 minutes
'and 11 minutes.

N=DM[10]

'*****FIRST 5 MIN BLOCK*****

IF dm[10] <= 5
   setio furnace_on : setio Hold_Off
ENDIF

'*****At 6 mins, if want heat, give heat, if not shut down*****

WHILE N = 6
   if testio(Want_Heat)=1
      setio furnace_on : setio Hold_Off : exit
   elseif testio(Want_Heat)=0
      clrio furnace_on : setio Hold_Off : exit
   endif
   exit
ENDWHILE


'*****At 10 min, if want heat, give heat, if not shut down*****

WHILE N = 11
   if testio(Want_Heat)=1
      setio furnace_on : setio Hold_Off : exit
   elseif testio(Want_Heat)=0
      clrio furnace_on : setio Hold_Off : exit
   endif
   exit
ENDWHILE


'*****At 16 mins, cut heat, wait*****

WHILE N = 16
   if testio(Want_Heat)=1
      clrio furnace_on : setio Hold_Off : exit
   endif
   exit
ENDWHILE

IF N >= 50
      clrio Latch : DM[10] = 0 : setlcd 0,0, chr$(1) : clrio Furnace_On : N=0 :clrio Hold_Off
   ENDIF

setlcd 1,1, "DM[10] "+str$(dm[10])
setlcd 2,1, "N= "+str$(n)

support

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3174
    • View Profile
    • Internet Programmable PLCs
Re:T-basic new user issues...
« Reply #3 on: November 11, 2014, 10:03:09 PM »
Using a lot of WHILE..ENDWHILE loop will make the CPU stuck inside this one custom function for the entire duration of the control process, and the PLC will not be responding to changes in the digital inputs etc. So you either have to test the status of the digital inputs within the WHILE loop and act accordingly, or you could define the digital input as interrupt inputs so that if a digital input is turned ON an interrupt service routine (another custom function) can run to process changes in the digital input.
Email: support@triplc.com
Tel: 1-877-TRI-PLCS

jonathan.kaye

  • Newbie
  • Posts: 16
  • I'm a llama!
    • View Profile
Re:T-basic new user issues...
« Reply #4 on: November 12, 2014, 11:36:25 AM »
Hmm that sounds a little tricky...

I think i can get the same outcome with "and" with an IF statement.

If N=6 and testio(Want_heat)=1
      setio furnace_on : setio Hold_Off
  elseif testio(Want_Heat)=0
       clrio furnace_on : setio Hold_Off
   endif
Endif

VS...

WHILE N = 6
   if testio(Want_Heat)=1
      setio furnace_on : setio Hold_Off : exit
   elseif testio(Want_Heat)=0
      clrio furnace_on : setio Hold_Off : exit
   endif
   exit
ENDWHILE


I think I can do that.
« Last Edit: November 12, 2014, 12:52:31 PM by jonathan.kaye »

support

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3174
    • View Profile
    • Internet Programmable PLCs
Re:T-basic new user issues...
« Reply #5 on: November 12, 2014, 12:23:59 PM »
Yes, since you have put an exit command at the end of the WHILE loop, it is basically the equivalent of an IF statement, but an IF statement is cleaner and more readable for programming.
Email: support@triplc.com
Tel: 1-877-TRI-PLCS