Page 223 - The Unofficial Guide to Lego Mindstorms Robots
P. 223
212
2. When legOS has control of the RCX, you can press and hold the Prgm button, then press the On-Off button. This blows
away legOS (and your program) and returns control of the RCX to the ROM. You'll need to do this before you can download a
new set of firmware to the RCX.
3. If your program doesn't stop itself and give control back to legOS, you'll need to erase the firmware by removing a battery.
If your program has a bug and does not terminate, you'll need to remove a battery to reset the RCX.
4. Sometimes, through a bug in legOS or in your program, the RCX cannot be initialized by removing the batteries for just a
few seconds. You will need to remove the batteries from your RCX and wait for a minute or so before the firmware is erased.
Some circuitry keeps the RCX's memory alive; in some cases, you need to wait for the circuitry to drain completely before the
firmware will be erased.
If the endless code-compile-download-reset cycle is getting you down, you might consider using an emulator. An emulator is a
special program that runs on your development PC but acts like an RCX. You can test your programs on the emulator much
faster than you can test them on an actual RCX. Currently one legOS emulator exists; see the "Online Resources" section for
details.
Debugging
The display is your best friend when it comes to debugging. legOS offers an impressive array of display functions. You can
show words that indicate which part of your program is executing or display the contents of variables. Of course, there's not a
lot of space to work with, but you could easily display a series of values for a short time. You could even write debugging code
that lets you cycle through data by pressing a button.
Unexpectedly Static Variables
One of the craziest things about legOS development is that global variables retain their value from one time you run your
program to the next. This is very important—it means that variables you initialize at declaration time are initialized only once,
when your program is first loaded on the RCX. Use the following program to convince yourself:
#include "conio.h"
int x = 44;
int main(void) {
lcd_int(x);
lcd_refresh();