Question

I'm programming a robot, and unfortunately in its autonomous mode I'm having some issues. I need to set an integer to 1 when a button is pressed, but in order for the program to recognize the button, it must be in a while loop. As you can imagine, the program ends up in an infinite loop and the integer values end up somewhere near 4,000.

 task autonomous()
   {
    while(true)
        {
    if(SensorValue[positionSelectButton] == 1)
        {
            positionSelect = positionSelect + 1;
            wait1Msec(0350);
        }
        }
   }

I've managed to get the value by using a wait, but I do NOT want to do this. Is there any other way I can approach this?

Was it helpful?

Solution

assuming that the SensorValue comes from a physical component that is asynchronous to the while loop, and is a push button (i.e. not a toggle button)

task autonomous()
{
    while(true)
    {
        // check whether 
        if(current_time >= next_detect_time && SensorValue[positionSelectButton] == 1)
        {
            positionSelect = positionSelect + 1;

            // no waiting here
            next_detect_time = current_time + 0350;
        }

        // carry on to other tasks
        if(enemy_is_near)
        {
            fight();
        }

        // current_time 
        current_time = built_in_now()
    }
}

Get the current time either by some built-in function or incrementing an integer and wrap around once reach max value.

Or if you are in another situation:

task autonomous()
{
    while(true)
    {
        // check whether the flag allows incrementing
        if(should_detect && SensorValue[positionSelectButton] == 1)
        {
            positionSelect = positionSelect + 1;

            // no waiting here
            should_detect = false;
        }

        // carry on to other tasks
        if(enemy_is_near)
        {
            if(fight() == LOSING)
               should_detect = true;
        }
    }
}

OTHER TIPS

Try remembering the current position of the button, and only take action when its state changes from off to on.

Depending on the hardware, you might also get a signal as though it flipped back and forth several times in a millisecond. If that's an issue, you might want to also store the timestamp of the last time the button was activated, and then ignore repeat events during a short window after that.

You could connect the button to an interrupt and then make the necessary change in the interrupt handler.

This might not be the best approach, but it will be the simplest.


From The Vex Robotics catalogue :

(12) Fast digital I/O ports which can be used as interrupts

So, most probably which ever micro-controller of Vex you are using will support Interrupts.

Your question is a bit vague
I m not sure why u need this variable to increment and how things exactly work...but i ll make a try.Explain a bit more how things work for the robot to move...and we will be able to help more.

task autonomous()
{
    int buttonPressed=0;
    while(true)
    {
        if(SensorValue[positionSelectButton] == 1)
        {
            positionSelect = positionSelect +1;
            buttonPressed=1;
        }
        else{
            buttonPressed = 0;
        }


        //use your variables here 
        if( buttonPressed  == 1){
            //Move robot front a little
        }

    }
}

The general idea is :
First you detect all buttons pressed and then you do things according to them
All these go in your while loop...that will(and should) run forever(at least as long as your robot is alive :) )
Hope this helps!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top