CODESYS - the IEC 61131-3 automation software

Welcome to the official CODESYS Forum by 3S-Smart Software Solutions GmbH | A member of the CODESYS Group
Deutsche Version English version russian version 
It is currently Sun Feb 17, 2019 10:49 am

All times are UTC+01:00




Post new topic  Reply to topic  [ 6 posts ] 
Author Message
PostPosted: Sun Jan 20, 2019 11:14 pm 
Offline

Joined: Mon Dec 17, 2018 11:20 am
Posts: 3
I've really struggled to find a way to do this but I cant seem to get it to work.

I want to get the minimum value from X amount of INT variables but not if its 0.

Theese are the once Ive tryed (and countless variations with <> 0 and so on):

Lets say the values are Var1 = 3, Var 2 = 5, Var3 = 0 and Var4 = 10

NewVariable := MIN(Var1, Var2, Var3, Var4);

This gives me the value 0.

NewVariable := LIMIT(1,(MIN(Var1, Var2, Var3, Var4)),15);

This gives me the value 1.

The value i really want is 3, which is the lowest value that is NOT 0.


Top
   
PostPosted: Mon Jan 21, 2019 5:43 am 
Offline
Frequent User
Frequent User

Joined: Fri Dec 23, 2005 7:36 pm
Posts: 178
A little brute force but you could do something like this.

Define a variable for the minimum value
MinVal: int := 32000;

Code:
IF Var1 <> 0 THEN
   MinVal := Min(MinVal,Var1);
END_IF

IF Var2 <> 0 THEN
   MinVal := Min(MinVal,Var2);
END_IF

IF Var3 <> 0 THEN
   MinVal := Min(MinVal,Var3);
END_IF

IF Var4 <> 0 THEN
   MinVal := Min(MinVal,Var4);
END_IF

IF Var5 <> 0 THEN
   MinVal := Min(MinVal,Var5);
END_IF


If you put the values in an array, then you could loop through it in a FOR loop.

_________________
nOrM
=====
nOrM = Norman Dziedzic Jr.


Top
   
PostPosted: Mon Jan 21, 2019 8:58 am 
Offline

Joined: Mon Dec 17, 2018 11:20 am
Posts: 3
Thanks, that almost worked. But I want the MinValue to be at 0 value until 1 or more of Val1-5 goes over 0, then i want MinValue to have that value...

Maybe I should start to look for a diffrent solution.

Iam making an elevator for school, and Ive added a second elevatorcar and I want the it to go down
to the lowest floor as soon as 1 of 5 variables becomes active. If the MinValue is 0 the elevator stops.


Top
   
PostPosted: Mon Jan 21, 2019 1:11 pm 
Offline
Frequent User
Frequent User

Joined: Fri Feb 23, 2018 3:41 pm
Posts: 100
Code:
MinValueNonZero := 0;
FOR index:=1 TO 4
 IF ElevatorBtn[index] > 0 THEN
  IF MinValueNonZero = 0 THEN
     MinValueNonZero := ElevatorBtn[index];
  ELSE
    IF MinValueNonZero > ElevatorBtn[index] THEN
    MinValueNonZero := ElevatorBtn[index];
    END_IF;
  END_IF;
 END_IF;
END_FOR;


EDIT 2019/01/24 : Correct fix for first value


Last edited by dFx on Thu Jan 24, 2019 3:05 pm, edited 1 time in total.

Top
   
PostPosted: Wed Jan 23, 2019 3:45 pm 
Offline

Joined: Mon Dec 17, 2018 11:20 am
Posts: 3
dFx wrote:
Code:
MinValueNonZero := 0;
FOR index:=1 TO 4
 IF ElevatorBtn[index] > 0 THEN
  IF MinValueNonZero > ElevatorBtn[index] THEN
    MinValueNonZero := ElevatorBtn[index];
  END_IF;
 END_IF;
END_FOR;


Genius :D

I solved it by using a whole bunch of IF statments but I will change that för this. Thanks!


Top
   
PostPosted: Thu Jan 24, 2019 3:05 pm 
Offline
Frequent User
Frequent User

Joined: Fri Feb 23, 2018 3:41 pm
Posts: 100
Edited my code


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 6 posts ] 

All times are UTC+01:00


Who is online

Users browsing this forum: Andreas Kehrer and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Limited