Tutorial: RobotC Bluetooth Examples

Bluetooth is a cool feature and it’s especially fun if you can make it work. That’s apparently a bit tricky, but it’s really not difficult if you’ve got a simple place to start. This page will show you two sets of programs that demonstrate the most basic Bluetooth example programs for two NXT with RobotC. (You can use bluetooth with NXT-G, too, but that’s not discussed here).

Set up

  • This code was tested with RobotC 3.08. Newer versions may have issues with this code.
  • Two NXT bricks
  • You can just copy paste the code from below, or download all 4 sample programs..
  • Connect the NXT’s using the menu on the NXT brick:
    • (Only the first time) Go to Bluetooth > Search
    • Go to Bluetooth > My Contacts
    • Choose the NXT that you want to connect to
    • Choose Connect.
    • The <> symbol in the top left of the screen indicates that your NXT’s are connected

Sending and receiving one value

In some occasions you want to send some basic numbers from one NXT to the other. The next examples (SenderSimple.c and ReceiverSimple.c) demonstrate the functionality to send one value from one NXT to another. I assume that your NXT’s are connected via Bluetooth (see “set up” section).

  • Transfer ReceiverSimple.c to one NXT. Use USB for this! (Bluetooth will be in use between your NXT’s.)
  • Transfer SenderSimple.c to your other NXT. Use USB for this!
  • Start ReceiverSimple.c first
  • Then start SenderSimple.c.

When you do this, the number 23 shows up on the receiving NXT. A few seconds later, 17 shows up. Got that right?

I’ve added some comments to the code to help you understand what each portion of the code does. The key is that you have to keep looking for messages on the receiver part. Think of it as emails you get from a friend. You’ll never know you’ve got a new email unless you check. But like email, messages are stored for your review until you open them. So, there’s no need to check every milisecond of the day. You won’t miss it, it will be in the mail box. The only issue here is that this mailbox will hold only one email, so old messages are overwritten when new ones come in.

If there’s no message, you’ll find 0 as the message. So, if you find something that is not 0, that’s the value that was sent by the other NXT. After storing this value in a variable for further usage, use ClearMessage(); to set the values back to 0 (erase your email inbox). If you do that, you can nicely repeat the procedure and wait for a message to come in.

Run the code and play with it a bit. Then move on to the slightly more useful programs below.

Simple Sender
[sourcecode language=”cpp” wraplines=”false” collapse=”false”]
//SenderSimple.c
task main()
{
wait1Msec(500);

int my_number = 23;

//This sends through one value: 23
sendMessage(my_number);

//Let’s wait a bit and send something else
wait1Msec(3000);
my_number = 17;
sendMessage(my_number);

}
[/sourcecode]

Simple Receiver
[sourcecode language=”cpp” wraplines=”false” collapse=”false”]
//ReceiverSimple.c
task main()
{

int my_message;

while(true)
{
//Read the message to variable
my_message = message;

//Let’s say something got through if the signal is not zero
if(my_message != 0)
{
//Display the value as stored in the variable
nxtDisplayBigTextLine(2,"1: %d",my_message);

//Clear the message, set it to 0
ClearMessage();
}

//Checking for a message every half a second is
//fine for this simple test

wait1Msec(500);

}

}

[/sourcecode]

Sending and receiving three values

These are essentially the same programs as above, except these will allow you to send through 3 values where the above programs would send only one value at the time. Run the programs as per the instructions for the samples above, and observe the results on the NXT screen.

Sender
[sourcecode language=”cpp” wraplines=”false” collapse=”false”]
//Sender.c
task main()
{
wait1Msec(500);

int number_1 = 5;
int number_2 = -1;
int number_3 = 700;

//This sends through three values: 5, -1 and 700
sendMessageWithParm(number_1,number_2,number_3);

//Let’s wait a bit and send something else
wait1Msec(3000);
number_1 = 2;
number_2 = 4;
number_3 = 6;
sendMessageWithParm(number_1,number_2,number_3);

}

[/sourcecode]

Receiver
[sourcecode language=”cpp” wraplines=”false” collapse=”false”]
//Receiver.c
task main()
{
int message_first;
int message_second;
int message_third;

while(true)
{
//Read the messages to variables
message_first = messageParm[0];
message_second = messageParm[1];
message_third = messageParm[2];

//Something got through if the signals are not all zero
if(message_first != 0 || message_second != 0 || message_third != 0)
{
//display the values as stored in the variables
nxtDisplayBigTextLine(2,"1: %d",message_first);
nxtDisplayBigTextLine(4,"2: %d",message_second);
nxtDisplayBigTextLine(6,"3: %d",message_third);

//Clear the messages, set them to 0
ClearMessage();
}
//Checking for a message every half a second is
//fine for this simple test
wait1Msec(500);

}

}
[/sourcecode]

