My robot is to suppose to be able to drive forward unless it detects an object, and the turn away from the object. When the Arduino has connected to the computer everything works fine. When the left sensor detects an object the robot turns right when the right sensor detects an object it turns let. Then when the sensor no longer detects an object the robot moves forward. the problem I am having is when the robot is turning left and needs to then turn right it disconnects and just turns in a circle. This is also true for the opposite. Now when the robot is not connected to the computer and running off batteries it just turns in a circle, until I pick it up and put it down then it goes straight, detects an object, and goes back to turning in circles. Another issue is that when the robot first comes on it tries to turn left then shifts to going straight. Can someone please examine and and tell me what is wrong?
/*This code is used to control a 4-wheel drive robot for autonomous movement */
#include<SoftwareSerial.h> // Import the serial Library
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_MS_PWMServoDriver.h"
// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
// Select which 'port' M1, M2, M3 or M4. In this case, M1
Adafruit_DCMotor *FL= AFMS.getMotor(1); //Front left motor
//Adafruit_DCMotor *BL= AFMS.getMotor(2); //Back left motor
//Adafruit_DCMotor *BR= AFMS.getMotor(3); //Back right motor
Adafruit_DCMotor *FR= AFMS.getMotor(4); //Front right motor
SoftwareSerial myConnection (0,1); //(10,11)RX, TX
// sensor pins
int left_trig = 3;
int left_echo = 4;
int mid_trig = 5;
int mid_echo = 6;
int right_trig = 8;
int right_echo = 9;
int backleft_trig = 10;
int backleft_echo = 11;
int backright_trig = 12;
int backright_echo = 13;
//char data = Serial1.write();
void setup() {
Serial.begin(9600); // pin for the reciever
myConnection.begin(9600);
//Serial1.begin(9600); //pin for the transmitter, what the robot will send out
AFMS.begin(); // create with the default frequency 1.6KHz
//This establishes the sensors as inputs and outputs
pinMode(left_trig,OUTPUT);
pinMode(left_echo,INPUT);
pinMode (mid_trig,OUTPUT);
pinMode(mid_echo,INPUT);
pinMode (right_trig,OUTPUT);
pinMode(right_echo,INPUT);
FL->setSpeed(150);
FL->run(FORWARD);
FL->run(RELEASE);
FR->setSpeed(150);
FR->run(BACKWARD);
FR->run(RELEASE);
}
void loop() {
long duration1, duration2, duration3, inches1, inches2, inches3;
//This portion of the code tells us how long it takes for the sensors To get a response from a nearby object
//This establishes the sensors as inputs and outputs
//pinMode(left_trig,OUTPUT);
digitalWrite(left_trig, LOW);
delayMicroseconds(2);
digitalWrite(left_trig, HIGH);
delayMicroseconds(10);
duration1 = pulseIn(left_echo,HIGH);
// pinMode (mid_trig,OUTPUT);
digitalWrite(mid_trig, LOW);
delayMicroseconds(2);
digitalWrite(mid_trig, HIGH);
delayMicroseconds(10);
duration2 = pulseIn(mid_echo, HIGH);
// pinMode (right_trig,OUTPUT);
digitalWrite(right_trig, LOW);
delayMicroseconds(2);
digitalWrite(right_trig, HIGH);
delayMicroseconds(10);
duration3 = pulseIn(right_echo, HIGH);
// FL->setSpeed(150);
// FL->run(FORWARD);
// FR->setSpeed(150);
// FR->run(BACKWARD);
if (inches1>100)
inches1=100;
if (inches2>100)
inches2=100;
if (inches3>100)
inches3=100;
// convert the time into inches
inches1 = microsecondsToInches(duration1);
inches2 = microsecondsToInches(duration2);
inches3 = microsecondsToInches(duration3);
Serial.print(inches1);
Serial.print("in,\t");
Serial.print(inches2);
Serial.print("in,\t");
Serial.print(inches3);
Serial.print("in");
Serial.println();
delay(300);
if(inches3<=8)
{
int i=1;
FL->setSpeed(150);
FL->run(FORWARD);
FR->setSpeed(150);
FR->run(FORWARD);
Serial.print(i);
Serial.print(" Left");
Serial.print("\t");
i++;
delay(300);
}
else if(inches3<=8 && inches1<=8)
{
int i=1;
FL->setSpeed(150);
FL->run(FORWARD);
FR->setSpeed(150);
FR->run(FORWARD);
Serial.print(i);
Serial.print(" other");
Serial.print("\t");
i++;
delay(300);
}
else if(inches1<=8)
{
int j =1;
FL->setSpeed(150);
FL->run(BACKWARD);
FR->setSpeed(150);
FR->run(BACKWARD);
Serial.print(j);
Serial.print(" Right"+j);
Serial.print("\t");
j++;
delay(300);
}
else
{
int k =1;
FL->setSpeed(150);
FL->run(FORWARD);
FR->setSpeed(150);
FR->run(BACKWARD);
Serial.print(k);
Serial.print(" Forward "+k);
Serial.print("\t");
k++;
}
delay(300);
}
long microsecondsToInches(long microseconds) {
// According to Parallax's datasheet for the PING))), there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second). This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
return microseconds / 74 / 2;
}