Switch case problems

  1. 5 weeks ago

    Hotsause

    Jan 8 Oregon 11761B

    hey, my name is Marshall from team 11761B, and I am having problems in my program, where when I use a switch() case: it is giving me compiler errors. If anybody could tell me how to fix this it would be very helpful.

    #pragma config(Sensor, dgtl1, rightencoder, sensorRotation)
    #pragma config(Sensor, dgtl2, leftencoder, sensorRotation)
    #pragma config(Motor, port1, rightmotor, tmotorVex393_HBridge, openLoop)
    #pragma config(Motor, port2, leftmotor, tmotorVex393_MC29, openLoop)
    //*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//

    void driveDirection(void ydirection, float inches, int power)
    {
    SensorValue[rightencoder] = 0;
    SensorValue[leftencoder] = 0;
    int dir = 0;

    switch(ydirection)
    {
    case forward:
    dir = 1;
    break;

    case backward:
    dir = -1;
    break;

    default:
    dir = 1;
    break;
    }
    int drivegoal = (inches * 49.9109902106);

    while(SensorValue[rightencoder] * dir < drivegoal || SensorValue[leftencoder] * dir < drivegoal)
    {
    motor[rightmotor] = (dir * power);
    motor[leftmotor] = (dir * power);
    }

    {
    motor[rightmotor] = 0;
    motor[leftmotor] = 0;
    }
    }

    //void driveBackward(float inches, int power) //New
    //{
    // SensorValue[rightencoder] = 0;
    // SensorValue[leftencoder] = 0;
    //
    // float drivegoal = -(inches * 49.9109902106);
    //
    // while(SensorValue[rightencoder] > drivegoal || SensorValue[leftencoder] > drivegoal)
    // {
    // motor[rightmotor] = -power;
    // motor[leftmotor] = -power;
    // }
    //
    // {
    // motor[rightmotor] = 0;
    // motor[leftmotor] = 0;
    // }
    //}

    void Turn(void direction, int degrees, int power)
    {
    SensorValue[rightencoder] = 0;
    SensorValue[leftencoder] = 0;
    int dir = 0;

    switch(direction)
    {
    case right:
    dir = 1;
    break;
    case left:
    dir = -1;
    break;
    }

    float turngoal = (degrees * 6.09777777778);

    while(SensorValue[leftencoder] * dir < turngoal || SensorValue[rightencoder] * dir > -turngoal)
    {
    motor[leftmotor] = dir * power;
    motor[rightmotor] = -dir * power;
    }

    {
    motor[leftmotor] = 0;
    motor[rightmotor] = 0;
    }
    }

    // if(direction == Right)
    // {
    // while(abs(SensorValue[leftencoder] > turngoal || SensorValue[rightencoder] < -turngoal))
    // {
    // motor[rightmotor] = -power;
    // motor[leftmotor] = power;
    // }
    //
    // {
    // motor[leftmotor] = 0;
    // motor[rightmotor] = 0;
    // }
    // }
    //
    // if(direction == Left)
    // {
    // while(abs(SensorValue[rightencoder] > turngoal || SensorValue[leftencoder] < -turngoal))
    // motor[rightmotor] = power;
    // motor[leftmotor] = -power;
    // }
    //
    // {
    // motor[leftmotor] = 0;
    // motor[rightmotor] = 0;
    // }
    // }
    //}

    void move(void xdirection, float amount, int power) //New
    {
    switch(xdirection)
    {
    case forward:
    driveDirection(xdirection, amount, power);
    break;
    case left:
    Turn(xdirection, amount, power);
    break;
    case right:
    Turn(xdirection, amount, power);
    break;
    case backward:
    driveDirection(xdirection, amount, power);
    break;

    }

    }

    task main() //Use move(direction, amount[if direction = left/right degrees, if direction = forward/backward use inches)
    {
    //Put code in here
    move(left, 90, 90);
    }

  2. jpearman

    Jan 13 Moderator, ROBOTC Tech Support, V5 Beta Moderator Rockstar, Los Angeles 8888

    The issues with you code are:
    "void" cannot be used as the type of a parameter (unless it's a pointer to void). So xdirection etc. has no type, you could make it an "int" or, as I will show below, a new type.
    "forward", "left" etc. in the switch statements have no meaning, the compiler does not know what these are.

    Here is one version of you code that defines a new type "tDirection" that can have predefined values. Behind the scenes these will be integers, directionForward will actually be 0, directionReverse will be 1, etc.

    #pragma config(Sensor, dgtl1, rightencoder, sensorRotation)
    #pragma config(Sensor, dgtl2, leftencoder, sensorRotation)
    #pragma config(Motor, port1, rightmotor, tmotorVex393_HBridge, openLoop)
    #pragma config(Motor, port2, leftmotor, tmotorVex393_MC29, openLoop)
    //*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//
    
    typedef enum _tDirection {
       directionForward,
       directionReverse,
       directionRight,
       directionLeft
    } tDirection;
    
    void driveDirection(tDirection ydirection, float inches, int power)
    {
      SensorValue[rightencoder] = 0;
      SensorValue[leftencoder] = 0;
      int dir = 0;
    
      switch(ydirection)
      {
      case directionForward:
        dir = 1;
        break;
    
      case directionReverse:
        dir = -1;
        break;
    
      default:
        dir = 1;
        break;
      }
      int drivegoal = (inches * 49.9109902106);
    
      while(SensorValue[rightencoder] * dir < drivegoal || SensorValue[leftencoder] * dir < drivegoal)
      {
        motor[rightmotor] = (dir * power);
        motor[leftmotor] = (dir * power);
      }
    
      {
        motor[rightmotor] = 0;
        motor[leftmotor] = 0;
      }
    }
    
    //void driveBackward(float inches, int power) //New
    //{
    //  SensorValue[rightencoder] = 0;
    //  SensorValue[leftencoder] = 0;
    //
    //  float drivegoal = -(inches * 49.9109902106);
    //
    //  while(SensorValue[rightencoder] > drivegoal || SensorValue[leftencoder] > drivegoal)
    //  {
    //  motor[rightmotor] = -power;
    //  motor[leftmotor] = -power;
    //  }
    //
    //  {
    //  motor[rightmotor] = 0;
    //  motor[leftmotor] = 0;
    //  }
    //}
    
    void Turn(tDirection direction, int degrees, int power)
    {
      SensorValue[rightencoder] = 0;
      SensorValue[leftencoder] = 0;
      int dir = 0;
    
      switch(direction)
      {
      case directionRight:
        dir = 1;
        break;
      case directionLeft:
        dir = -1;
        break;
      }
    
      float turngoal = (degrees * 6.09777777778);
    
      while(SensorValue[leftencoder] * dir < turngoal || SensorValue[rightencoder] * dir > -turngoal)
      {
        motor[leftmotor] = dir * power;
        motor[rightmotor] = -dir * power;
      }
    
      {
        motor[leftmotor] = 0;
        motor[rightmotor] = 0;
      }
    }
    
    //  if(direction == Right)
    //  {
    //  while(abs(SensorValue[leftencoder] > turngoal || SensorValue[rightencoder] < -turngoal))
    //  {
    //  motor[rightmotor] = -power;
    //  motor[leftmotor] = power;
    //  }
    //
    //  {
    //  motor[leftmotor] = 0;
    //  motor[rightmotor] = 0;
    //  }
    //  }
    //
    //  if(direction == Left)
    //  {
    //  while(abs(SensorValue[rightencoder] > turngoal || SensorValue[leftencoder] < -turngoal))
    //  motor[rightmotor] = power;
    //  motor[leftmotor] = -power;
    //  }
    //
    //  {
    //  motor[leftmotor] = 0;
    //  motor[rightmotor] = 0;
    //  }
    //  }
    //}
    
    void move(tDirection xdirection, float amount, int power) //New
    {
      switch(xdirection)
      {
      case directionForward:
        driveDirection(xdirection, amount, power);
        break;
      case directionLeft:
        Turn(xdirection, amount, power);
        break;
      case directionRight:
        Turn(xdirection, amount, power);
        break;
      case directionReverse:
        driveDirection(xdirection, amount, power);
        break;
    
      }
    
    }
    
    task main() //Use move(direction, amount[if direction = left/right degrees, if direction = forward/backward use inches)
    {
      //Put code in here
      move( directionLeft, 90, 90);
    }
 

or Sign Up to reply!