'NewPing' does not name a type

Thread Starter

encrypted05

Joined Nov 24, 2014
6
Hello. I am currently doing a project wherein I'm using a US-100 Ultrasonic as a proximity sensor to detect objects within less than or equal to 3 inches. I asked for the help of someone and he gave me these codes to which I tried but got an error that the 'NewPing' does not name a type. I tried emailing him again to ask how to solve it but has still gotten no reply. I also tried fixing the code by myself but couldn't seem to figure out the right way. I hope any of you could help me in this problem. Thank you very much.

Code:
#include <NewPing.h>
/*-----( Declare Constants and Pin Numbers )-----*/
#define  TRIGGER_PIN  11
#define  ECHO_PIN     10
#define MAX_DISTANCE  14 // Maximum distance we want to ping for (in centimeters).
                         //Maximum sensor distance is rated at 400-500cm.
/*-----( Declare objects )-----*/
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
/*-----( Declare Variables )-----*/
int DistanceIn;
int DistanceCm;

void setup()   /****** SETUP: RUNS ONCE ******/
{
  Serial.begin(9600);
  Serial.println("UltraSonic Distance Measurement");
  Serial.println("YourDuino.com  terry@yourduino.com");
}//--(end setup )---


void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{
  delay(100);// Wait 100ms between pings (about 10 pings/sec). 29ms should be the shortest delay between pings.
  DistanceIn = sonar.ping_in();
  Serial.print("Ping: ");
  Serial.print(DistanceIn); // Convert ping time to distance and print result
                            // (0 = outside set distance range, no ping echo)
  Serial.print(" in     ");
 
  delay(100);// Wait 100ms between pings (about 10 pings/sec). 29ms should be the shortest delay between pings.
  DistanceCm = sonar.ping_cm();
  Serial.print("Ping: ");
  Serial.print(DistanceCm);
  Serial.println(" cm"); 
  if ( (DistanceCm <= 7) && (DistanceCm != 0) )
  {
  Serial.println("3 Inches or closer! ");
  }

}//--(end main loop )---
 

sirch2

Joined Jan 21, 2013
1,037
At a guess the #include <NewPing.h> statement implies you need the NewPing library code which you probably download from somewhere and include in the library path of your compiler/linker.
 

tshuck

Joined Oct 18, 2012
3,534
As sirch2 mentioned, you'll need to get the NewPing library from the Arduino website so that #include had something to reference.

You probably got a warning about not being able to find NewPing.h, that would be your first clue.
 

Thread Starter

encrypted05

Joined Nov 24, 2014
6
It is actually my first time to try Arduino so pardon the lack of knowledge about things such as the NewPing library, but nonetheless, thank you. :)
 

tshuck

Joined Oct 18, 2012
3,534
It is actually my first time to try Arduino so pardon the lack of knowledge about things such as the NewPing library, but nonetheless, thank you. :)
No worries there, I've never even touched an Arduino, what I know has been gleaned vicariously.

However, since it uses C-like syntax, it is clear to a programmer that NewPing.h probably has a type definition within.

Asking when you don't know something is how you get that knowledge (provided you ask the right people the right question). :)
 
Top