I am having trouble with the following code in VCS. I teach a HS robotics class and have run some basic Robot C codes on our EDR bots in the past. Now switching to C++ in VCS and struggling to understand the code. I have been through the Carnegie Mellon transitioning from Robot C to VCS videos and they have helped however I haven’t been able to find example codes for while loop conditions other than true conditions. After hours of time searching I decided to create an account here and ask for some help to get me through the rest of the school year. Can anyone give me some help with the attached code screenshots? We have built the V5 clawbots and are using the three wire ultrasonic sensors. In robot C we were able to make the clawbot drive forward until the robot’s sensor detected less than 10 inches then stop. I don’t quite understand how to code this in VCS C++
I have uploaded two files showing my written code in VCS, motors/sensors configuration and the glaring error message. Any help would be greatly appreciated.
The error message is stating that you’ve already defined the sonar sensor. Have you configured it already in the graphical “robot” menu? If so, you won’t need to do it again.
The code you’ve written will work. Make sure you remember to stop the motor after the while loop completes.
Thanks for the help. Since my program was not running I thought maybe I had to configure the sensor in the text page. I deleted the config since I had already configured it in the graphical menu. This got rid of the error message but alas the program still did not work even when I added the stop motor command. I checked my ports/wiring and it was all good. The only way I could get it to work was with the code attached in the image. I’m wondering if the VCS code immediately returns a value less than 10 even though my while loop instructs the bot to move forward while the object detected is greater than 10 inches away, which would make the loop false and end the program? Anyway, I appreciate your reply, and it helped me get closer to the answer I was looking for. Thanks.
hi, you can try to add a time delay before the first if condition. Your guess was right, during my test, when the program begin, it will get an original value from the sensor then disappeared quickly, add a delay, should get rid of this error value.