Problem using NewPing Library

Right now, I am working with the HC-SR04. I am having some difficulties with the NewPing library. When I write out all the code that sends out the trigger pulse and receives through the echoPin with the fadeLED() function, everything works smoothly. When I use the NewPing library with the fadeLED() function, the LED does not do what it should. Could anybody help me with this issue?

Here is the code where the fadeLED() function works:

   This sketch reads a HC-SR04 ultrasonic rangefinder and returns the 
    distance to the closest object in range. To do this, it sends a pulse 
   to the sensor to initiate a reading, then listens for a pulse  
    to return.  The length of the returning pulse is proportional to  
   the distance of the object from the sensor.        
    The circuit: 
  * VCC connection of the sensor attached to +5V 
  * GND connection of the sensor attached to ground 
  * TRIG connection of the sensor attached to digital pin 2 
  * ECHO connection of the sensor attached to digital pin 4 

 */
const int redLEDPin = 6;
const int trigPin = 9;
const int echoPin = 10;
long duration;
int distance;
int pwmOut = 0;
//float engUnits = 2.54;

void setup() {
  // put your setup code here, to run once:
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(redLEDPin, OUTPUT);
Serial.begin(9600);
}

void loop() {
  // The sensor is triggered by a HIGH pulse of 10 or more microseconds. 
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse: 
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
  // Read the signal from the sensor: a HIGH pulse whose 
  // duration is the time (in microseconds) from the sending 
  // of the ping to the reception of its echo off of an object. 
duration = pulseIn(echoPin, HIGH);
// The speed of sound is 340 m/s or 29 microseconds per centimeter. 
// The ping travels out and back, so to find the distance of the 
// object we take half of the distance travelled. 
distance = duration*0.034/2;
// engUnits = distance/2.54;
fadeLED();
Serial.print("Distance = ");
Serial.print(distance);
Serial.println(" cm");
/*Serial.print("English Units = ");
Serial.print(engUnits);
Serial.println(" in");
*/
delay(250);
}

void fadeLED() {
  pwmOut = map(distance, 1, 60, 255, 1);
  analogWrite(redLEDPin, pwmOut);
  }

Here is the code where the fadeLED() function does not work:

#include <NewPing.h>
int trigPin = 9;
int echoPin = 10;
int maxDist = 100;
int pwmOut = 0;
int redLEDPin = 6;
int distance;
NewPing sonar(trigPin, echoPin, maxDist);


void setup() {

Serial.begin(9600);
pinMode(redLEDPin, OUTPUT);
}

void loop() {
delay(50);
int distance = sonar.ping_cm();
fadeLED();
Serial.print(distance);
Serial.println(" cm");
}

void fadeLED() {
  pwmOut = map(distance, 1, 100, 255, 1);
  analogWrite(redLEDPin, pwmOut);
  }

When I use the NewPing library with the fadeLED() function, the LED does not do what it should.

What should it do ?
What does it do instead ?

Do you see the same value printed for distance in both versions ?
Why are the map() parameters different in the two programs ?

What it should do:
As the distance the distance decreases, the LED gets brighter. As the distance increases, the LED gets dimmer. This is exactly what happens in the first code.

What it does instead:
In the code using the NewPing library, the LED just stays dim regardless of the distance.

I see the same value printed for distance in both programs.

The parameters are different due to experimenting to try to solve the issue. When the parameters are the same, the result is the same, a dim LED.

Your fadeLED sees a different "distance" variable because you created a new "distance" in your loop function.

How many different variables named distance, each with their own scope, do you have in the working program ?

How many different variables named distance, each with their own scope, do you have in the non working program ?

UKHeliBob:
How many different variables named distance, each with their own scope, do you have in the working program ?

How many different variables named distance, each with their own scope, do you have in the non working program ?

In the working sketch I see one global variable named distance. It is used in the distance calculation formula, in the serial print statement and in the fadeLED function.

In the non-working sketch I see one global variable named distance. It is given a value in the loop function and this value is used in the fadeLED function. It is the same integer value displayed in the serial monitor.

n the non-working sketch I see one global variable named distance.

What about the other one declared in the loop() function ?

UKHeliBob:
What about the other one declared in the loop() function ?

I believe you are trying to lead me somewhere, but I'm too obtuse to see where you are going with it. If I comment out the distance variable at the start of the sketch, the compiler returns an error in the fadeLED function definition. But I did the same thing in the sketch that works. What's the difference?

int distance = sonar.ping_cm();

This one

I believe you are trying to lead me somewhere, but I'm too obtuse to see where you are going with it.

int distance;Declares a global variable named distance

int distance = sonar.ping_cm();Declares a different variable with the same name but a different scope

UKHeliBob:
int distance;Declares a global variable named distance

int distance = sonar.ping_cm();Declares a different variable with the same name but a different scope

This is critically important for me to understand the difference and at this point I'm just not getting it. If I don't declare 'distance. as a variable, I get an error. In both sketches does not this variable simply return an integer which represents the actual distance?

Try this is your loop function

distance = sonar.ping_cm();

CtrlAltElite:
Try this is your loop function

distance = sonar.ping_cm();

all this does is to remove the type of variable we are working with. Why does removing int change anything? Also, the map function only works with int variables.

all this does is to remove the type of variable we are working with.

No. Why do you think that? The compiler knows what sort of variable you're using - you already told it the type.

Hi

In your second program you declare an integer variable named 'distance' at the start of your program outside any of the functions. This means that the variable is available to all functions and is know as a "global" variable.

By using the reserved word "int" you defined a new integer variable also called 'distance' inside the loop() function. This variable is "local" to loop() and not available outside loop(). This is then assigned the value returned by the sonar.ping_cm() function.

Therefore when you use 'distance' inside the fadeLED() function it refers to the global variable which was assigned a default value of 0 when it was first defined. This falls outside the defined range if your map() function which then returns a value of 1 to the 'pwmOut' variable. As nothing in the program ever changes the global variable 'distance' you will always get 1 assigned to 'pwmOut' and your led will always be dim.

Remove the "int" keyword from the line in the loop() function and it will then change the global variable 'distance' and your program should work.

Hope this helps

Ian

Thanks to all! The light bulb finally went on! I got it!