here’s a little piece of code that can be used to eliminate the good old cut/paste/find/replace . i use it to do totalizing on digital input points (fan/pump runtimes) but it could be customized for any application. its pretty compact and easy to add to, just add another case statement and adjust pointers to suit and away you go. i know this could be done using a subroutine, but some people do not like using subs, and i think its a cool little piece of code that shows the power of optoscript by combining a loop, switch and if statement as well as playing with pointers!
temp_int = 1; //init a counter
repeat
//ok select a totalizer and variable to hold the total on time
switch (temp_int)
case 1:
//point to the ontime totalizer
on_totalizer_ptr=&ex_fan1_run;
//point to the accumulated time variable
float_ptr=&pnb_exf1_runtime;
break
case 2:
//point to the ontime totalizer
on_totalizer_ptr=&ex_fan3_run;
//reuse the float pointer for this e
float_ptr=&pnb_exf3_runtime;
break
case 3:
//point to the ontime totalizer
on_totalizer_ptr=&ex_fan4_run;
//point to the accumulated time variable
float_ptr=&pnb_exf4_runtime;
break
case 4:
//point the pointer to the ontime totalizer
on_totalizer_ptr=&ex_fan2_run;
//point to the accumulated time variable
float_ptr=&pnb_exf2_runtime;
break
case 5:
//point to the ontime totalizer
on_totalizer_ptr=&chiller_chwp1_run;
//point to the accumulated time variable
float_ptr=&pnb_chiller_chwp1_runtime;
break
case 6:
//point to the ontime totalizer
on_totalizer_ptr=&chiller_chwp2_run;
//point to the accumulated time variable
float_ptr=&pnb_chiller_chwp2_runtime;
break
default:
//if we get here then we have run out of things to check so move NULL to the pointers
on_totalizer_ptr=null;
float_ptr=null;
break
endswitch
//if the pointer is null then skip this bit
if (not(on_totalizer_ptr==null)) then
//get the totalizer
fan_runtime=0;
fan_runtime=GetRestartOnTimeTotalizer(*on_totalizer_ptr);
//ok now convert the seconds to msecs
fan_runtime = fan_runtime*1000; //to the millisecond will do us
//now convert to hours and add to the total
fan_runtime=fan_runtime/(1000*60*60);
*float_ptr=*float_ptr+fan_runtime;
//increment the counter so we can move to the next unit.
IncrementVariable(temp_int);
endif
until (on_totalizer_ptr==null); //bail when the pointer is null
i hope this is useful for someone out there.
Nick