About the author

20 Responses
  1. […] ROBOTC Bluetooth Tutorial ROBOTC no comments Laurens, over at Robot Square, has written an easy to follow Bluetooth tutorial for ROBOTC.  So if you’re keen to do some remote controlling or other message passing but you’re not quite sure how to get started, then head on over: [LINK]. […]

  2. Mike Partain

    I sure wish it was as easy to get messages from a brick to a PC. Any idea what corresponding LCP command(s), if any, might work with these, or do we need to resort to the regular mailbox stuff?

      1. Angelica

        I’ve never run it on a hard floor and don’t recommend it. Actually, in the book I scpiefy that, lacking shock absorbers, it should be used on a rug. In the end, it’s a rather delicate hop—certainly not anything more than experienced by the sumo-bots in competition.

    1. Leonardo

      I downloaded Brams’s soawtfre..Like a old thing I remember on TV.’TRY IT YOU’LL LIKE IT!I tried it out with my 4 year old Granddaughter. Geeee…. She had so much fun! But my LEGO Robot’s battries ran out and she cried.. LOL.. LOL…Anyway.. I LOVED IT TOO!

  3. leona

    I need some help I want to connect BT module with a NXT robot ,in other words NXT-other devices via bluetooth, just i want to know if these codes appropriate with other bluetooth chip like rn42

  4. ashok

    sir,
    i am getting two error mesages in the same statement ClearMessage();

    errors are:

    **Error**:Invalid assignment to a non-variable
    **Error**:Unexpected scanner token-> ‘0’

    1. These programs are tested to work with version 3.0x. I have not tested these programs with 3.5. Could that be the problem? If so, please report that to RobotC support so they can fix it.

      Laurens

  5. carlos

    was testing this code on my robot c 3.5 and still that problem, you have a new code where we can see the connection between the nxt?

    **Error**:Invalid assignment to a non-variable
    **Error**:Unexpected scanner token-> ’0′

  6. John S. Colonias

    Laurens,

    This has nothing to do with BT, but I need help to understand what it means when I am trying to run a ROBOTC
    program with functions and I get an error
    **Error**:Functions must be defined at main scope level

    Do you have an example of its meaning?

    Thanks very much for the help!

    John

    1. Lex Hegt

      Hi John,

      I expect that you solved the problem, since you reported it a month ago.
      In case you did not yet solve it, below you find some RobotC code which shows what goes wrong:

      task main()
      {

      // The following statement is nested in task main(), which
      // leads to the error.
      // It should be placed outside the task main()
      void notAtMainScopeLevel()
      {}

      }

      // This statement is at the main scope level
      void atMainScopeLevel()
      {}

      Hope this helps!
      Lex

  7. Ed Gatzke

    Thanks for the nice example! Have you ever connected from a PC to a set of bricks? What did you use?

    I have have managed to connect to 1 brick using a couple of python methodss but I can’t seem to get more than one connected…

    Thanks!

  8. Tony

    It would be very useful to have a PC based controller using bluetooth, such as “NXT Vehicle Remote” or the standard remote controller, that sends signals to a brick that also interacts with sensors. For example, a PC remote controls a lego car with a proximity sensor to stop it hitting walls.

    How can this be done please?

  9. Henrique Hamisch

    Hi!

    I am trying to use more than two NXT robots with the Bluetooth communications in RobotC. I know that the Bluetooth functions of this software are optimized for only two units, but do you know how I can do that? I have read some texts about the subject, and I think the message in the functions (cCmdMessageRead, cCmdMessageWriteToBluetooth) need to be written in byte arrays, right? Can you help me?

    Thanks,
    Henrique Hamisch

Leave a Reply

Discover LEGO MINDSTORMS EV3

EV3 Discovery

Discover the many features of the EV3 set, and learn to build and program your own robots! Learn more

Start Building Robots

ev3set

Website Maintenance

Robotsquare is currently being updated, which means that it may look a little different (and not very polished) for a while. All the content and pages should still be there, though. It should be back and fully operational soon. Thanks for your patience!