Potentiometer That Switches Autonomous

Im trying to write a code for a potentiometer that switches autonomous for each side but I never have used a potentiometer before. Can anyone provide this code or direct me where to find help?

PROS? VCS? ROBOTC? VEXCode?
We need :clap: to know :clap: more information :clap: before :clap: we can provide :clap: a response :clap:

9 Likes

ROBOTC. Sorry for not including the language my bad.

1 Like

For some basic information, the potentiometer has a 250 degree rotation freedom that ranges from 0 to 4095. So if you are wanting to code something for autonomous for switching sides, you can do something along the lines of this:

if(sensorValue[Potentiometer] > 2047){
//Code 1
}
else{
//Code 2
}
3 Likes

Thank you a lot this is going to help a lot!

1 Like

No problem! Also, make sure that you get familiar with VCS, VEXCode, or PROS in the future because it will eventually be a requirement in the future when V4 becomes discontinued in the upcoming years.

2 Likes

For sure I am already experimenting for when we get a v5 but that isn’t in the for seeable future however

1 Like

I had a potentiometer for auton switching in TP. Worked pretty well. That solution above was basically exactly what I used, but with “else if” statements embedded.

If (pot<LargeValue)
     {
          If (pot>SmallValue)
               {
                    //Run code 
               }
      }
Else If (pot<LargerValue)
     {
          If (pot>SmallishValue)
               {
                    //Run code 2
               }
      }
Else
     {
          //do nothing
     }

Edit: obviously you can embed the statements within each other, but, for simplicity in debugging, I did this setup.

I’d also like to point out that this thread is in the wrong category.

1 Like

(A tad bit off topic but an annoying thing about coding)
Okay, I never realized how annoying coding languages are with how they use statements. Some languages use “else if” and another has it doing “elseif” statements. Some languages use curly brackets “{” and “}” to determine that what’s inside is what would run, other languages use the [Tab] indents to determine what does and doesn’t run, and another group of languages doesn’t require anything and just has “end” typed to determine what closes it. These small little differences make me want to slam my keyboard sometimes, but I’m glad that almost all languages in VEX are fairly similar so I can code in any language VEX offers without having to throw my desktop out of my window. :slight_smile:

But RobotC does use the format presented, correct? I was fairly confident I remembered somewhat (minus specific syntax, mostly the logic) how RobotC worked?

Edit: nevermind, I see.

int pot = sensorValue[Potentiometer];
int largeValue = 3071;
int smallValue = 1024;
int smallishValue = 2047;
int largerValue = 4095;

if (pot<largeValue)
     {
          if (pot>smallValue)
               {
                    //Run code 
               }
      }
else if (pot<largerValue)
     {
          if (pot>smallishValue)
               {
                    //Run code 2
               }
      }
else
     {
          //do nothing
     }

I think this is the correct format.

3 Likes

Minus the taskMain() (or for that matter If we’re getting technical, but we aren’t. :smile:

1 Like

This will pretty much work, but I would suggest the following improvements:

First, this code will do nothing if you happen to land the pot on one of the threshold values (3071, 1024, 2047, and 4095).

Second, the two ranges (1024-3071 and 2047-4095) overlap with each other, which may be a bit confusing to the user.

Third, when you have values that you want to define, but that don’t change during the program, it’s good practice to use #define preprocessor directives rather than variables.

Implementing these yields the following (for selecting between 4 autonomous routines):

//at or near the top of the file:
#define POT_MIN 0
#define THRESHOLD_1 1024
#define THRESHOLD_2 2048
#define THRESHOLD_3 3072
#define POT_MAX 4095

//the autonomous task/function provided by your competition template:
task Autonomous(){
    int pot_value = sensorValue[Potentiometer];//or however you read a pot in your language of choice
    if ((pot_value >= POT_MIN) && (pot_value < THRESHOLD_1)){
        //code for routine 1
    }
    else if ((pot_value >= THRESHOLD_1) && (pot_value < THRESHOLD_2)){
        //code for routine 2
    }
    else if ((pot_value >= THRESHOLD_2) && (pot_value < THRESHOLD_3)){
        //code for routine 3
    }
    else if ((pot_value >= THRESHOLD_3) && (pot_value <= POT_MAX)){
        //code for routine 4
    }
}
2 Likes