using the same function but with arguments rather than normal void function breaks the code

Hello, im new to vex and i was assigned to do the autonomous phase for 2 robots. i made a set of functions that used one camera (called Vision19) and one ultrasonic (called *RangeFinderA) to track where the object is and move to it then lift it, and it worked!!, the problem is that i need to do the same method for 4 other cameras and ultrasonics so i changed the function from : void moveToObject() to moveToObject(vision camera, sonar ultrasonic, signature obj) and it completely broke the code even though its the same code.

the original code that works:

// Autonomus Phase

void VisionConfig() {
Vision19.setBrightness(55);
Vision19.setSignature(Vision19__YELLOW_GOAL);
}
bool is_close() {
if (RangeFinderA.distance(mm) < 40){
Brain.Screen.printAt(1, 60, “distance: %f cm”, RangeFinderA.distance(distanceUnits::cm));
return true;

}
return false;
}

bool find_object() {
//Brain.Screen.print(“trying to find object”);
//Brain.Screen.newLine();

Vision19.takeSnapshot(Vision19__YELLOW_GOAL);
while (Vision19.objectCount == 0) {
  Vision19.takeSnapshot(Vision19__YELLOW_GOAL);
  moveCar(0, 25);
  if (Vision19.objectCount != 0)
    return true;

}
if (Vision19.objectCount != 0)
return true;
return false;

}
bool is_lined() {

int screen_middle_x = 316 / 2;
Vision19.takeSnapshot(Vision19__YELLOW_GOAL);
if (Vision19.largestObject.centerX > screen_middle_x - 60 &&
Vision19.largestObject.centerX < screen_middle_x + 60 &&
Vision19.objectCount>0){
//Brain.Screen.print(“Lined”);
//Brain.Screen.newLine();
return true;
}
//Brain.Screen.print(“not Lined”);
// Brain.Screen.newLine();
return false;
}
void lining_up(){
//Brain.Screen.print(“lining up now i think”);
// Brain.Screen.newLine();
int screen_middle_x = 316 / 2;
int counter=0;
while (not is_lined()) {
//Brain.Screen.print("lining up again, Counter: ");
//Brain.Screen.print(counter);
//Brain.Screen.newLine();
counter++;
// snap a picture
Vision19.takeSnapshot(Vision19__YELLOW_GOAL);
// did we see anything?
if (Vision19.objectCount>0) {
// where was the largest thing?
if (Vision19.largestObject.centerX < screen_middle_x - 60) {
// on the left, turn left
is_lined();
Brain.Screen.print(“Turning Right”);
Brain.Screen.newLine();
moveCar(0, 35);
} else if (Vision19.largestObject.centerX > screen_middle_x + 60) {
// on the right, turn right
is_lined();
Brain.Screen.print(“Turning Left”);
Brain.Screen.newLine();
moveCar(0, -35);
} else {
// in the middle, we’re done lining up
Brain.Screen.print(“Stop”);
Brain.Screen.newLine();
is_lined();
moveCar(0, 0);
break;
}
} else {
// saw nothing, relax
Brain.Screen.print(“found nothing sorry”);
Brain.Screen.newLine();
}
}

}
bool moveToObject() {
//#region config_init
VisionConfig();

// camera image is 316 pixels wide, so the center is 316/2
while (1) {

if (not is_close()) {
  Brain.Screen.print(RangeFinderA.distance(mm));
  Brain.Screen.newLine();
  Vision19.takeSnapshot(Vision19__YELLOW_GOAL);
  if(find_object()){
    if (not is_lined()) {
     //Brain.Screen.print("not linedup anymore");
     lining_up();
     //Brain.Screen.newLine();
    }

   if (is_lined() && not is_close()&&
       Vision19.objectCount > 0) {
     //Brain.Screen.print("moving forwards");
      //Brain.Screen.newLine();
     moveCar(50, 0);
   }

  }
} else
  if(delayAuto >10000){
    moveCar(0,0);
    Brain.Screen.print("DONE MOVING TO OBJECT THNKFULLY");
    break;
  }
screenClear++;
if(screenClear >=1000){
  screenClear=0;
  Brain.Screen.clearScreen();
  Brain.Screen.setCursor(1,1);

}

delayAuto++;

}
return true;
}
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!

vexcodeInit();
setup();
moveToObject();
comp.drivercontrol(userControl);
while (true) {
Brain.Screen.printAt(1, 60, “distance: %f cm”, RangeFinderA.distance(distanceUnits::cm));
// Keep the main program running.
wait(0.05, seconds);
}
}

then the code after adding argument and does not work:

// Autonomus Phase

void VisionConfig(vision camera ,signature obj ) {
camera.setBrightness(55);
camera.setSignature(Vision19__YELLOW_GOAL);
}
bool is_close(sonar UltraSonic) {
if (UltraSonic.distance(mm) < 40){
Brain.Screen.printAt(1, 60, “distance: %f mm”, UltraSonic.distance(distanceUnits::mm));
return true;

}
return false;
}

