Ultrasonic sensor doesn't puts out correct numbers

What is the "it" that you don't think will change the values you get?

I'm guessing that as well as being new to Arduino, you're also new to ultrasonic ranging?

#include <Servo.h>

int mleft1 = 2;
int mleft2 = 3;
int mright1 = 4;
int mright2 = 5;



Servo s;



int echoPin = 6;
int trigPin = 7;
int x;
int duration = 0;
int durationleft;
int durationright;
int distance = 0;
int distanceleft;
int distanceright;


void setup() {

  Serial.begin(9600);
  pinMode (mleft1, OUTPUT);
  pinMode (mleft2, OUTPUT);
  pinMode (mright1, OUTPUT);
  pinMode (mright2, OUTPUT);
  s.attach(10);

  s.write(100);

  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);


}

void loop() {

  drive();

  rangeFunction ();

  Serial.print(distance);
  Serial.println("cm");


  if (distance < 20) {

    digitalWrite (mleft1, LOW);
    digitalWrite (mleft2, HIGH);
    digitalWrite (mright1, LOW);
    digitalWrite (mright2, HIGH);
    delay(4000);
    brake();
    delay(300);

    turn1();
    delay(3000);
    rangeFunction ();



    Serial.print(distanceright);
    Serial.println("cm right");
    s.write(100);

    if (distanceright < 20) {
      digitalWrite (mleft1, LOW);
      digitalWrite (mleft2, LOW);
      digitalWrite (mright1, HIGH);
      digitalWrite (mright2, LOW);
      delay(2000);

    }
    turn2();
    delay(3000);
    rangeFunction ();


    s.write(100);
    Serial.print(distanceleft);
    Serial.println("cm left");

    if (distanceleft < 20) {
      digitalWrite (mleft1, HIGH);
      digitalWrite (mleft2, LOW);
      digitalWrite (mright1, LOW);
      digitalWrite (mright2, LOW);
    }
    else {
      drive();
    }
  }
}
void turn1() {
  s.write(100);
  delay(1000);
  s.write(40);

}

void drive() {
  digitalWrite (mleft1, HIGH);
  digitalWrite (mleft2, LOW);
  digitalWrite (mright1, HIGH);
  digitalWrite (mright2, LOW);

}

