Society of Robots - Robot Forum
Mechanics and Construction => Mechanics and Construction => Topic started by: ROVER on January 16, 2013, 05:22:20 PM
-
I would like to seek help how do I get by using two xbee and its shield and arduino uno
I am using this code to run the dc motor of my remote truck to go forward and backward, I was able to make it move using this code, now I am trying to work it wirelessly using xbee, but I am kinda loss, I thought I could just copy this code again to the other arduino and hooked up the xbee and the shield, any help would be appreciated
const int in1 = 2; // direction pin 1
const int in2 = 4; // direction pin 2
const int ena = 3; // PWM pin to change speed
int pot; // integer for potentiometer
int fspeed; // forward speed
int bspeed; // backward speed
void setup() {
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(ena, OUTPUT);
}
void loop() {
pot = analogRead(A0);
if (pot >= 480 && pot <= 540)
{
stop();
}
if (pot < 500)
{
fspeed = map(pot, 479, 0, 70, 250);
forward(fspeed);
}
if (pot > 520)
{
bspeed = map(pot, 541, 1023, 70, 250);
backward(bspeed);
}
}
void stop()
{
analogWrite(ena, 0);
}
void forward(int fspeed)
{
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
analogWrite(ena, fspeed);
}
void backward(int bspeed)
{
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
analogWrite(ena, bspeed);
}
(http://imageshack.us/a/img41/5827/01162013135.jpg) (http://imageshack.us/photo/my-images/41/01162013135.jpg/)
(http://imageshack.us/a/img688/7294/01162013134.jpg) (http://imageshack.us/photo/my-images/688/01162013134.jpg/)
(http://imageshack.us/a/img24/3216/01162013136.jpg) (http://imageshack.us/photo/my-images/24/01162013136.jpg/)
-
The XBees are serial devices. You need to hook the XBee on each end to a serial port.
I would recommend using SoftwareSerial on the sending Arduino (on some pins other than 0 and 1,) so you don't get interference with programming the board.
You can use the hardware serial port (pins 0 and 1) on the receiving Arduino.
Then, build a simple protocol, and send commands over the serial wire. A simple protocol might be:
0xEE <command byte> <parameter byte>>
So, a command might be "Set forward speed." Call this 0x01. The parameter is the speed value (0 - 255.) Your sending Arduino would then do something like this:
enum {
CMD_FORWARD = 0x1,
CMD_TURN = 0x2
};
SoftwareSerial ss(...);
void sendForward(unsigned char val) {
unsigned char data[3] = { 0xEE, CMD_FORWARD, val };
ss.write(3, data);
}
On the receiving end, you have to receive and decode commands. You should be getting one byte at a time -- you can't assume that a full command will be coming in in a single read request. The algorithm looks something like:
unsigned char buffer[3];
unsigned char bufptr = 0;
void handle_command() {
switch (buffer[1]) {
case CMD_FORWARD:
... value is in buffer[2];
break;
....
}
}
void read_one() {
if (myserialport has data) {
buffer[bufptr] = read one byte;
if ((bufptr == 0 && buffer[0] == 0xEE) || (bufptr > 0)) {
++bufptr; // in a packet
}
if (bufptr == 3) { // packet finished
handle_command();
bufptr = 0;
}
}
}