Posted 20 May 2020 (edited) HI everyone ive decided to try and use a structured text function block for an machine output calculation, to put it simply i want the machine to say how many units its outputting per minute so i thought it would be much nicer to have a basic maths Structured text block rather than a long bit of ladder for some simple instructions. i have 2 inputs, one is the start of the machine cycle, the other is the end of the machine cycle. every cycle is one unit output. i want to time the gap between input 1 and 2 then just a bit of simple maths 60/(cycle_time) for uniters per minute. i want this to be to 2 decimal places for a reasonable resolution. the main problem im having is the timers in structured text, im a little confused how they work i would do this in ladder by having input 1 set a bit and then input 2 resetting this bit(i assume i can set a bit by using an if statement [if input 1 true, timer{bool}true]) this timer bool would then activate the timer, again how i would do it in ladder would be to have a timer1 count down from &500 then use a maths block for 500-timer1 and this would give time elapsed this is the bit i really dont know how to do in structured text and would appreciate any help in the matter. otherwise i can just do it in ladder added my idea of what it would look like IF Cycle_start=TRUE THEN Timer_start:= TRUE; ELSIF Cycle_end=TRUE THEN Timer_start:= FALSE; END_IF; TIMHX(Timer_start, Cycle_timer,Timer_time); Cycle_time_bin:= 500-Timer_time; Cycle_time_real:= UINT_TO_REAL(Cycle_time_bin); Cans_per_min:= 60.0/Cycle_time_real; Edited 20 May 2020 by Bobodopalus Share this post Link to post Share on other sites
Posted 20 May 2020 after a bit more fiddling i got the timer to stop and start , the IF statement wasnt working as expected IF Cycle_start=TRUE THEN Timer_start:= TRUE; END_IF; IF Cycle_end=TRUE THEN Timer_start:= FALSE; END_IF; TIMHX(Timer_start, Cycle_timer,Timer_time); Cycle_time_bin:= (500-Timer_time); Cycle_time_real:= UINT_TO_REAL(Cycle_time_bin); Cans_per_min:= 60.0/Cycle_time_real/100; the thing that needs adjusting now is i need to record the time the timer ended on as it resets on cycle end, i need to snapshot it before it resets and the cycle_time_bin isnt working do i need some symbol attached to the 500? i tried & but that gave an error Share this post Link to post Share on other sites
Posted 20 May 2020 after alot more fiddling and this probably isnt the best way of doing it i got it to output what i wanted. for whatever reason dividing on the cans per min line gave an incorrect final answer but moving it to Cycle_time_real seemed to make it work :/ heres the final working code if anyones interested IF Cycle_start=TRUE THEN Timer_start:= TRUE; END_IF; IF Cycle_end=TRUE THEN Timer_snap:= Cycle_timer.PV; Cycle_end_bit:= TRUE; END_IF; IF Timer_10.CF THEN Timer_start:= FALSE; Cycle_end_bit:= FALSE; END_IF; TIMHX(Cycle_end_bit, Timer_10,1); TIMHX(Timer_start, Cycle_timer,500); Cycle_time_bin:= (500-Timer_snap); Cycle_time_real:= UINT_TO_REAL(Cycle_time_bin)/100.0; Cans_per_min:= 60.0/Cycle_time_real; Share this post Link to post Share on other sites