PDA

View Full Version : Communication between two arduino boards via Xbee



Thalvus
03-21-2016, 01:28 PM
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.

jwatte
03-21-2016, 01:48 PM
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:



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:



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 :-)

Thalvus
04-02-2016, 05:09 PM
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.

jwatte
04-02-2016, 11:43 PM
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!)

Thalvus
04-11-2016, 09:49 AM
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();
}
}
}

jwatte
04-11-2016, 12:36 PM
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.



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.



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 :-)

KevinO
04-11-2016, 01:11 PM
I second jwatte's statement! For example isn't this easier to read and understand?



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

KurtEck
04-11-2016, 01:32 PM
Also agree, but sometimes do get lazy :o 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:

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

Thalvus
05-20-2016, 09:04 PM
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.