Go Down

Topic: Someone please help with robotic project. (Read 227 times) previous topic - next topic

gamebo5000

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?

Code: [Select]
/*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;
}

IndianaTux

Quick tips:

1. Format your code properly so it's readable
2. Comments... There are hardly any comments in your code.
3. There is just one huge loop function. Break it up.
4. Commented-out lines are useless, remove them.
5. To debug, add serial output and give the result of non-working scenarios.

PaulS

Quote
5. To debug, add serial output and give the result of non-working scenarios.
Did you read this part?
Quote
When the Arduino has connected to the computer everything works fine.
Adding more Serial.print() statements hardly seems useful, since the problem only happens when Serial.print() is meaningless.

Since the problem only happens when the Arduino is running on batteries. I suspect a power issue. But, without seeing any code, or knowing what kind of batteries are being used, and what they are actually powering, we can only guess.

I don't do guessing games.
The art of getting good answers lies in asking good questions.

wvmarle

Code: [Select]

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);


It doesn't makes sense to do it in this order. Those if statements are effectively overridden by the conversion that follows it.

The formatting is a mess indeed.
Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.

Delta_G

#4
Mar 16, 2018, 02:35 am Last Edit: Mar 16, 2018, 02:37 am by Delta_G
Code: [Select]
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);
  }


What purpose is i serving?  It goes out of scope and loses its value 300ms after you increment it.  How is that remotely useful?  Same for j or k I see later used in a similar way.


Code: [Select]
SoftwareSerial myConnection (0,1); //(10,11)RX, TX

Why are you doing SoftwareSerial on the Hardware serial pins?  You realize that you create a conflict here when you try to use those pins for two purposes at once.

Code: [Select]
Serial.begin(9600); // pin for the reciever
   myConnection.begin(9600);
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

Go Up