xbee code for rc boat help please!!

I’m building a project for school. I’m using an old rc boat that i want to control via smart phone. I’m using the android bluetooth rc controller app. So the app is going to my osepp bluetooth board (Arduino BT) which has built in bluetooth. On that board i have a xbee pro shield and a xbee s2. On the boat i have a osepp mega 2560 r3 plus which has adafruit motor shield v2.3, xbee pro shield, and xbee s2. Here is my script so far.

#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include “utility/Adafruit_MS_PWMServoDriver.h”
#include <SoftwareSerial.h>

Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_DCMotor *LeftMotor = AFMS.getMotor(1);
Adafruit_DCMotor *RightMotor = AFMS.getMotor(3);

int state;

void setup() {
Serial.begin(115200); //connect to bt

AFMS.begin();

LeftMotor->setSpeed(150);
LeftMotor->run(FORWARD);
LeftMotor->run(BACKWARD);
LeftMotor->run(RELEASE);

RightMotor->setSpeed(150);
RightMotor->run(FORWARD);
RightMotor->run(BACKWARD);
RightMotor->run(RELEASE);
}

void loop() {
if(Serial.available() > 0){
state = Serial.read();
}

if (state == ‘F’){ // Move forward
LeftMotor->run(FORWARD);
RightMotor->run(FORWARD);
}

else if (state == ‘B’){ // Move backward
LeftMotor->run(BACKWARD);
RightMotor->run(BACKWARD);
}

else if (state == ‘S’){ // Stop
LeftMotor->run(RELEASE);
RightMotor->run(RELEASE);
}

else if (state == ‘L’){ // Move left
LeftMotor->run(BACKWARD);
RightMotor->run(FORWARD);
}

else if (state == ‘R’){ // Move right
LeftMotor->run(FORWARD);
RightMotor->run(BACKWARD);
}

else if (state == ‘G’){ // Move forward left
LeftMotor->run(RELEASE);
RightMotor->run(FORWARD);
}

else if (state == ‘I’){ // Move forward right
LeftMotor->run(FORWARD);
RightMotor->run(RELEASE);
}

else if (state == ‘H’){ // Move back left
LeftMotor->run(RELEASE);
RightMotor->run(BACKWARD);
}

else if (state == ‘J’){ // Move back right
LeftMotor->run(BACKWARD);
RightMotor->run(RELEASE);
}

}

Questions:

  1. Does this look right so far?

  2. What is the best way to send this with the xbees? AT or API

  3. Do i need some kind of xbee library to do this?

I’ll just start with these for now. This is my first time really using arduinos and xbees so i’m new to all of this. Thanks for reading and any input is very much appreciated!!!

Here is my script so far.

For which device? The one on the boat or the one on the shore?

  1. Does this look right so far?

No. The code was not posted correctly.

  1. What is the best way to send this with the xbees? AT or API

To send what? API mode is most useful when the XBee has stuff connected to it. When the XBee is just acting as a radio, and only talking to one other device, use AT mode.

  1. Do i need some kind of xbee library to do this?

To do what? You have not defined what you need to do.

First of all thank you very much for replying!!

This script is for the one on the shore. I don't have anything connected to the xbee on the boat, I'm just sending the code to the other arduino on the boat. So i believe i'm going to use AP mode. So i think for this code (smart phone to shore arduino) i need to change state = Serial.print so it will send the state through the xbee to the boat arduino? Then for the arduino on the boat i will to have state = Serial.read? Can you tell me what else would be wrong with this script? Do i need to include something to connect my phone to the bluetooth on the board because it says it connects through the serial at 115200?

Page for bluetooth arduino
http://arduino.cc/en/Main/ArduinoBoardBluetooth

Characters for app
https://sites.google.com/site/bluetoothrccar/home/4PuttingItAllTogetherAndTheAndroidApplication

Thanks again for your help!! It is much appreciated!!

So i think for this code (smart phone to shore arduino) i need to change state = Serial.print so it will send the state through the xbee to the boat arduino?

I don't understand that statement/question. Serial.print() returns the number of bytes written to the serial port. In general, that is a useless value. It certainly is not a state.

Then for the arduino on the boat i will to have state = Serial.read?

You will need to use Serial.available() and Serial.read(). Pay some attention to this thread:

Can you tell me what else would be wrong with this script?

The code does something. You didn't explain what it actually does.
You expect the code to do something. You didn't explain exactly what you expect.
Without knowing where point A is (what actually happens) and where point B is (what you expect), a map to get from point A to point B is not possible.

Sorry i will try to explain it a little better. Here are the characters for the app. It's an app that you can get from the google play store and it's called Arduino Bluetooth RC Car.

Forward---------------------F
Back-------------------------B
Left---------------------------L
Right-------------------------R
Forward Left--------------G
Forward Right------------I
Back Left------------------H
Back Right----------------J
Stop-------------------------S
Front Lights On---------W
Front Lights Off---------w (lower case)
Back Lights On---------U
Back Lights Off---------u (lower case)
Speed 0-------------------0
Speed 10-----------------1
Speed 20-----------------2
Speed 30-----------------3
Speed 40-----------------4
Speed 50-----------------5
Speed 60-----------------6
Speed 70-----------------7
Speed 80-----------------8
Speed 90-----------------9
Speed 100---------------q
Everything OFF--------D

So what i'm trying to do is use this app on my phone to control my boat. So when push the forward button on the app, the app will send "F" from my phone to the bluetooth that is built into the arduino that is on the shore. Then the arduino will send the "F" to the xbee that is attached to it. This xbee will send the "F" to the xbee that is on the boat. This xbee that is on the boat will send the "F" to the arduino that it is attached to. Then the arduino will send the "F" to the motor shield that then will move both motors to make the boat go forward. Hope this helps explain what i am trying to do better. I got the state part from a code that i saw here. Arduino Bluetooth RC Car (Android Controlled) - Ardumotive Arduino Greek Playground

I will take a look at the link you gave me. Thanks again for your input and time.

Then the arduino will send the "F" to the motor shield that then will move both motors to make the boat go forward.

What kind of motor shield understands "F"?

Nothing in what you want to do is even remotely difficult, if you understand that the bluetooth and XBee devices are just replacements for wires and that it is the Arduino's job to make sense of the data, not that of things attached to the Arduino.

I know the motor shield doesn't understand the "F", i was just trying to explain the steps better so you could understand what i was trying to do. When the arduino on the boat receives the data that corresponds with "F", the arduino will tell the motor shield to run both motors in a forward position.

How would you recommend to do this? The char function?

How would you recommend to do this?

Presumably, the phone already knows how to send data.

Presumably, reading the data that arrives at the bluetooth device is something you know how to do. Serial.available() and Serial.read(), or their SoftwareSerial instance equivalents.

Presumably, sending the data to the XBee is something you know how to do. Serial.print() or its SoftwareSerial equivalent.

Presumably, reading the data that arrives at the XBee is something you know how to do. Serial.available() and Serial.read(), or their SoftwareSerial instance equivalents.

I don't see what the issue is.