Functions in a switch statement

  1. 10 months ago

    ProgrammingPerson

    13 Jan 2018 Bellefontaine 9185A

    Is there a way to put functions into a switch statement? i would like to use a switch statement for the lift i have as an example here, but im at a loss right now. i guess i mainly need to know how to set up a variable for this.

    Here is what i have so far

    #pragma config(Motor, port1, Lift1, tmotorVex393_HBridge, openLoop)
    #pragma config(Motor, port2, Lift2, tmotorVex393_MC29, openLoop)
    #pragma config(Motor, port3, Lift3, tmotorVex393_MC29, openLoop)
    #pragma config(Motor, port4, Lift4, tmotorVex393_MC29, openLoop)
    //*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//

    void LiftPower(int power){
    motor(Lift1) = power;
    motor(Lift2) = power;
    motor(Lift3) = power;
    motor(Lift4) = power;
    }

    task main()
    {

    switch(){
    case 1:
    LiftPower(127);
    break;

    case 2:
    LiftPower(-127);
    break;

    default:
    LiftPower(0);
    break;

    }

    }

  2. jpearman

    13 Jan 2018 Moderator, ROBOTC Tech Support, V5 Beta Moderator Los Angeles 8888
    Edited 10 months ago by jpearman

    Yes you can call functions from within a switch statement.

    The switch statement needs a variable to decide which branch it takes. You should place the whole thing in a while loop if this is part of some driver control code.

    #pragma config(Motor, port1, Lift1, tmotorVex393_HBridge, openLoop)
    #pragma config(Motor, port2, Lift2, tmotorVex393_MC29, openLoop)
    #pragma config(Motor, port3, Lift3, tmotorVex393_MC29, openLoop)
    #pragma config(Motor, port4, Lift4, tmotorVex393_MC29, openLoop)
    //*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//
    
    void LiftPower(int power){
      motor(Lift1) = power;
      motor(Lift2) = power;
      motor(Lift3) = power;
      motor(Lift4) = power;
    }
    
    task main()
    {
      int action = 0;
      
      // put everything in a while loop
      while(1) {
          // now set action in some way, we could use a button
          // this will just change it betweeen 0 and 1
          // you can figure out what other values it could have
          action = vexRT[ Btn6U ];
          
          switch( action ){
          case 1:
            LiftPower(127);
            break;
        
          case 2:
            LiftPower(-127);
            break;
        
          default:
            LiftPower(0);
            break;
          }
          
        wait1Msec(10);  
      }
    }
 

or Sign Up to reply!