So far I'm putting together a robot that I can control with my android phone. Thus far I've been able to somehow manage to write a code and I can control the robot wirelessly.
I am coming to you all to work out some kinks in the programming to make the operation very fluid.
In the code below I am only controlling the arduino's ability to stop, move forward or turn right at the push of a button or at the absence of a command.
#include <Servo.h> // Servo library used
Servo servoLeft; // Define left servo
Servo servoRight; // Define right servo
int val = 0; // variable to store the servo position
#include <SoftwareSerial.h> //Software Serial Port
#define RxD 6
#define TxD 7
#define DEBUG_ENABLED 1
SoftwareSerial blueToothSerial(RxD,TxD);
void setup()
{
Serial.begin(9600);
pinMode(RxD, INPUT);
pinMode(TxD, OUTPUT);
servoLeft.attach(9); // Set left servo to digital pin 9
servoRight.attach(10); // Set right servo to digital pin 10
setupBlueToothConnection();
}
void loop()
{
char recvChar;
while(1){
if(blueToothSerial.available()){//check if there's any data sent from the remote bluetooth shield
recvChar = blueToothSerial.read();
Serial.print(recvChar);
recvChar = Serial.read();
{val = Serial.read();}{ // read value coming in from phone
if( val == 'Forward'); // If Forward button is pressed
forward();} // follow command sequence to move forward
} else
{stopRobot(); // do not move if no buttons are pressed
}
}
{
char recvChar;
while(24){
if(blueToothSerial.available()){//check if there's any data sent from the remote bluetooth shield
recvChar = blueToothSerial.read();
Serial.print(recvChar);
recvChar = Serial.read();
{val = Serial.read();}{ // read value coming in from phone
if( val == 'Right'); // If right button is pressed
{
turnRight(); // follow command sequence to turn right
}
}
} else
{stopRobot(); // do not move if no buttons are pressed
}
}
}}
void setupBlueToothConnection()
{
blueToothSerial.begin(38400); //Set BluetoothBee BaudRate to default baud rate 38400
blueToothSerial.print("\r\n+STWMOD=0\r\n"); //set the bluetooth work in slave mode
blueToothSerial.print("\r\n+STNA=SeeedBTSlave\r\n"); //set the bluetooth name as "SeeedBTSlave"
blueToothSerial.print("\r\n+STOAUT=1\r\n"); // Permit Paired device to connect me
blueToothSerial.print("\r\n+STAUTO=0\r\n"); // Auto-connection should be forbidden here
delay(2000); // This delay is required.
blueToothSerial.print("\r\n+INQ=1\r\n"); //make the slave bluetooth inquirable
Serial.println("The slave bluetooth is inquirable!");
delay(2000); // This delay is required.
blueToothSerial.flush();
}
// Motion routines for forward, reverse, turns, and stop
void forward() {
servoLeft.write(0);
servoRight.write(180);
}
void reverse() {
servoLeft.write(180);
servoRight.write(0);
}
void turnRight() {
servoLeft.write(180);
servoRight.write(180);
}
void turnLeft() {
servoLeft.write(0);
servoRight.write(0);
}
void stopRobot() {
servoLeft.write(90);
servoRight.write(90);
}
Here is what I WANT it to do.
- Move forward at the push of the onscreen "Forward" button on my phone
- Turn right at the push of the onscreen "Right" button on my phone
- Turn left at the push of the onscreen "Left" button on my phone
- Move Reverse at the push of the onscreen "Reverse button on my phone
- Stop at the push of the onscreen "Stop" button on my phone
- Do nothing until a command is received from phone
Here is what it is doing right now
- Turns in place infinitely at a slow speed until command is received
- Moves forward for about 2 seconds then begins turning in place again (when command is received)
- Robot only moves forward no matter what command is received
Things I've tried so far
+Using different PWM pins 3,5,6, and 11
+adding delays in random locations
- Use direct coding instead of void referencing
- Changing the values of the servos in the parenthesis
Hardware
Arduino Duemilanove AtMega 328
Bluetooth shield from trossen robotics http://www.seeedstudio.com/wiki/index.php?title=Bluetooth_Shield
Servos from parallax kit (they just say "continuous rotation" but the specs are somewhere on their site)
Android Phone (T-mobile g2)
Application on phone: Bluetooth SPP <- Great app by the way!