Facebook Twitter Gplus YouTube RSS
Home » Tutorials » NXT Programming » RobotC » Tutorial: RobotC Bluetooth Examples
formats

Tutorial: RobotC Bluetooth Examples

Published on March 21, 2012 by in RobotC

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

//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);

}

Simple Receiver

//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);

	}

}

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

//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);

}

Receiver

//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);

	}

}
 
 Share on Facebook Share on Twitter Share on Reddit Share on LinkedIn
18 Comments  comments 

18 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?

    • Hi Mike, I’ve successfully used this resource to communicate with the NXT brick. It works quite well – except it works better with NXT-G than with RobotC in terms of messaging.

      • 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.

    • 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. [...] is one of those harder behaviors to program. Luckily,  Laurens over at Robot Square wrote up a nice tutorial on how to use Bluetooth communication with [...]

  4. 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

    • No, you’ll need different programs — if what you want to do is doable at all. There are some specific smartphone apps for android and NXT with BT.

  5. [...] es una de las conductas más difíciles de programar. Por suerte,  Laurens más en Robot Square escribió un buen tutorial sobre cómo utilizar la comunicación Bluetooth con [...]

  6. 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′

    • 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

  7. 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′

  8. 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

    • I’m not sure. The best place to ask would be the RobotC forums: http://www.robotc.net/forums/

    • 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

  9. 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!

  10. 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?

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>