Xbee remote control

Hi Guys

I am designing a remote control system and need a little help with incorporating ZigBee for the wireless communication between both Arduino Leonardo’s. One is the remote control and the other is the receiver. Can someone please help me to include the ZigBee code into the code i already have.

Thanks

Below is the code i am currently using

#include <SoftwareSerial.h>

const int buttonPin1 = 7;
const int buttonPin2 = 6;
const int buttonPin3 = 5;
const int buttonPin4 = 4;
const int buttonPin5 = 8;
const int buttonPin6 = 9;
const int ledPin = 13;

SoftwareSerial zigBee(3,2);
int buttonState = 0;

void setup() {

pinMode(buttonPin1, INPUT_PULLUP);
pinMode(buttonPin2, INPUT_PULLUP);
pinMode(buttonPin3, INPUT_PULLUP);
pinMode(buttonPin4, INPUT_PULLUP);
pinMode(buttonPin5, INPUT_PULLUP);
pinMode(buttonPin6, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
digitalWrite (ledPin, HIGH);
Serial.begin(9600);
zigBee.begin(9600); //begin software serial between XBee and Arduino Leonardo
Keyboard.begin();
}

void loop() {

if (digitalRead(buttonPin1) == LOW) {
// turn LED on:
digitalWrite(ledPin, !digitalRead(ledPin));
}

if (digitalRead(buttonPin2) == LOW) {
// turn LED on:
digitalWrite(ledPin, !digitalRead(ledPin));
}

if (digitalRead(buttonPin3) == LOW) {
// turn LED on:
digitalWrite(ledPin, !digitalRead(ledPin));
}

if (digitalRead(buttonPin4) == LOW) {
// turn LED on:
digitalWrite(ledPin, !digitalRead(ledPin));
}

if (digitalRead(buttonPin5) == LOW) {
// turn LED on:
digitalWrite(ledPin, !digitalRead(ledPin));
}

if (digitalRead(buttonPin6) == LOW) {
// turn LED on:
digitalWrite(ledPin, !digitalRead(ledPin));
}

if (!digitalRead(buttonPin1)) // if first button is pressed, 32 is “space bar” in ascii table and will play the media
Keyboard.write(32);

if (!digitalRead(buttonPin2)) // if second button is pressed, 115 is the letter “s” in ascii table and will stop the media
Keyboard.write(115);

if (!digitalRead(buttonPin3)) // if third button is pressed, 110 is the letter “n” in the ascii table and will play next song/video
Keyboard.write(110);

if (!digitalRead(buttonPin4)) // if fourth button is pressed, 112 is the letter “p” in the ascii table and will play the previous song/video
Keyboard.write(112);

if (!digitalRead(buttonPin5)) // if fifth button is pressed, 62 is the “>” button in the ascii table and will increase volume
Keyboard.write(62);

if (!digitalRead(buttonPin6)) // if sixth button is pressed, 60 is the “<” button in the ascii table and will decreases volume
Keyboard.write(60);

}

The Leonardo has two hardware serial ports - one using the TX and RX pins and the other using the USB chip. Why do you need SoftwareSerial, too?

Which XBees are you using? How have you configured them?

The code does something. What does it actually do? What do you want it to do instead? Is that code on the sender or the receiver? Where is the other code?

I have been using Arduino Duemilanove's at school and have been told to add the softwareserial. Is it not needed for the Leonardos? Using Xbee series 2 on shields and my tutor has configured them, i did a hello world sketch test on the Duemilanove's and communication works between them both, tried doing the same on the Leonardos and not working. The code is pressing keys so is acting like a keyboard, they are hotkeys to carry out functions. This is the only code i got and i guess will be the same for the sender and receiver? Please can you provide information on this as i am new to the Arduino.

Thanks

s it not needed for the Leonardos?

That depends on which pins you are connecting the XBee to. On the Leonardo, pins 0 and 1 are connected to Serial1, not Serial. So, you can use Serial1 to talk to the XBee and Serial to talk to the PC.

Using Xbee series 2 on shields and my tutor has configured them

That doesn't tell us squat.

This is the only code i got and i guess will be the same for the sender and receiver?

I can't imagine why you think that.

I have been using Duemilaniove’s and the example below is for the Hello world example between teo arduinos in xbee. How will it be different on Leonardos?

The example i have been using for the Duemilanove Atmega 328 for the transmitter is shown below and works fine.

#include <SoftwareSerial.h>

void setup () {
Serial.begin(9600);
zigBee.begin(9600);
}

void loop () {
zigBee.println (“Hello World”);
delay (1000)
}

And for the receiver the code is as below:

#include <SoftwareSerial.h>

SoftwareSerial zigBee (3,2);
char barry;

void setup() {
Serial.begin(9600);
zigBee.begin(9600);
}

void loop () {
if (zigBee.available()) {
barry = zigBee.read();
Serial.print(barry);
}
}

Please can someone help me to incorporate this code on two Leonardos as i cannot get it to work

Thanks

The Leonardo has two hardware serial ports - Serial that uses the USB-to-serial converter and Serial1 that uses pins 0 and 1. Why are you using SoftwareSerial?

I'm sure that somewhere there is a list of pins that support pin change interrupts, which SoftwareSerial needs, but I can't find it. On the Duemilanove, all pins support pin change interrupts. On the Micro, which uses the same chip as the Leonardo, not all pins do. On the Mega, not all pins do. The key is which ones don't.

If you MUST use SoftwareSerial, you MUST determine which pins support pin change interrupts, and use only those pins.

I am new to all of this as you can probably tell :). I was just trying that sketch from what i used to see if it would work with the Leonardo's. Is Xbee the best way to go round this or bluetooth? Basically i am making a remote control that will control VLC media player. My initial design involved me using two arduinos with Xbee as one as the transmitter and the other as the receiver. If my laptop already has bluetooth built in, can i use just one arduino and connect to it and control it?