title: CAPL using timers as delay causing crash
body: <p>I'm trying to edit a script written by someone else in CAPL. The original script works as intended, but my edited version compiles, but has some run-time issues. When I press key 'u' the whole program crashes, as if it's getting stuck in a loop it cannot break. A cut-down version of the code is shown below, for the parts I've edited:</p>
  
  <p>(The original code decrements CMD by 5 each time 'u' is pressed, and increments CMD by 5 each time 'U' is pressed. I want my new script to continuously ramp up and down CMD between my defined limits, at my defined step and time interval indefinitely until 'U' is pressed, which will reset CMD to 0)</p>
  
  <pre><code>variables
  {
  
      //Added for my script
      int rampFlag = 0;
      int tmrFlag  = 0;
      long rampRate = 50; //ms
      long rampStep = 1;
      long LimUpper = 100;    //max 350
      long LimLower = -100;   //min -350
      msTimer tmrRamp; 
  
      //Other existing variables from working script...
  }
  
  on start
  {
      //No changes for my script
  }
  
  //Added for my script:
  on timer tmrRamp
  {
      //Do nothing
  }
  
  on key 'u'
  {
      //Working script (commented out) This just decrements CMD every time 'u' is pressed:
      /*CMD = CMD - 5;
  
      if(CMD &lt; -350)
      {
          CMD = -350;
      }*/
  
      //Added for my script:
  
      CMD = 0;
  
      //ramp up and down the command CMD until key 'U' is pressed
  
      rampFlag = 1;
      while(rampFlag == 1)
      {
  
          //ramp up
      while(CMD &lt; LimUpper)
      {
          CMD = CMD + rampStep;   
          setTimer(tmrRamp,rampRate);             //start timer
          while(isTimerActive(tmrRamp) == 1){}    //wait for timer to expire
          //delay_ms(rampRate);    
      }
  
      //ramp down
      while(CMD &gt; LimLower)
      {
          CMD = CMD - rampStep;
          setTimer(tmrRamp,rampRate);             //start timer
          while(isTimerActive(tmrRamp) == 1){}    //wait for timer to expire
      }
      }
  }
  
  
  on key 'U'
  {
      //Working script (commented out) This just increments CMD every time 'U' is pressed:
      /*CMD = CMD + 5;
  
      if(CMD &gt;= 350)
      {
          CMD = 350;
      }*/
  
      //Added for my script:
      //Cease ramping up and down the torque request when key 'U' is pressed    
  
      rampFlag = 0;
      cancelTimer(tmrRamp);
      CMD = 0;
  }
  </code></pre>
  
  <p>I tried commenting out the outer while loop in "on key 'u' " in case the "on key 'U' " event statements are unable to be executed while " on key 'u' " statements are running....I was assuming any event could override any other even if it is still executing? Anyway running this still causes everything to freeze when I'm pressing 'u', so I think there is also a problem with the use of my timer functions.</p>
  
  <p>All I want is function similar to delay_ms(), but CAPL doesn't seem to recognise this, so I'm forced to try and use the setTimer and isTimerActive functions.</p>
  
  <p>Any suggestions?</p>