Go Down

Topic: obstacle avoiding robot, help with code (Read 54456 times) previous topic - next topic

Shehjar

#include <Ultrasonic.h>
#include <Servo.h>

Ultrasonic ultrasonic(12,13);

const int RForward = 0;
const int RBackward = 180;
const int LForward = 180;
const int LBackward = 0;
const int RNeutral = 91;
const int LNeutral = 92; //constants for motor speed

const int TrigPin = 12;
const int EchoPin = 13;  //Echo pin

const int dangerThresh = 10; //threshold for obstacles (in cm)
int leftDistance = 0;
int rightDistance = 0; //distances on either side

Servo HeadServo;
Servo LeftServo;
Servo RightServo; //declare servos

long duration; //time it takes to recieve ECHO))) signal

void setup()
{
pinMode(TrigPin, OUTPUT);
pinMode(EchoPin, INPUT);
RightServo.attach(11);
LeftServo.attach(10);
HeadServo.attach(6); //attach servo to proper pins
HeadServo.write(90); //centre head servo
}

void loop()
{

int distanceFwd = (ultrasonic.Ranging(CM));

Serial.begin( 9600 );
Serial.print( ultrasonic.Ranging(CM) );
Serial.println( "cm" );
delay(500);

if (distanceFwd>dangerThresh) //if path is clear

{
LeftServo.write(LForward);
RightServo.write(RForward); //move forward
}
else //if path is blocked
{
LeftServo.write(LNeutral);
RightServo.write(RNeutral);
HeadServo.write(0);
delay(500);
rightDistance = ultrasonic.Ranging(CM); //scan to the right
delay(500);
HeadServo.write(180);
delay(700);
leftDistance = ultrasonic.Ranging(CM); //scan to the left
delay(500);
HeadServo.write(90); //return to center
delay(100);
compareDistance();
}

}

void compareDistance()
{
if (leftDistance>rightDistance) //if left is less obstructed
{
LeftServo.write(LBackward);
RightServo.write(RForward); //turn left
delay(2000);
}
else if (rightDistance>leftDistance) //if right is less obstructed
{
LeftServo.write(LForward);
RightServo.write(RBackward); //turn right
delay(2000);
}
else //if they are equally obstructed
{
LeftServo.write(LForward);
RightServo.write(RBackward); //turn 180 degrees
delay(2000);
}
}

long ping()
{
// Send out PING))) signal pulse
pinMode(TrigPin, OUTPUT);
digitalWrite(TrigPin, LOW);
delayMicroseconds(2);
digitalWrite(TrigPin, HIGH);
delayMicroseconds(5);
digitalWrite(TrigPin, LOW);

//Get duration it takes to receive echo
pinMode(TrigPin, INPUT);
duration = pulseIn(TrigPin, HIGH);

//Convert duration into distance
return duration / 29 / 2;
}

AWOL

Is there a question?

Will we be seeing your code inside code tags?
"Pete, it's a fool (who) looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

PaulS

What is the ping() function for? You never call it.

compareDistance() is a dumb name for a function that does a whole lot more than that.
The art of getting good answers lies in asking good questions.

Sarazarvisphineix

Am making a obstical avoiding car...my whole circuit is ok.. but I am not been able to upload the codes....each time l try it doesn't compile and without compiling l can't upload the codes .. it shows that l have some data (newping)missing ...plz anybody help me with providing a valid code..


The code l used

  // Arduino Obstacle Avoiding Robot             
 // Code adapted from http://www.educ8s.tv                                         
 // First Include the NewPing and Servo Libraries


#include <NewPing.h>http:max
#include <Servo.h>

#define TRIG_PIN A4
#define ECHO_PIN A5
#define MAX_DISTANCE 200
NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE);
Servo myservo;   

boolean goesForward=false;
int distance = 100;
int speedSet = 0;

const int motorPin1  = 11; 
const int motorPin2  = 10; 
//Motor B
const int motorPin3  = 6;
const int motorPin4  = 5; 

void setup() {

  myservo.attach(9); 
  myservo.write(115);
  delay(2000);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
}

void loop() {
 int distanceR = 0;
 int distanceL =  0;
 delay(40);
 
 if(distance<=20)
 {
  moveStop();
  delay(100);
  moveBackward();
  delay(300);
  moveStop();
  delay(200);
  distanceR = lookRight();
  delay(200);
  distanceL = lookLeft();
  delay(200);

  if(distanceR>=distanceL)
  {
    turnRight();
    moveStop();
  }else
  {
    turnLeft();
    moveStop();
  }
 }else
 {
  moveForward();
 }
 distance = readPing();
}

int lookRight()
{
    myservo.write(50);
    delay(500);
    int distance = readPing();
    delay(100);
    myservo.write(115);
    return distance;
}

int lookLeft()
{
    myservo.write(170);
    delay(500);
    int distance = readPing();
    delay(100);
    myservo.write(115);
    return distance;
    delay(100);
}

int readPing() {
  delay(70);
  int cm = sonar.ping_cm();
  if(cm==0)
  {
    cm = 250;
  }
  return cm;
}

void moveStop() {
  analogWrite(motorPin1, 0);
    analogWrite(motorPin2, 0);
    analogWrite(motorPin3, 0);
    analogWrite(motorPin4, 0);
  }
 
void moveForward() {


    analogWrite(motorPin1, 180);
    analogWrite(motorPin2, 0);
    analogWrite(motorPin3, 180);
    analogWrite(motorPin4, 0); 
 
}

void moveBackward() {
   
    analogWrite(motorPin1, 0);
    analogWrite(motorPin2, 180);
    analogWrite(motorPin3, 0);
    analogWrite(motorPin4, 180);   
 


void turnRight() {
 analogWrite(motorPin1, 180);
    analogWrite(motorPin2, 0);
  analogWrite(motorPin3, 0);
    analogWrite(motorPin4, 180);   
  delay(300);
 moveForward();     
 
}
 
void turnLeft() {
  analogWrite(motorPin1, 0);
    analogWrite(motorPin2, 180);   
 analogWrite(motorPin3, 180);
    analogWrite(motorPin4, 0);     
  delay(300);
   moveForward();




Sarazarvisphineix

Am using Arduino nano ATmega328 ... regarding that plz provide me a code

The components am using
1------WD4 robot chassis
2-------arduino nano AT MEGA 328
3--------ultrrasonic senior
4--------Robodo Electronics L298 Motor Driver Module
5-----BB-801 400 Tie Point Solderless Breadboard Circuit Experiment Accessory
6-----BRC 18650 6800mah battery

PaulS

Code: [Select]

#include <NewPing.h>http:max

What is THAT nonsense?

Quote
regarding that plz provide me a code
For a (n astronomical) fee. Writing the code is YOUR job.
The art of getting good answers lies in asking good questions.

slipstick

You have a code. If you post the actual error messages you get trying to compile it we can help you get it going.

Then all you need to do is to test it and then make it do what you want. If you tell us a) what exactly it does and b) what you want it to do that's different we can probably help with that too.

Steve

Go Up