Results 1 to 9 of 9

Thread: Communication between two arduino boards via Xbee

  1. Communication between two arduino boards via Xbee

    Hello all,
    Thank you for viewing first of all,
    and if you reply, even more thanks.


    I have been "working" on a project for a while now, and have some success with it. Unfortunately, I really only know how to build, which means that this project is mechanically sound. I have some knowledge of the Arduino programming language, but not enough to actually do anything with. Which is why I made this post. I was wondering if anyone had extensive knowledge on arduino-to-arduino communication through xbee, and could put it on an understandable level. Pretty much what I am looking for is an example where you push a button on one arduino, the other arduino completes an action.

    As for the project, it is supposed to use 2 series 2 Xbee's for communication between the boards, if thats any help. Any other details on the project will be posted on an alternate discussion board.

    Again, thank you for viewing.

  2. #2

    Re: Communication between two arduino boards via Xbee

    If you look at the Serial class, if you hook up the Xbees correctly (note the 3.3V requirement!) then what you write on one end with Serial.write() shows up on the other end as Serial.read()
    If you just define that one Arduino sends a "1" when pressing the button, then the other end just needs to wait for a "1" to come in on Serial.read().

    Note that the Arduino Uno only has one serial port, and uses that serial port for programming the chip, which will conflict with the Xbee usage. Thus, I'd recommend using an Arduino Leonardo, where the serial port is free (because it programs over the built-in USB.)
    Another option is to use Teensy 3 boards instead -- they have multiple serial ports, and none of them conflict with programming.

    Assuming you have configured your Xbees correctly, the code for the sender is very simple. Something like:

    Code:
    uint32_t lastButtonTime;
    
    void setup() {
      Serial.begin(38400); // or whatever speed you use
      lastButtonTime = millis();
    }
    
    void loop() {
      // don't check too often, to avoid flooding the serial connection
      if (millis() - lastButtonTime > 100) {
        if (buttonIsPressed()) {
          Serial.write("1");
        }
        lastButtonTime = millis();
      }
    
      // other code here
    }
    Similarly, the receiver is equally simple:

    Code:
    void setup() {
      Serial.begin(38400);
    }
    
    void loop() {
      if (Serial.available()) {
        int ch = Serial.read();
        if (ch == '1') {
          if (!alreadyDoingTheThing()) {
            startDoingTheThing();
          }
        }
      }
    
      // other code here
    }

    You of course provide your own code for things like buttonIsPressed() and alreadyDoingTheThing() and such :-)

  3. Re: Communication between two arduino boards via Xbee

    Sorry for not responding asap, some connection problems, as well as teenage laziness is to blame.

    Anyways, I have two xbee shields for the arduino megas that I plan to use on both ends, would it be possible to use those instead of the serial port. As far as I know, the shields only use the tx and RX pins, which shouldn't conflict with the serial port.

    On the coding bit, I can understand a bit of it, but could you please clarify the functions you used? The end goal is to use a button to move some servos around.

    Thank you in advance if you reply, and plz forgive the noobishness.

  4. #4

    Re: Communication between two arduino boards via Xbee

    Each serial port has a "TX" and "RX" pin. "TX" on one device (an Arduino) connects to "RX" on the other device (such as an Xbee.)
    The Mega has four hardware serial ports, and as long as the Xbee uses a serial port that is different than the one used for programming, you will be fine. (Assuming you're using an Arduino Mega board!)

  5. Re: Communication between two arduino boards via Xbee

    So that covers the hardware part.

    In all honesty I'm not really concerned about figuring out the hardware, I am primarily concerned with the software. Based on the sketch provided above I can understand most of it, but the function that I don't understand is the "int ch" function.

    Another question is that in this section after the "if" statement and after the parentheses there are brackets, I assume that those brackets conclude an "if-then" statement?

    void loop() {
    if (Serial.available()) {
    int ch = Serial.read();
    if (ch == '1') {
    if (!alreadyDoingTheThing()) {
    startDoingTheThing();
    }
    }
    }
    Thalvus the ???
    the JgPz 38(t) Hetzer, my baby, my love, and my weapon of mass destruction

  6. #6

    Re: Communication between two arduino boards via Xbee

    An if statement is followed by one statement to execute if the if() is true, and then optionally followed by the "else" keyword and another statement to execute if the statement is not true.

    Code:
    if (theThing)
      Serial.print("The Thing!\n");
    If you want to do more than one thing inside the if(), you have to create a statement that does more than one thing in sequence. That's what "blocks" are -- open brace, any number of statements, close brace -- looks to the if() statement as a single statement.

    Code:
    if (theThing) {
      Serial.print("Thing 1\n");
      Serial.print("Thing 2\n");
    }
    Because the C/C++ compiler doesn't look at the whitespace level (putting statements further to the right to show where they behave,) it's generally considered good practice to always use the braces after if() (and similar control flow like while() and for()) even if you only have one statement inside then. This avoids bugs when you come back and extend the code.

    For more information, see your favorite Arduino C/C++ tutorial web page :-)

  7. #7
    Join Date
    Dec 2012
    Location
    Los Angeles, CA
    Posts
    858
    Images
    25
    Rep Power
    88

    Re: Communication between two arduino boards via Xbee

    I second jwatte's statement! For example isn't this easier to read and understand?

    Code:
    void loop()
    {
        if( Serial.available() )
        {
            int ch = Serial.read();
            if( ch == '1' )
            {
                if( !alreadyDoingTheThing() )
                {
                    startDoingTheThing();
                }
            }
        }
    }

  8. #8

    Re: Communication between two arduino boards via Xbee

    Also agree, but sometimes do get lazy Also note, code is a lot easier to read in form messages, when it is contained in code tags.

    Now as to the first part of the question:
    Code:
    int ch = Serial.read();
    The Serial class is built into the Arduino code base. You can find lots of information in their reference, including
    its methods which include read(). The manual page for it is at: http://www.arduino.cc/en/Serial/Read

    Basically read take no parameters and returns an integer value, which in your code is saved into a new variable called
    ch.

    If you have not received anything on the serial port, the value returned will be -1. So in the code example you are
    looking to see if you received a byte that is equal to an ASCII 1.

    Kurt

  9. Re: Communication between two arduino boards via Xbee

    Sorry I couldn't reply sooner, I had school work to take care of. Thank you all so much for all the information, it will definitely help with my future endeavors in robotics. I tip my hat to all of you.
    Last edited by Thalvus; 05-20-2016 at 09:09 PM.
    Thalvus the ???
    the JgPz 38(t) Hetzer, my baby, my love, and my weapon of mass destruction

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Question(s) Robust communication between Python and Xbee?
    By ddugi79 in forum Arbotix, Microcontrollers, Arduino
    Replies: 11
    Last Post: 02-11-2015, 07:22 PM
  2. Serial communication between Arduino and Arbotix
    By dburongarcia in forum Arbotix, Microcontrollers, Arduino
    Replies: 17
    Last Post: 03-09-2014, 12:44 PM
  3. Question(s) My Arbotix-M is not showing up in my arduino IDE boards list
    By fxmech in forum Arbotix, Microcontrollers, Arduino
    Replies: 4
    Last Post: 10-29-2013, 02:23 PM
  4. Designing a shield for Arduino Rev 3 boards that also work with non Rev 3 boards...
    By KurtEck in forum Arbotix, Microcontrollers, Arduino
    Replies: 4
    Last Post: 10-23-2012, 08:06 PM
  5. Question(s) Arduino- xbee -xbee-arduino - stepper motors
    By Icemonkey in forum Arbotix, Microcontrollers, Arduino
    Replies: 7
    Last Post: 06-04-2012, 12:35 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •