Skip to content

Use with AI

Use with AI

Program ​

Block or pause program, get program time

Block Program ​

Function "waitfor()" ​

These instructions show how the program can be paused until an input reaches a transferred value. In contrast to pausing the program, no fixed time is waited here, but until an input changes state.

The waitfor(inputName, value) function waits for the transferred input until it reaches the transferred value.

  • 1 Parameter: Name of the input (e.g. "INPUT_1")
  • 2 Parameter: Value to be waited for (0 or 1)
waitfor("INPUT_1", 0);

Function "waituntil()" ​

For the "wait for" function, you can define a timeout period and a corresponding error handling function that will be triggered if the specified time elapses without the condition being met.

As this feature has not yet been implemented graphically in the interface, a textual solution has been developed as a workaround that can be made available to the customer.

This section presents two methods for implementing the "waituntil" function.

  1. Simplified: Implementation via while loop directly in the main code. The disadvantage is that the entire logic and the parameters have to be programmed/defined each time.

    //Definition of parameters
    var start = getRuntime();   //Start time for timeout check
    var success = false;        //Reset check parameter
    var timeout = 5;            //Set timeout duration
    
    //While loop to check the "Wait for" condition within the time constraint
    
    while(start + timeout > getRuntime())  {
                    //Condition met, exit loop and set check parameter to true
                    if(getInput("INPUT_1") == true) {
                                                   success = true;
                                                   break;
                                   }
                                   sleep(10);
                    }
    //Execute error function in case of timeout
    
    if(success == false){
                    FailFunction();
    }
    
    //Definition of the error function triggered on timeout
    function failFunction() {
        //...
    }
  2. Reusable function: Implement the logic in a function whose return value determines whether to call failFunction. The main program does not need to repeat the complete implementation.

    //Calling the waituntil function with evaluation of the return value: execute error function if false
    
    if(!waituntil("OUTPUT_1", 5, true)) {
      failFunction();
    }
    
    //Definition of the waituntil function
    
    function waituntil(output, timeout, targetValue)  {
      var start = getRuntime();           //Start time for timeout check
      var success = false;                //Reset check parameter
      while(start + timeout > getRuntime())  {       //While loop to check the "Wait for" condition within the time constraint
        if(getOutput(output) == targetValue)  {
          success = true;                  //Set return parameter if condition met within the time
          break;
    
        }
        sleep(10);
      }
      return success;                   //Return the check parameter
    }
    
    //Definition of the error function triggered on timeout
    function failFunction() {
      //...
    }

Pause Program ​

The sleep(timeMillis) command has only one parameter, which specifies how long the program should pause at this point. The time is specified in milliseconds. The code snippet in the example would therefore let the program sleep for 1 second (1000 milliseconds).

sleep(1000);

Output elapsed program time ​

The elapsed program time is returned in seconds.

The command getRuntime() is used to output the elapsed program time. The command returns a float value with the elapsed program time in seconds.

var runtime = getRuntime();

show_info( runtime );