Why is this variable giving me always the value of 0? I am doing a project and here is the code that I am using, but I do not know why the values of duration and sofia is always 0 so the values of distance and monroy are also 0

const int trigPin = 9;
const int echoPin = 10;

const int t = 8;
const int e = 7;

long duration, sofia;
int distance, monroy;
void setup() {
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  pinMode(t, OUTPUT);
  pinMode (e, INPUT);
  Serial.begin(9600); // Starts the serial communication
}
void loop() {
  // Clears the trigPin
  digitalWrite(trigPin, LOW);
  digitalWrite (t, LOW);
  delayMicroseconds(2);
  // Sets the trigPin on HIGH state for 10 micro seconds
  digitalWrite(trigPin, HIGH);
  digitalWrite (t, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  digitalWrite (t, LOW);
  // Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);
  sofia = pulseIn(e, HIGH);
  // Calculating the distance
  distance = duration * 0.034 / 2;
  monroy = sofia * 0.034 / 2;
  Serial.print(duration);
  if (distance = 15);
  {
    digitalWrite (3, HIGH);
    delay (5000);
    digitalWrite (3, LOW);
  }

  if ((monroy &gt;= 0) &amp;&amp; (monroy &lt;= 5));
  {
    digitalWrite (4, HIGH);
    digitalWrite (5, LOW);
    digitalWrite (6, LOW);
  }
  if ((monroy &gt;= 5.1) &amp;&amp; (monroy &lt;= 9));
  {

    digitalWrite (5, HIGH);
    digitalWrite (6, LOW);
  }
  if (monroy &gt;= 9.1);
  {

    digitalWrite (5, LOW);
    digitalWrite (6, HIGH);
  }
}
When I formatted your code for readability and then compiled it I got these warnings:

/tmp/arduino_modified_sketch_71221/sketch_may02a.ino: In function 'void loop()':
/tmp/arduino_modified_sketch_71221/sketch_may02a.ino:34:20: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
   if (distance = 15);
                    ^
/tmp/arduino_modified_sketch_71221/sketch_may02a.ino:34:21: warning: suggest braces around empty body in an 'if' statement [-Wempty-body]
   if (distance = 15);
                     ^
/tmp/arduino_modified_sketch_71221/sketch_may02a.ino:41:38: warning: suggest braces around empty body in an 'if' statement [-Wempty-body]
   if ((monroy &gt;= 0) &amp;&amp; (monroy &lt;= 5));
                                      ^
/tmp/arduino_modified_sketch_71221/sketch_may02a.ino:47:40: warning: suggest braces around empty body in an 'if' statement [-Wempty-body]
   if ((monroy &gt;= 5.1) &amp;&amp; (monroy &lt;= 9));
                                        ^
/tmp/arduino_modified_sketch_71221/sketch_may02a.ino:53:21: warning: suggest braces around empty body in an 'if' statement [-Wempty-body]
   if (monroy &gt;= 9.1);


These are important - you should pay attention to them. For example, here:

  if (distance = 15);  // &lt;-- NO SEMICOLON NEEDED HERE!
  {
    digitalWrite (3, HIGH);
    delay (5000);
    digitalWrite (3, LOW);
  }


Putting a semicolon there means that stuff under the if is always executed - the semicolon terminates the scope of the if.



To see the warnings open the Preferences dialog in the IDE GUI and change "Compiler warnings" to "All".



Also:

  if (distance = 15);


This assigns 15 to distance, so distance will always be 15. You mean:

  if (distance == 15)