RobotC Interrupt Instruction

Is there an interrupt instruction in RobotC or something like that.

Let me explain… I got a while waiting for button press to execute the next code. Once pressed it enters a routine, but if the routine fails I want to be able to press another bumper button and break the execution of that routine so that it goes back to the main program and waits for the next button press to continue the execution of the next step of the routine without having to wait for it to finish.

I’m probably not understanding the question, but couldn’t you just ‘return’ from the routine when you detect the failure? The purpose of pressing another bumper button is not clear to me.

OK… we have one program that we run. The program starts and waits for the left bumper to be pressed to execute the first routine… after it finishes the first routine, they grab the robot place it back to the starting position and press the left bumper to execute the second routine and so on… that way they don’t waste time choosing the program to run next.

Sometimes the robot don’t grab the ball, or turns incorrectly, and they know it is going to fail, so instead of waiting for the routine to execute completely before going to the next, they want to be able to press the right bumper to finish executing the current routine, so that they may grab it, place it in the starting position and execute the next routine in line by pressing the left bumper.

Hope this clarifies a bit.

There’s no direct “interrupts”, but you could achieve something using two methods:

  1. “Breaking” out of the loop - in this instance, you could use the “break;” command to exit something like a repeating loop. In this example, the loop is running and waiting for for the motor to reach a specific encoder value (1080) by moving two motors forward. However, if for some reason the motors will never reach that target and you wish to terminate the loop “early”, you can use a condition to say “if a specific joystick button is pressed, break out of the while loop” by using the “break” command.
task main()
{
    while(getMotorEncoder(motor1) < 1080)
    {
        setMotorSpeed(motor1, 50);
        setMotorSpeed(motor6, 50);
        
        if(getJoystickValue(BtnLUp) == 1)
        {
            break;
        }
    }
}
  1. Multitasking + Monitoring - This example gives you a little more flexibility, but a little bit more complicated. The idea here is your “task main” is the monitor task, and then you can have up to 4 different “routine” tasks that you can start with a button. In the example below, you would put each routine’s code into the specific task, and then start that routine with a specified joystick button. This would allow you to “interrupt” the routine at any time
task routine1()
{
    //Actions for "Routine1"
}

task routine2()
{
    //Actions for "Routine2"
}

task main()
{
    while(true)
    {
        if(getJoystickValue(BtnEDown) == 1)
        {
            startTask(routine1);  //Begin running the code in "routine #1"

            //Wait for joystick button to be released so we don't start
            //the task multiple times
            while(getJoystickValue(BtnEDown) == 1)
            {
                sleep(10);
            }
        }

        if(getJoystickValue(BtnFDown) == 1)
        {
            startTask(routine2);  //Begin running the code in "routine #2"

            //Wait for joystick button to be released so we don't start
            //the task multiple times
            while(getJoystickValue(BtnFDown) == 1)
            {
                sleep(10);
            }
        }        

        //The "Cancel" button - it'll stop the tasks (but not the motors!)
        if(getJoystickValue(BtnLDown) == 1)
        {
            //Turn off tasks
            stopTask(routine1);
            stopTask(routine2);
            //Shut down motors (if needed)
            setMotorSpeed(motor1, 0);
            setMotorSpeed(motor6, 0);
        }
        
        //Repeat the cycle every 25ms - sleeping give processor time to other tasks
        sleep(25);
    }
}

Thank you… we where thinking on option #1, but wanted to know if there was something that involved less coding, but #1 will do. I think that option will be better for them since they are in elementary school and will probably be easier for them to code… even though they will have to use a ton of breaks :slight_smile: