Need help with functions

So i’m working in vexcode V5 text and want to create a function that creates a min/max encoder value for a motor. However, i can’t see a way how to substitute the motor in the function arguments. here is the code

void minmax(motor, int max, int min){

if(motor.rotation >= min){

}

}

I don’t have anything beyond this because i don’t know how i’d be able to make the motor you want to minmax be within the arguments

Hey I can do that!

void minmac(vex::motor &motorname, int max, int min)
{
  if(motorname.rotation() >= min)
  {}
}

That should work. Or at least I hope so :grin:

2 Likes

i’ll check it out!

20characters

Thanks! it works, but now, would it be possible to do the same thing with a controller button?

Do something like

if (Controller1.buttonX.pressing()){
cinmac (motor1, 100, 0);
}

i see what you’re trying to say here, but what i meant was if it was possible to substitute a button in the function with a button that you put into the argument, so like

void bblehbleh (int speedorsomething, BUTTON, int goovy){
if(Controller1.BUTTON.pressing()){
}
something like that

Yeah but it’ll be a bit longer. The option I’m thinking of involves checking what ‘BUTTON’ equals.

If(controller.buttonx.pressing and BUTTON == “x”){
    Code
}
if(controller.buttony.pressing and BUTTON == “y”){
    More code
}

(‘And’ in CPP can be achieved with ‘&&’)

the problem with that is that in my function i would need to call for every single button and only 1 would be used, it would just be a giant block of text and would be easier to just not use the function at all, though this might be possible but right now this isn’t exactly what i’m looking for

Might I ask why you want a function to be controlled with different buttons?

i want to make coding as convenient and organized as possible, currently i have a whole bunch of if’s else if’s and else’s just to make motor groups move with some button push, creating this function will help make coding easy and oraganized

Well if you’re really worried about organization then consider using multiple files, but I really doubt your code is so messy that you’ll need it.

1 Like

Building on @trontech569 example you can pass reference to a button into a function:

void minmac(vex::motor &motorRef, 
             vex::controller::button &buttonRef, 
             int minVal,
             int maxVal)
{
   if( buttonRef.pressing() )
   {
      motorRef.spin(forward, minVal, pct);
   }
   else
   {
      motorRef.stop();
   }
} 

void main()
{
     minmac(Motor1, Controller1.buttonX, 50, 80);
     minmac(Motor2, Controller1.buttonY, 30, 90);
}
2 Likes

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.