bool find_object(vision camera, signature obj) {
//Brain.Screen.print(“trying to find object”);
//Brain.Screen.newLine();
Brain.Screen.printAt(1, 100, “trying to find obj”);

camera.takeSnapshot(Vision19__YELLOW_GOAL);
while (camera.objectCount == 0) {
  camera.takeSnapshot(Vision19__YELLOW_GOAL);
  moveCar(0, 25);
  if (camera.objectCount != 0){
     Brain.Screen.printAt(1, 120, "obj found");
    return true;

  }

}
if (camera.objectCount != 0)
return true;
return false;

}
bool is_lined(vision camera, signature obj) {

int screen_middle_x = 316 / 2;
camera.takeSnapshot(Vision19__YELLOW_GOAL);
if (camera.largestObject.centerX > screen_middle_x - 60 &&
camera.largestObject.centerX < screen_middle_x + 60 &&
camera.objectCount>0){
Brain.Screen.printAt(1, 140, " lined up");
//Brain.Screen.print(“Lined”);
//Brain.Screen.newLine();
return true;
}
Brain.Screen.printAt(1, 160, “NOT lined up”);
//Brain.Screen.print(“not Lined”);
// Brain.Screen.newLine();
return false;
}
void liningUp(vision camera2){
//Brain.Screen.print(“lining up now i think”);
// Brain.Screen.newLine();
Brain.Screen.printAt(1, 20, “function LiningUp”);
int screen_middle_x = 316 / 2;
int counter=0;
while (not is_lined( camera2, Vision19__YELLOW_GOAL)) {
//Brain.Screen.print("lining up again, Counter: ");
//Brain.Screen.print(counter);
//Brain.Screen.newLine();
counter++;
// snap a picture
camera2.takeSnapshot(Vision19__YELLOW_GOAL);
// did we see anything?
if (camera2.objectCount>0) {
// where was the largest thing?
if (camera2.largestObject.centerX < screen_middle_x - 60) {
// on the left, turn left
is_lined( Vision19, Vision19__YELLOW_GOAL);
Brain.Screen.printAt(1, 180, “Turning Right”);
moveCar(0, 35);
} else if (camera2.largestObject.centerX > screen_middle_x + 60) {
// on the right, turn right
is_lined( Vision19, Vision19__YELLOW_GOAL);
Brain.Screen.printAt(1, 180, “Turning Left”);

        moveCar(0, -35);
      } else {
        // in the middle, we're done lining up
        Brain.Screen.printAt(1, 180, "Done lining up");
         is_lined( Vision19,  Vision19__YELLOW_GOAL);
        moveCar(0, 0);
        break;
      }
    } else {
      // saw nothing, relax
      Brain.Screen.printAt(1, 180, "found nothing");
    }
  }

}
bool moveToObject(sonar Ultrasonic,vision camera,signature obj) {
//#region config_init
VisionConfig(camera,Vision19__YELLOW_GOAL);

// camera image is 316 pixels wide, so the center is 316/2
while (1) {
Brain.Screen.printAt(1, 60, “distance: %f cm”, RangeFinderA.distance(distanceUnits::cm));

if (not is_close(Ultrasonic)) {
  Brain.Screen.printAt(1, 60, "distance: %f cm", RangeFinderA.distance(distanceUnits::cm));
  camera.takeSnapshot(Vision19__YELLOW_GOAL);
  if(find_object(camera,Vision19__YELLOW_GOAL)){
    Brain.Screen.printAt(1, 200, ": obmoveToObjectject found");
    if (not is_lined( camera,  obj)) {
      Brain.Screen.printAt(1, 200, "moveToObject: its not lined liningUp");
     //Brain.Screen.print("not linedup anymore");
     liningUp(camera);
     //Brain.Screen.newLine();
    }

   if ( is_lined( camera,  obj) && not is_close(Ultrasonic)&&
       camera.objectCount > 0) {
         Brain.Screen.printAt(1, 200, "moveToObject: LinedUp moving forwards");
     //Brain.Screen.print("moving forwards");
      //Brain.Screen.newLine();
     moveCar(50, 0);
   }

  }
} else
  if(delayAuto >= 100){
    delayAuto =0;
    moveCar(0,0);
    Brain.Screen.print("DONE MOVING TO OBJECT THNKFULLY");
    break;
  }
  
screenClear++;
if(screenClear >=1000){
  screenClear=0;
  Brain.Screen.clearScreen();
  Brain.Screen.setCursor(1,1);

}

delayAuto++;

}
return true;
}
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!

vexcodeInit();
setup();
wait(1,seconds);
moveToObject(RangeFinderA,Vision19,Vision19__YELLOW_GOAL);
comp.drivercontrol(userControl);

while (true) {
Brain.Screen.printAt(1, 60, “distance: %f cm”, RangeFinderA.distance(distanceUnits::cm));
// AutoMoveCar();
// obj_in_center();
// Keep the main program running.
wait(0.05, seconds);
}
}

the problem im getting with the second code is that the car never finds the object so it just spins around forever even though the screen prints (object found) after the camera detects the object

ps. some part from the second code might differ from the original because of trail and error of me trying to fix it

Welcome to the forums @talalowaid. Would you mind editing your post to put code tags around your code? You can do this by putting 3 back ticks ( ```) at the beginning and end of your code, or using code tags: [code] [/code] . This formats your code to be more readable, and will help people help you. Thanks!

3 Likes