Wednesday, May 26, 2010

Learning NXC - part 2

This idea to try to rewrite NXT-G programs from David J.Perdue's The Unofficial LEGO MINDSTORMS NXT Inventor's Guide using NXC really helps me learn. I just did this for the ClawBot1 program (which you can find in the chapter 11-16 source code archive on this page) and it was a lot faster than my initial try for the BumperBot. The code is on Google code here.

Nothing fancy, just 3 things I want to mention so I don't forget:
  1. When I first started to write the code, repeat(3) didn't work for some reason. I changed to repeat(repeatTimes) where repeatTimes = 3 and it started to work. Later when the program was done, I changed back to repeat(3) and this time it worked great. So, I guess the initial problem was just a fluke.
  2. The equivalent of NXT-G's "Wait for completion" when playing a sound file is this:
    until (SoundFlags() == SOUND_FLAGS_IDLE);
    I found this in John Hansen's LEGO Mindstorms NXT Power Programming: Robotics in C book and it is a great find: thanks, John!
  3. From a learning point of view, is really cool to try to match an existing program to the dot. I wrote code for the US Sensor before and never really bothered with exact distances, it worked pretty well using values like 20 or 35 (I knew that it returns values between 0 and 255) but I really had no idea what these numbers represent. Since the original program looked for 11 inches I had to dig a bit (in the forums at and found out that 0 to 255 are centimeters; knowing this it was easy to obtain the distance in inches. This is not a big deal but I just learned something more.
Update: I just finished the NXC version of ClawBot2 program in 2 versions: everything in main() and using a function (I noticed that some of the actions were common with only a couple of differences so I tried to use a function with 2 arguments and it worked great).

No comments:

Post a Comment