Displaying Variables in NXC

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
fuzzball27
Posts: 103
Joined: 29 Sep 2010, 17:14
Location: US

Displaying Variables in NXC

Post 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 :|
fuzzball27 >>-->
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: Displaying Variables in NXC

Post 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);
  }
}
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
gusjansson
Posts: 18
Joined: 28 Sep 2010, 23:57

Re: Displaying Variables in NXC

Post 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 ':'.
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: Displaying Variables in NXC

Post by mattallen37 »

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 ;)
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: Displaying Variables in NXC

Post 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
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: Displaying Variables in NXC

Post 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!
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 4 guests