void turn2() {
  s.write(100);
  delay(2000);
  s.write(180);
  delay(3000);


}
void brake() {
  digitalWrite (mleft1, LOW);
  digitalWrite (mleft2, LOW);
  digitalWrite (mright1, LOW);
  digitalWrite (mright2, LOW);
  delay(1000);
}
float rangeFunction ()
{
  digitalWrite (trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  unsigned long duration = pulseIn(echoPin, HIGH);
  return (duration / 2) * 0.03432 ;
}

how am i supposed to do the if loop without any distance val

int duration = 0;
int durationleft;
int durationright;

Are you still using 'int'? The pulseIn() function returns 'unsigned long'.

rangeFunction returns a value.

Why aren't you using or saving that value ?

Slow down.

how am i supposed to do the if loop without any distance val

There's no such thing as an "if loop".
Work through the basics.

Forget the car - as I said on your other topic, it is ambitious.

I thought you had to use int to declare a val

You are calling the function that returns the range but you didn't store the range anywhere. Try:

  distance = rangeFunction ();

and

  distanceright = rangeFunction ();

and

  distanceleft = rangeFunction ();

Also, 'rangefunction()' returns a 'float' but you store the values in 'int' varaibles.

chillimilli:
I thought you had to use int to declare a val

You have to specify the type. 'int', 'float', 'unsigned long' are all types of variables.

isnt there a method without rangeFunction

chillimilli:
isnt there a method without rangeFunction

Didn't you already try that?
How did that work out for you?

Slow down.
Forget the car.
Work through the basics.

I still think that there is a simple problem in my code which i just dont see

chillimilli:
I still think that there is a simple problem in my code which i just dont see

Which is a very good reason for putting your code aside, and WORKING AT THE BASICS.

(The simple problem is that it is a mess)

I know the basics I think range Function doesn't fit in the basics
i can code a Servo, DC motors and a Ultrasonicsensor i just cant fit them togheter and i thought this forum is for problems like this and people will help me

chillimilli:
I know the basics I think range Function doesn't fit in the basics

I think that you're wrong.

If you understood how functions simplified things, you'd try to learn from that, rather than try to fit broken code to your way of thinking.

Your code didn't include any comments, so it's hard to figure-out your intentions of how it is supposed to work

You can't find anything wrong in the code yourself but claim it is broken

TheMemberFormerlyKnownAsAWOL:

float rangeFunction ()

{
  digitalWrite (trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  unsigned long duration = pulseIn(echoPin, HIGH);
return (duration/2) *0.03432 ;
}


(uncompiled, untested)

There you go - a freebie.

As I suggested on your other thread, you could add a timeout to pulseIn. Maybe return a negative range.

Oi! Someone got you to post code! Mark this day.

chillimilli:
You can't find anything wrong in the code yourself but claim it is broken

Does your code do what you want it to? If so, why are you here?

I can't find anything wrong with it, because I don't know what you expect it to do.

But it looks bad.

Idahowalker:
Oi! Someone got you to post code! Mark this day.

You're new here, I take it?

Here is your original sketch with the mistakes fixed. I hope it helps you learn.

#include <Servo.h>
Servo HeadServo;


// I/O Pin Assignments
const byte LeftMotorForwardPin = 2;
const byte LeftMotorBackwardPin = 3;
const byte RightMotorForwardPin = 4;
const byte RightMotorBackwardPin = 5;
const byte TriggerPin = 6;
const byte EchoPin = 7;
const byte ServoPin = 10;


// Some named constants for servo positions, in case you want to change them
const int ServoForwardPosition = 100;  // Sonar pointed straight ahead
const int ServoLeftPosition = 40;  // Sonar pointed left
const int ServoRightPosition = 180;  // Sonar pointed right


void setup()
{
  Serial.begin(9600);


  pinMode (LeftMotorForwardPin, OUTPUT);
  pinMode (LeftMotorBackwardPin, OUTPUT);
  pinMode (RightMotorForwardPin, OUTPUT);
  pinMode (RightMotorBackwardPin, OUTPUT);


  HeadServo.attach(ServoPin);
  HeadServo.write(ServoForwardPosition);


  pinMode(TriggerPin, OUTPUT);
  pinMode(EchoPin, INPUT);
}


// Some constants useful for measuring distance
const float SpeedOfSound = 34300.0; // cm per second
const float MicrosecondsPerCM = 1000000.0 / SpeedOfSound;


// Get the distance in cm
float getDistance()
{
  digitalWrite (TriggerPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(TriggerPin, LOW);


  unsigned long durationMicroseconds = pulseIn(EchoPin, HIGH, MicrosecondsPerCM * 500); // Timeout at 5 meters
  if (durationMicroseconds == 0) // no echo received
    durationMicroseconds = MicrosecondsPerCM * 500.0;  // Out of range (call it 5 meters)


  float roundTripDistanceCM = durationMicroseconds / MicrosecondsPerCM;
  return roundTripDistanceCM / 2.0; // One-way distance in cm
}


float getDistanceAtBearing(int direction)
{
  // Point the sonar in the desired direction
  HeadServo.write(direction);
  delay(30);


  float distance = getDistance();


  // Turn back to Forward
  HeadServo.write(ServoForwardPosition);
  delay(30);


  return distance;
}


void loop()
{
  Forward();


  float distance = getDistance();


  Serial.print(distance);
  Serial.println("cm");


  if (distance < 20.0)
  {
    // Back up for 4 seconds and stop
    Backward();
    delay(4000);
    Brake();


    // Check distances left and right
    float distanceLeft = getDistanceAtBearing(ServoLeftPosition);
    Serial.print("distanceLeft=");
    Serial.print(distanceLeft);

    float distanceRight = getDistanceAtBearing(ServoRightPosition);
    Serial.print(", distanceRight=");
    Serial.println(distanceRight);


    // Turn away from the closer obstacle.
    if (distanceLeft > distanceRight)
    {
      Serial.println("Turning Left");
      TurnLeft();
    }
    else
    {
      Serial.println("Turning Right");
      TurnRight();
    }
    delay(2000);  // Time spent turning (adjust to get the desired turn angle)
  }
}




void Forward()
{
  digitalWrite (LeftMotorForwardPin, HIGH);
  digitalWrite (LeftMotorBackwardPin, LOW);
  digitalWrite (RightMotorForwardPin, HIGH);
  digitalWrite (RightMotorBackwardPin, LOW);
}


void Backward()
{
  digitalWrite (LeftMotorForwardPin, LOW);
  digitalWrite (LeftMotorBackwardPin, HIGH);
  digitalWrite (RightMotorForwardPin, LOW);
  digitalWrite (RightMotorBackwardPin, HIGH);
}


void Brake()
{
  digitalWrite (LeftMotorForwardPin, LOW);
  digitalWrite (LeftMotorBackwardPin, LOW);
  digitalWrite (RightMotorForwardPin, LOW);
  digitalWrite (RightMotorBackwardPin, LOW);
}


void TurnLeft()
{
  digitalWrite (LeftMotorForwardPin, LOW);
  digitalWrite (LeftMotorBackwardPin, LOW);
  digitalWrite (RightMotorForwardPin, HIGH);
  digitalWrite (RightMotorBackwardPin, LOW);
}


void TurnRight()
{
  digitalWrite (LeftMotorForwardPin, HIGH);
  digitalWrite (LeftMotorBackwardPin, LOW);
  digitalWrite (RightMotorForwardPin, LOW);
  digitalWrite (RightMotorBackwardPin, LOW);
}
pinMode(TriggerPin, OUTPUT);
digitalWrite (TriggerPin LOW); // just for completeness/safety ;)
  pinMode(EchoPin, INPUT);

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.