Programming rangefinder sensor in V5 pro

for programming skills in tipping point i’m using the rangefinder sensor to locate the mobile goals. i want it to drive and when its with in a certain distance of the goal, a clamp spins and grabs it. but i need it to continuously do it. so every single time a goal is with in the certain distance the clamp will spin.

i tried the code below

waitUntil (Sonar1.distance(distanceUnits::in) <=3);
Drivetrain.stop();
 clamp.spin(directionType::rev,100,velocityUnits::pct);
  wait(200, msec);
  clamp.stop(brakeType::hold);


as well as this
int sonarReading = 0;
void sonarValue(){
  if (Sonar1.distance(distanceUnits::in) <=3) {
  clamp.spin(directionType::rev,100,velocityUnits::pct);
  wait(200, msec);
  clamp.stop(brakeType::hold);
  }
}

any ideas on how to code this sensor, or can anyone notice something wrong with what i’ve done. any help would be greatly appreciated :slight_smile:

edit: code tags added by mods.

Is this code working?

Do you have it in a forever loop? What is the code around it? What triggers that code to execute?