My Block equivalent in NXC

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
bungeshea
Posts: 207
Joined: 14 Aug 2011, 08:45
Location: Australia
Contact:

My Block equivalent in NXC

Post by bungeshea »

I am just starting to learn NXC, and I was wondering if there was an equivalent for NXT-G My Blocks in NXC :?:
muntoo
Posts: 834
Joined: 01 Oct 2010, 02:54
Location: Your Worst Nightmare
Contact:

Re: My Block equivalent in NXC

Post by muntoo »

'My Blocks' in NXC probably have more functionality than NXT-G My Blocks. :)

They're called subroutines/procedures and functions.

Subroutines:

Code: Select all

void MyVoid()
{
    TextOut(0, 0, "Hello MyVoid!");
    Wait(2000);
}

task main()
{
    MyVoid();
}
Guess what that code does?

-----

I recommend learning C++ (or at least, the C parts of it) from this tutorial. It's explained very well for a beginner.
Image

Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE


Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
bungeshea
Posts: 207
Joined: 14 Aug 2011, 08:45
Location: Australia
Contact:

Re: My Block equivalent in NXC

Post by bungeshea »

muntoo wrote:'My Blocks' in NXC probably have more functionality than NXT-G My Blocks. :)

They're called subroutines/procedures and functions.
Yes, but is there any way to save a section of code that you can call on in any of your programs?
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: My Block equivalent in NXC

Post by mattallen37 »

Yes there is. Make a library.

What I do, is have a folder that contains all my libraries, and include that path with the compiler settings. If I want to use one of libraries, all I have to do is #include it, and all the functions are available.
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
muntoo
Posts: 834
Joined: 01 Oct 2010, 02:54
Location: Your Worst Nightmare
Contact:

Re: My Block equivalent in NXC

Post by muntoo »

Ah, I get what you're talking about now. Use #includes.

A demonstration:

main.nxc

Code: Select all

#include "myblocks.nxc"

task main()
{
    HelloWorld();
}
myblocks.nxc

Code: Select all

void HelloWorld()
{
    TextOut(0, 0, "Hello, World!", 0);
    Wait(2000);
}
Just put #include "myblocks.nxc" in all of your programs from now on. (And don't forget to put "myblocks.nxc" in the same folder as each of your programs.)

-----

A better way would be to create a library, as Matt (who ninja'd me) suggested. That way, your "myblocks.nxc" code won't have to be compiled unnecessarily for one function/"block" you want to use out of a few unrelated hundred.
Image

Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE


Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: My Block equivalent in NXC

Post by mattallen37 »

muntoo wrote:...Just put #include "myblocks.nxc" in all of your programs from now on. (And don't forget to put "myblocks.nxc" in the same folder as each of your programs.)

-----

A better way would be to create a library, as Matt (who ninja'd me) suggested. That way, your "myblocks.nxc" code won't have to be compiled unnecessarily for one function/"block" you want to use out of a few unrelated hundred.
Muntoo, I think you missed my point. You do not have to save the libraries (or "myblocks.nxc") in the same directory... you can make the compiler look in an additional directory (where all the libraries or whatever else is located). It keeps me from have a zillion duplicates all over the place. It also means I only have to modify it in one place, and everything get modified.
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
bungeshea
Posts: 207
Joined: 14 Aug 2011, 08:45
Location: Australia
Contact:

Re: My Block equivalent in NXC

Post by bungeshea »

mattallen37 wrote: ... you can make the compiler look in an additional directory (where all the libraries or whatever else is located).
How do you do that?

Also is there any way to pass parameters into and out of voids? (Like data wires in NXT-G?)
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: My Block equivalent in NXC

Post by mattallen37 »

Like this:
BCC Compiler settings.jpg
Yes you can pass values in and out of a void function. Here is an example:

Code: Select all

void SetLimits(int value, int min, int max, int &result){
  if (value>max)value=max;
  if (value<min)value=min;
  result=value;
}
See how I did that? In that simple (example) function, I passed three ints into it (int value, int min, int max), and it send one back (int &end_value). The "&" means that it is an "output wire".

However, for that specific function, I would probably normally use something more like this:

Code: Select all

int SetLimits(int value, int min, int max){
  if (value>max)value=max;
  if (value<min)value=min;
  return value;
}
Instead of calling the function like this "SetLimits(value, min, max, result);", you would call it like this "NumOut(0, LCD_LINE1, SetLimits(value, min, max));". Because it is a "int" function, it has a numerical value (chosen by the "return" command).

Also, you are not limited to passing ints in and out of functions. You can use bool, char, byte, int, Uint, long, Ulong, array, string...
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
bungeshea
Posts: 207
Joined: 14 Aug 2011, 08:45
Location: Australia
Contact:

Re: My Block equivalent in NXC

Post by bungeshea »

Thanks! :D
Post Reply

Who is online

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