SCL While loop?

Berra

Member
Join Date
Mar 2007
Location
Sweden
Posts
137
Dear expert.

This is my first try to program scl code. The problem is that the while loop dosen't count up.

Here is my code, i hope someone can help me out whit this.

FUNCTION FC1 : VOID
// FC1 är en slumpgenerator som ska slumpa
// fram tal använderen väljer
// Hur många gånger slumpandet ska utföras //
NAME : RANDOM
VAR_INPUT
HiLimit : INT ;
Antal: INT; // = 10 Antalet slumpade tal
END_VAR
VAR_OUTPUT
Random : INT ; // Random tal
END_VAR
VAR_TEMP
TimeTck : TIME; // aktuell systemtid
Index: INT;// vilkor för att loppen ska sluta
END_VAR

BEGIN
Index:=0;

WHILE Antal >= Index DO
Index:=Index+1;
TimeTck := TIME_TCK(); // Tid
Random := ABS(DINT_TO_INT(TIME_TO_DINT(TimeTck))) MOD HiLimit ;// Räkna ut slumpnumer

END_WHILE;
END_FUNCTION


/ Berra
 
Either index must be IN_OUT or convert to a FB and use a stat for the index. Temp variables are not remembered between calls.
 
I do think that the index does count up..

But you will never se that becausse its all done in the same scan..

The other thing I do notice is that if you put 3 as input Antal your function will only show the third number your random generator has made..

If you do want to have three different numbers you have to do it another way..

Example:

Call this function with a pulse whenever you want some random numbers..

//Just a little modification of your function not tested..

As a little tip you could also call the TimeTck function directly from SCL so you dont need that as an input just Variable:=TimeTck() if you dont want an variable you could just exchange your TimeTck with TimeTck()..

Code:
FUNCTION FC1 : VOID
NAME : RANDOM

VAR_INPUT
HiLimit : INT; 
Antal: INT;
END_VAR

VAR_OUTPUT
bError : BOOL;
Random : ARRAY[1..10] OF INT;
END_VAR

VAR_TEMP
TimeTck : TIME;
Index: INT; 
END_VAR

BEGIN

IF Antal > 10 THEN
bError:=1;
ELSE
   FOR Index:=1 TO Antal DO 
   TimeTck := TIME_TCK(); // Tid 
   Random[Index]:=ABS(DINT_TO_INT(TIME_TO_DINT(TimeTck))) MOD HiLimit; 
   END_FOR;
END_IF;
END_FUNCTION
 

Similar Topics

HI i would like to know how to get a variable that will store the amount of times a program has been executed. The issue is I have 3 DBs for 1 FB...
Replies
2
Views
81
Hi, I have an intermediate-advance knowledge of programming with TIA Porta in Ladder, would you recommend me to start learning SCL for it to...
Replies
11
Views
559
Hello nice to meet you, im new in here, I'm currently trying to convert code written in STL for a S7-400 to SCL for an S7-1500, because when i run...
Replies
5
Views
317
Hi everyone, I am new to this amazing world of PLC, well... I mean, in practice, since I already knew electronics, programming languages, IT, and...
Replies
7
Views
652
Hi all, This is my first post; I am new to PLC Controls stuff and English is not my native language, so I apologize in advance if something is...
Replies
4
Views
512
Back
Top Bottom