How to calculate distance using arduino

I am using GSM module with arduino and also drive motors of robot. so I want to calculate how much distance covered by robot linearly and print that distance in message which will send through GSM module.

i tried this formula in void loop
but its not working. plz help me.

my code is

#define trigPin 12 // output for sensor
#define echoPin 2 // input for sensor
#define leftmotor 5
#define rightmotor 6
#define led 13
#define rxPin 0
#define txPin 1
#define ldr1 4
#define ldr2 7
#include <SoftwareSerial.h>
#include <string.h>

void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(leftmotor, OUTPUT);
pinMode(rightmotor, OUTPUT);
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
pinMode(led, OUTPUT);
pinMode(ldr1, INPUT);
pinMode(ldr2, INPUT);
}

void loop() {
int time_request;
unsigned long int time, duration, distance, pathcover;
digitalWrite(trigPin, HIGH);
delayMicroseconds(1000);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance <=20)
{
Serial.print(distance);
Serial.println(" cm");
Serial.print(pathcover);
Serial.println(" m");
digitalWrite(leftmotor, LOW);
digitalWrite(rightmotor, LOW);
digitalWrite(led, HIGH);
delay(8000);
digitalWrite(trigPin, HIGH);
delayMicroseconds(1000);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance <=20)
{
Serial.print(distance);
Serial.println(" cm");
Serial.println("AT");
delay(2000);
Serial.println("AT+CMGF=1");
delay(1500);
Serial.print("AT+CMGS=");
delay(2000);
Serial.print((char)34);
Serial.print("+919096532386");
delay(4000);
Serial.println((char)34);
delay(2000);
Serial.println("Obstacle detected on "-pathcover-" m from start point");
delay(10000);
Serial.println((char)26);
return;
}
}
else {
Serial.println("Out of range");
digitalWrite(leftmotor, HIGH);
digitalWrite(rightmotor, HIGH);
digitalWrite(led, LOW);
}

delay(500);
}

What is "time_request" supposed to be? It does not seem to be the number of revolutions the wheel has turned, which your pathcover equation is expecting.

You also initialize it to 0 at the top of loop every pass with
int time_request;

``````void loop() {
int time_request;
unsigned long int time, duration, distance, pathcover;
digitalWrite(trigPin, HIGH);
delayMicroseconds(1000);
digitalWrite(trigPin, LOW);
``````

In what programming language is this valid ?

``````Serial.println("Obstacle detected on "-pathcover-" m from start point");
``````

@sagarsukoshe: Please edit your post, select the code, and put it between [code] ... [/code] tags.

You can do that by hitting the # button above the posting area.

How to use this forum

In what programming language is this valid ?
Code:

Serial.println("Obstacle detected on "-pathcover-" m from start point");

C/C++, it would appear.
I wonder what it does?
It isn't a simple string continuation - maybe it is a bizarre constant pointer arithmetic operation

Yep, that compiles fine. No worries.

``````unsigned long int  pathcover;

void setup ()
{
Serial.begin (115200);
Serial.println("Obstacle detected on "-pathcover-" m from start point");
}  // end of setup

void loop () { }
``````

Output:

``````-22
``````

Of course, the answer should really be 42

Let's work out why.

We'll assume that the string "Obstacle detected on " was allocated at address 256.

It is 22 bytes long including the trailing null.

And we'll assume that " m from start point" was allocated at 256 + 22.

So the calculation gives us:

``````256 - 0 - (256 + 22)
``````

Yep, that's -22.

Of course, the answer should really be 42

The answer really should be "There is no spoon."

Suppression of warnings really is a double-edged sword.

You don't want to frighten people with warnings. They might get alarmed.

42 I can see. (How much is 6 x 8 ?)

Spoon? Lost me there.

Or as Magritte might have said "Ceci n'est pas une cuillère"