Lego NXT-RobotC ultrasonic sensor

I am newbie in programming so I need help with my Ultrasonic sensor driven NXT robot. It is attached to motor(A) and I'd like it to scan the room from robot's centerline to 90° left and 90° right in 30° increments (seven measurements total), store the data to an array and based on largest distance point my robot in the direction that measurement was taken to avoid obsticles. Is this possible at all? Or is there some better solution? Any advice or suggestion is more than welcome.

Other Answer1

This would work somewhat for avoiding obstacles, as for implementing it, it depends what you are programming the robot in. I only know lejos(java), in which a function for getting the angle to go would be something like:

If you're coding the robot in any code based language that should be fairly easy to convert (assuming you have functions such as rotateTo, otherwise you would have to use relative movements). Otherwise I don't know how easy this would be to do in the graphical programming language that you use originally.

comments:

Thank you Andreas for answering with a speed of light :)

I'm programming using RobotC and there's no "rotateTo" function but MotorEncoderTarget looks close..."motorTop" in youe example is the "name" of motor involved?

motorTop is just the name of an object that is an instance of the RegulatedMotor class which contains methods for controlling a motor.

Other Answer2

I would suggest attaching the ultrasonic sensor to a 180° servo that is vertical. You can take a measurement at a specific degree by assigning that degree to the servo. Using this: