0

I am relatively New to Arduino and need your help. Actually i made a code and the problem is that the ultrasound sensor doesn't work at all. It isn't displaying the distance between the object and and the sensor. Can you help me please! Here is my code:

note: the code has some motor cotrol commands

int enableBridgePin = 10; 
int forwardPin = 11;
int backWardPin = 9;
int tickPin = 2;
const int trig = 5;
const int echo = 6;

int dt;
int distance;

float rotationSpeed[50];

void setup() {
  pinMode(enableBridgePin, OUTPUT);
  pinMode(forwardPin, OUTPUT);
  pinMode(backWardPin, OUTPUT); 
  pinMode(trig, OUTPUT);
  pinMode(echo, INPUT);
  Serial.begin(9600);

  digitalWrite(forwardPin, LOW);
  digitalWrite(backWardPin, LOW);

  interrupts(); 
  digitalWrite(enableBridgePin, HIGH);
}

void loop() {
  analogWrite(enableBridgePin,255);

  digitalWrite(forwardPin, HIGH);
  digitalWrite(backWardPin, LOW);

  distancefonction(dt, distance);
  if(distance>5){
   detachInterrupt(digitalPinToInterrupt(tickPin));
   digitalWrite(forwardPin, LOW);
   }

}

int distancefonction(int dt, int distance)
{
  digitalWrite(trig, LOW);
  delayMicroseconds(2);
  digitalWrite(trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig, LOW);
  dt = pulseIn(echo, HIGH);
  distance = 0.034/2*dt;
  Serial.println(distance );}
  • reduce the sketch to minimum to measure and print the distance and then test it. – Juraj Dec 4 '19 at 20:42
0

You have declared int distance outside of any method. That means it's available from all methods. Some people call it a global variable.

However, you have a method int distancefonction(int dt, int distance) which receives int distance as a parameter. This results in an ambiguity: if you refer to distance within the method like

distance = 0.034/2*dt;

should this now refer to the parameter distance or the global variable distance?

The answer is: you can't decide. The C/C++ standard has decided for you and the answer is: the parameter will take precedence and thus hide the global variable. This thing is also called "shadowing".

What does it actually mean?

  1. The global variable distance has an undefined value at the start of the program. Let's assume the value is 0.

  2. distancefonction() is called, passing distance as a parameter. This will create a copy of the value 0 on the stack.

  3. the function will calculate the distance and update the variable on the stack.

  4. The Serial.println() statement within the function accesses the variable on the stack and will print the correct distance.

  5. Once the function is complete, the code in loop() will access the global variable distance, which still has the old value, because only a copy has been updated.

What is the solution?

Of course there are several solutions.

  1. The simplest fix is probably to remove the parameter int distance from distancefonction. Thus the function will not receive a copy and instead operate on the global variable.

  2. Another solution is to let distancefonction return the actual distance. The method signature already indicates that you wanted to return a value: the intfrom int distancefunction(...) says that. At the moment, the function does not return anything. You could write return distance; at the end. This approach could eliminate the global variable completely.

| improve this answer | |
  • "Some people call it a global variable." because if it is on the top of the project's main ino file, the it has global scope – Juraj Dec 5 '19 at 8:09
  • thanks a lot for your help it worked! i actually returned the distance from the function. – kevin0304 Dec 8 '19 at 9:37

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.