Autonomous Selection with Potentiometer

So I have two autonomous tasks and want to use a potentiometer to select between them. My thinking is that I can create the two routines (‘void autonLeft’ & ‘void autonRight’) and then just put this code for the actual autonomous portion:

task autonomous() {
       int potentiometer = SensorValue[in8];

	if(potentiometer >= 0 && potentiometer <= 800) 
       {
	  autonLeft();
       }
        else if(potentiometer >= 801 && potentiometer <= 1200) 
       {
	  autonRight();
       }
        else {
       }
    }

Would this work? Do I have to add anything else?

Yes sir, it will work!
Hopefully this helps :slight_smile:

Ok, thanks! I thought as much but wanted to get the opinion of someone more knowledgeable just to be safe. Will test it out tomorrow.

Yes but if I were you, I’d change the else if statement to say the following because the range of the sensor is 0-4095. However, that code will work fine on its own.

else if(potentiometer >= 801 && potentiometer <= 4095)
{
autonRight();
}

One thing that you want to learn about analog inputs is never just grab one and use it, you need to average the potentiometer value some and then make decision based upon the average.

You can do this by just adding up 100 of them and dividing (doesn’t take long) or, if you are using it for real-time angle measurement, use a low pass IIR filter.

what our kids did was place the averager in pre-auton then light LED’s to indicate the selected value. So you know for sure it has selected the right thing before telling the ref you are ready.