Page 1 of 1
Displaying Variables in NXC
Posted: 18 Oct 2010, 22:00
by fuzzball27
I need to know how to display variables on the NXT. It would be very helpful for future projects. For example, displaying the compass heading on one robot to a remote would be handy. It seems simple enough to display a variable's value but binary programming is sensitive
Re: Displaying Variables in NXC
Posted: 18 Oct 2010, 22:13
by mattallen37
Use something like this.
Code: Select all
int heading;
task main()
{
SetSensorLowspeed(S1);
while (true)
{
heading=SensorHTCompass(S1);
Wait (50);
ClearScreen();
NumOut(0,LCD_LINE1,heading);
}
}
Re: Displaying Variables in NXC
Posted: 18 Oct 2010, 23:34
by gusjansson
I usually avoid using ClearScreen since it can cause screen flicker. Instead I pair a TextOut with the NumOut. I use the TextOut to first display a caption as well as a bunch of spaces that will overwrite the previous value and then I display the new value offset by the length of the caption. For example, to display the Compass, I do this:
Code: Select all
//=====================================================================
// HiTechnic Compass Viewer
#define COMPASS S1
task main()
{
int heading;
SetSensorLowspeed(COMPASS);
Wait(100);
while(true) {
heading = SensorHTCompass(COMPASS);
TextOut(0, LCD_LINE4, "Compass: ");
NumOut(8*6, LCD_LINE4, heading);
Wait(50);
}
}
The 8*6 for the x position of the NumOut is the column*pixel width of a character. Since "Compass:" is 8 characters, 8*6 will put the number just right of the ':'.
Re: Displaying Variables in NXC
Posted: 18 Oct 2010, 23:37
by mattallen37
I use a Wait(); to stop flicker. I suppose either way would work for this case.
Re: Displaying Variables in NXC
Posted: 19 Oct 2010, 02:26
by afanofosc
The difference is ClearScreen erases the entire screen which takes longer than redrawing/erasing a single line. There is a new API function called ClearLine which you can use to only erase the specified line. I really like Gus's code, though.
The OP may have been talking about sending a variable's value to another device, though. For that I would consider using SendRemoteNumber. This uses the MessageWrite direct command and adheres to the message format used by the NXT-G message block.
John Hansen
Re: Displaying Variables in NXC
Posted: 19 Oct 2010, 13:13
by HaWe
I need to know how to display variables on the NXT.
for displaying 3 values in 1 line with addional formating and adjustion of decimals I would suggest to use this simple macro.
Code: Select all
#define printf3( _x, _y, _format1, _format2, _format3, _value1, _value2, _value3) { \
string sval1 = FormatNum(_format1, _value1); \
string sval2 = FormatNum(_format2, _value2); \
string sval3 = FormatNum(_format3, _value3); \
string s =sval1+sval2+sval3; \
TextOut(_x, _y, s); \
}
it's working somewhat like the original printf command (ANSI C stdio) but uses single format strings for every single value.
Blanks are inserted automatically.
the format strings may use integer format like "%2d" or fp format like "%4.1f"
use e.g.:
Code: Select all
#define printf3( _x, _y, _format1, _format2, _format3, _value1, _value2, _value3) { \
string sval1 = FormatNum(_format1, _value1); \
string sval2 = FormatNum(_format2, _value2); \
string sval3 = FormatNum(_format3, _value3); \
string s =sval1+sval2+sval3; \
TextOut(_x, _y, s); \
}
task main(){
int number=1;
int length=23;
float heading=45.6;
printf3(0,56, "n=%1d", " L=%2d", " H=%4.1f", number,length,heading);
while (true);
}
I assume if you extract the formatted value string (string s=sval1+sval2+sval3 of the macro) you might pass it to a BT send string function
HTH!