Android Controlled Bot
The objective of my project is to create a bot which has the brain of an Arduino and is controlled by Android via Bluetooth. The hardware I am using is as follows:
-RN42 Bluetooth Module by Parallax
-Arduino Uno
-Parallax BOEShield for Arduino robot kit
Bluetooth - Arduino Connections:
GND - GND
VCC - 5V
TX - RX (pin 4, SoftwareSerial)
RX - TX (pin 5, SoftwareSerial)
*tied together CTS and RTS pins on Bluetooth module since they are not in use
I used SoftwareSerial to open the Bluetooth port because of the problems associated with using Arduino RX(0)/TX(1) pins.
This code has been adapted from the tutorial.
With this code, the servos jerk when a signal is sent to it. For instance, when the up button is pushed, the Data LED lights up, but the robot does not move. Pushing the button repeatedly, the servos start to jerk a little, like it is attempting to move.
Is there a problem with the data that is being sent to the Arduino from the Bluetooth Serial?
I also tested to see if the batteries was the problem. I added the maneuver outside of the if statements, the robot began to move as soon as power was applied to it.
-RN42 Bluetooth Module by Parallax
-Arduino Uno
-Parallax BOEShield for Arduino robot kit
Bluetooth - Arduino Connections:
GND - GND
VCC - 5V
TX - RX (pin 4, SoftwareSerial)
RX - TX (pin 5, SoftwareSerial)
*tied together CTS and RTS pins on Bluetooth module since they are not in use
I used SoftwareSerial to open the Bluetooth port because of the problems associated with using Arduino RX(0)/TX(1) pins.
#include <Servo.h> #include <SoftwareSerial.h> Servo servoLeft; // Declare left and right servos Servo servoRight; SoftwareSerial btSerial(4, 5); //(RX, TX) int incomingByte =0; // a variable to read incoming serial data into void setup() { // initialize serial communication: Serial.begin(9600); servoLeft.attach(13); servoRight.attach(12); btSerial.begin(115200); // Default Baud Rate of RN42 Bluetooth module delay(320); // IMPORTANT DELAY! (Minimum ~276ms) btSerial.print("$$$"); // Enter command mode delay(15); // IMPORTANT DELAY! (Minimum ~10ms) btSerial.println("U,9600,N"); // Temporarily Change the baudrate to 9600, no parity btSerial.begin(9600); } void loop() { // see if there's incoming serial data: if (btSerial.available() > 0) { // read the oldest byte in the serial buffer: incomingByte = btSerial.read(); } // if incoming byte is a f, go forward: if (incomingByte == '1') { btSerial.println("forward"); //maneuver(200, 200, 500); servoLeft.writeMicroseconds(1700); servoRight.writeMicroseconds(1300); delay(1000); } // if incoming byte is a D, go backwards: else if (incomingByte == '2') { //Serial.println("backward"); maneuver(-50, -50, 500); delay(1000); } // if incoming byte is a r, turn right: else if (incomingByte == '3') { //Serial.println("right"); maneuver(-50, 50, 500); delay(500); } // if incoming byte is a l, turn left: else if (incomingByte == '4') { //Serial.println("left"); maneuver(50, -50, 500); delay(500); } // if imcoming byte is a S, stop: else if (incomingByte == '5') { //Serial.println("stop"); } } // End loop() void maneuver(int speedLeft, int speedRight, int msTime) { // speedLeft, speedRight ranges: Backward Linear Stop Linear Forward // -200 -100......0......100 200 servoLeft.writeMicroseconds(1500 + speedLeft); // Set left servo speed servoRight.writeMicroseconds(1500 - speedRight); // Set right servo speed if(msTime==-1) // if msTime = -1 { servoLeft.detach(); // Stop servo signals servoRight.detach(); } delay(msTime); // Delay for msTime }
This code has been adapted from the tutorial.
With this code, the servos jerk when a signal is sent to it. For instance, when the up button is pushed, the Data LED lights up, but the robot does not move. Pushing the button repeatedly, the servos start to jerk a little, like it is attempting to move.
Is there a problem with the data that is being sent to the Arduino from the Bluetooth Serial?
I also tested to see if the batteries was the problem. I added the maneuver outside of the if statements, the robot began to move as soon as power was applied to it.
Comments
Assuming my guesses are right about what the code is doing that is most likely your problem. If I am off-base then please elaborate and I will attempt to help you as best as I can.