What do you guys think of my code?

I wrote this code today for my X DRIVE robot with wings, intake, and a CATAPULT I will be adding my autonomous code soon BTW its my first year and I just learned to code

// User defined function
void myblockfunction_AUTONOMOUS() {
}

// User defined function
void myblockfunction_DRIVERCONTROL() {
  Controller1.rumble("-.-.");
  LeftBackMotor.spin(forward);
  RightBackMotor.spin(forward);
  LeftFrontMotor.spin(forward);
  RightFrontMotor.spin(forward);
  while (true) {
    LeftBackMotor.setVelocity(((Controller1.Axis3.position() + Controller1.Axis4.position()) + Controller1.Axis1.position()), percent);
    RightBackMotor.setVelocity(((Controller1.Axis3.position() - Controller1.Axis4.position()) - Controller1.Axis1.position()), percent);
    LeftFrontMotor.setVelocity((Controller1.Axis3.position() + (Controller1.Axis4.position() - Controller1.Axis1.position())), percent);
    RightFrontMotor.setVelocity((Controller1.Axis3.position() - (Controller1.Axis4.position() + Controller1.Axis1.position())), percent);
    if (Controller1.ButtonX.pressing()) {
      PneuL.set(true);
      PneuR.set(true);
    }
    if (Controller1.ButtonY.pressing()) {
      PneuL.set(false);
      PneuR.set(false);
    }
    if (Controller1.ButtonL1.pressing()) {
      INTAKE.spin(forward);
    }
    if (Controller1.ButtonL2.pressing()) {
      INTAKE.spin(reverse);
    }
    CATAPULT.setStopping(brake);
    if (Controller1.ButtonR1.pressing()) {
      CATAPULT.spin(forward);
    }
    else {
      CATAPULT.stop();
    }
  wait(5, msec);
  }
}

// User defined function
void myblockfunction_COMPETION() {
  myblockfunction_AUTONOMOUS();
  myblockfunction_DRIVERCONTROL();
}

// User defined function
void myblockfunction_TEST() {
  Brain.Screen.setCursor(1, 40);
  Brain.Screen.print("TEST MENU");
  if (Brain.Screen.yPosition() < 25.0 && Brain.Screen.xPosition() < 50.0) {
    Controller1.rumble("----");
    Brain.Screen.clearScreen();
    Brain.Screen.setCursor(3, 24);
    Brain.Screen.setFont(mono30);
    Brain.Screen.print("TEST MENU ");
    Brain.Screen.setCursor(7, 24);
    Brain.Screen.print("AUTONOMOUS TEST");
    Brain.Screen.setFillColor(white);
    Brain.Screen.drawRectangle(0, 0, 480, 160);
    if (Brain.Screen.yPosition() < 160.0) {
      Controller1.rumble("----");
      myblockfunction_AUTONOMOUS();
    }
  }
}

// "when started" hat block
int whenStarted1() {
  Controller1.rumble("....");
  Brain.Screen.clearScreen();
  myblockfunction_TEST();
  Controller1.Screen.print(printToController1_numberFormat(), static_cast<float>(Brain.Battery.capacity()));
  Brain.Screen.setCursor(1, 1);
  Brain.Screen.setFont(mono30);
  Brain.Screen.print(printToBrain_numberFormat(), static_cast<float>(Brain.Battery.capacity()));
  Brain.Screen.setCursor(3, 24);
  Brain.Screen.setFont(mono30);
  Brain.Screen.print("WELCOME 13565A");
  Brain.Screen.setFillColor(red);
  Brain.Screen.drawRectangle(0, 0, 240, 160);
  Brain.Screen.setFillColor(blue);
  Brain.Screen.drawRectangle(0, 0, 240, 160);
  if (Brain.Screen.xPosition() < 240.0) {
    Controller1.rumble("----");
    Brain.Screen.clearScreen();
    Brain.Screen.setCursor(1, 1);
    Brain.Screen.print("DRIVERCONTROL");
    Brain.Screen.setCursor(2, 1);
    Brain.Screen.print(printToBrain_numberFormat(), static_cast<float>(Brain.Battery.capacity()));
    Brain.Screen.setCursor(3, 1);
    Brain.Screen.print(printToBrain_numberFormat(), static_cast<float>(Brain.Battery.current(amp)));
    Brain.Screen.setCursor(4, 1);
    Brain.Screen.print(printToBrain_numberFormat(), static_cast<float>(Brain.Battery.voltage(volt)));
    Brain.Screen.setFillColor(black);
    myblockfunction_DRIVERCONTROL();
  }
  else {
    Controller1.rumble("----");
    Brain.Screen.clearScreen();
    Brain.Screen.setCursor(1, 1);
    Brain.Screen.print("COMPETITON WITH AUTNOMOUS");
    Brain.Screen.setCursor(2, 1);
    Brain.Screen.print(printToBrain_numberFormat(), static_cast<float>(Brain.Battery.capacity()));
    Brain.Screen.setCursor(3, 1);
    Brain.Screen.print(printToBrain_numberFormat(), static_cast<float>(Brain.Battery.current(amp)));
    Brain.Screen.setCursor(4, 1);
    Brain.Screen.print(printToBrain_numberFormat(), static_cast<float>(Brain.Battery.voltage(volt)));
    Brain.Screen.setFillColor(black);
    myblockfunction_COMPETION();
  }
  return 0;
}


int main() {
  // post event registration

  // set default print color to black
  printf("\033[30m");

  // wait for rotation sensor to fully initialize
  wait(30, msec);

  whenStarted1();
}
3 Likes

Pro tip - add comments
// this section will write “WELCOME 13565A” on the fourth row in mono30 text, then wipe
// it out twice so no one can read it - 31!T3

Seriously, just putting information about what the code should do, will help others help you debug your code.

Typically on larger program you will want to spell out how your robot is wired, the sensors used, etc. you will want have all your functions state what input they expect, what output they generate etc.

Hope that helps.

3 Likes

Yes that does help, in my programing I was trying to create an interface that works during competitions so that I can test certain things before comp like my auton or go straight to driving or maybe just a scrimmage so that’s also why I put welcome because that’s my opening screen

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