NewPing does not name a type

Hi
I’m new to the programming thing…
I downloaded a program from ElectroMinds.com.
Firstly it gave me a error with '#'
I removed the #parts from the program.
```
/

#include <NewPing.h>

#define DEBUG         true // Set to true to enable Serial debug
#define TRIGGER_PIN   12  // Board pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN      11  // Board pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE  300 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

// leds pins
int ledR = 3;
int ledY = 4;
int ledG = 5;

int potPin = A0;

int optDistance;
int optDistanceGap = 5;
int optMin;
int optMax;
int yellowGap = 10;
int redGap = 20;
int maxDistance = MAX_DISTANCE;
int currentDistance;
int prevDistance = 0;
int deviationThreshold = 2;
int timerStartTime = 0;
int ledsTimout = 30; // seconds

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

void setup() {
 if (DEBUG) {
   Serial.begin(115200);
   Serial.println(“Garage sensor is starting”);
 }
 
 pinMode(ledG, OUTPUT);
 pinMode(ledY, OUTPUT);
 pinMode(ledR, OUTPUT);
}

void loop() {
 
 // get pot reading for the optimal distance.
 optDistance = analogRead(potPin) / 5; // potentiometer readins are in between 0 and 1023. Deviding it by 5 makes our optimal distance range from 0 to about 200cm
 
 // recalculate optimal distance gaps
 optMin = optDistance - optDistanceGap;
 optMax = optDistance + optDistanceGap;

if (DEBUG) {
   Serial.print(“Optimal distance:”);
   Serial.println(optDistance);
 }
 
 delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.

currentDistance = sonar.ping() / US_ROUNDTRIP_CM; // Send ping, get ping time in microseconds (uS) and translate it into cm
 
 if (abs(currentDistance - prevDistance) >= deviationThreshold) {
   prevDistance = currentDistance;
   timerStartTime = millis();
   
   if (DEBUG) {
     Serial.print(“Current distance:”);
     Serial.println(currentDistance);
   }
   
   // if distance is in range of optimal distances, turn on green led
   if (inRange(currentDistance, optMin, optMax)) {
     setLeds(LOW, LOW, HIGH);
     if (DEBUG) Serial.println(“Got in optimal distance range”);
   }
   // if distance is close to the optimal distances, but not yet there, turn on yellow led
   else if (inRange(currentDistance, optMin - yellowGap, optMin) || inRange(currentDistance, optMax, optMax + yellowGap)) {
     setLeds(LOW, HIGH, LOW);
     if (DEBUG) Serial.println(“Close to the optimal distance”);
   }
   // if distance is close to zero or too far, turn on red led
   else if (inRange(currentDistance, 0, optMin - yellowGap) || inRange(currentDistance, optMax + yellowGap, optMax + yellowGap + redGap)) {
     setLeds(HIGH, LOW, LOW);
     if (DEBUG) Serial.println(“Too far from the optimal distance”);
   }
   // otherwise turn all leds off
   else {
     setLeds(LOW, LOW, LOW);
     if (DEBUG) Serial.println(“Turn all leds off”);
   }
 } else {
   int currentTime = millis();
   if ((currentTime - timerStartTime) / 1000 > ledsTimout) {
     if (DEBUG) Serial.println((currentTime - timerStartTime) / 1000);
     setLeds(LOW, LOW, LOW);
   }
 }
}

/**

  • Turn leds on and off
    */
    void setLeds(int r, int y, int g) {
     digitalWrite(ledR, r);
     digitalWrite(ledY, y);
     digitalWrite(ledG, g);
    }

/**

  • Check is value in range
    /
    bool inRange(int d, int rangeMin, int rangeMax) {
     return d >= rangeMin && d < rangeMax;
    }
    *
    ```
    Now I have a NewPing does not name a type error.
    Can someone please a dummy?? :confused:

I removed the #parts from the program.

You can’t just blindly remove stuff from a program and expect it to work. Is your library installed?

Please read these two posts:

How to use this forum - please read.
and
Read this before posting a programming question …

You have posted code without using code tags. The code tags make the code look

like this

when posting source code files. It makes it easier to read, and can be copied with a single mouse click. Also, if you don’t do it, some of the character sequences in the code can be misinterpred by the forum code as italics or funny emoticons.
If you have already posted without using code tags, open your message and select “modify” from the pull down menu labelled, “More”, at the lower left corner of the message. Highlight your code by selecting it (it turns blue), and then click on the “</>” icon at the upper left hand corner. Click on the “Save” button. Code tags can also be inserted manually in the forum text using the code and /code metatags.

Where did you put the NewPing library?

Well, we know you found the bold tags, so how about finding the code tags?

I downloaded v1.7 as I cannot find V1.5.

The guy used v1.5 so i don't know if it will make a difference.

Installed in:

Sketch/ include Library/ add .zip library

In library manager when i search for NewPing. it shows me "NewPing version unknown Installed"

Here's the link to the project he's talking about (to the OP: use the link button to add links):

http://www.electrominds.com/projects/arduino-garage-parking-sensor

Also to the OP, I hope you went back to the original program. The "#" parts are necessary.

Beastl8r: Sketch/ include Library/ add .zip library

In library manager when i search for NewPing. it shows me "NewPing version unknown Installed"

Did you unzip it? :)

aarg: Did you unzip it? :)

i selected the zip file as is.

Beastl8r: i selected the zip file as is.

The IDE cannot read zip files. The library installer might, but I never use it. In any case, look in the library folder and see what's there. It should be unzipped.

I think I have the library issue sorted.

I have the following error now.

sketch_jan24b:40: error: a function-definition is not allowed here before '{' token

 void setup() {

              ^

sketch_jan24b:121: error: expected '}' at end of input

 }

 ^

exit status 1
a function-definition is not allowed here before '{' token

Any ideas?

sketch_jan24b:121: error: expected '}' at end of input

That seems pretty explicit - have you matched your opening { with your closing } ?

Beastl8r: I think I have the library issue sorted.

I have the following error now.

sketch_jan24b:40: error: a function-definition is not allowed here before '{' token

void setup() {

              ^

sketch_jan24b:121: error: expected '}' at end of input

}

^

exit status 1 a function-definition is not allowed here before '{' token




Any ideas?

Are you sure you are using the code as it appears in your first post? The original code above does not seem to have "{}" problems.

Hi

Thank you guys got it to work