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 >= 0) && (monroy <= 5));
  {
    digitalWrite (4, HIGH);
    digitalWrite (5, LOW);
    digitalWrite (6, LOW);
  }
  if ((monroy >= 5.1) && (monroy <= 9));
  {

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

    digitalWrite (5, LOW);
    digitalWrite (6, HIGH);
  }
}
8
  • do you have the Arduino connected to anything?
    – jsotola
    May 1 '18 at 22:49
  • My Arduino is connected to my laptop
    – mmiguell
    May 1 '18 at 22:50
  • What do you have connected to pins 9 and 10 of your Arduino? You might find this post on our meta site helpful: How to ask a good question for Arduino Stack Exchange May 1 '18 at 22:59
  • In the 9th and 10th pins I have the echo and trig pins connected. Thanks!
    – mmiguell
    May 1 '18 at 23:00
  • Please do a Serial.print of sofia so we can see what value is being used for the subsequent calculation.
    – Nick Gammon
    May 1 '18 at 23:05
4

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 >= 0) && (monroy <= 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 >= 5.1) && (monroy <= 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 >= 9.1);

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

  if (distance = 15);  // <-- 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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.