Displaying Variables in NXC
-
- Posts: 103
- Joined: 29 Sep 2010, 17:14
- Location: US
Displaying Variables in NXC
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
fuzzball27 >>-->
-
- Posts: 1818
- Joined: 02 Oct 2010, 02:19
- Location: Michigan USA
- Contact:
Re: Displaying Variables in NXC
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);
}
}
Matt
http://mattallen37.wordpress.com/
I'm all for gun control... that's why I use both hands when shooting
http://mattallen37.wordpress.com/
I'm all for gun control... that's why I use both hands when shooting
-
- Posts: 18
- Joined: 28 Sep 2010, 23:57
Re: Displaying Variables in NXC
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:
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 ':'.
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);
}
}
-
- Posts: 1818
- Joined: 02 Oct 2010, 02:19
- Location: Michigan USA
- Contact:
Re: Displaying Variables in NXC
I use a Wait(); to stop flicker. I suppose either way would work for this case.
Matt
http://mattallen37.wordpress.com/
I'm all for gun control... that's why I use both hands when shooting
http://mattallen37.wordpress.com/
I'm all for gun control... that's why I use both hands when shooting
Re: Displaying Variables in NXC
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
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
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
http://bricxcc.sourceforge.net/
Re: Displaying Variables in NXC
for displaying 3 values in 1 line with addional formating and adjustion of decimals I would suggest to use this simple macro.I need to know how to display variables on the NXT.
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); \
}
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);
}
HTH!
Who is online
Users browsing this forum: No registered users and 1 guest