'NewPing' does not name a type

Discussion in 'Programmer's Corner' started by encrypted05, Nov 24, 2014.

  1. encrypted05

    Thread Starter New Member

    Nov 24, 2014
    6
    0
    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 (Text):
    1. #include <NewPing.h>
    2. /*-----( Declare Constants and Pin Numbers )-----*/
    3. #define  TRIGGER_PIN  11
    4. #define  ECHO_PIN     10
    5. #define MAX_DISTANCE  14 // Maximum distance we want to ping for (in centimeters).
    6.                          //Maximum sensor distance is rated at 400-500cm.
    7. /*-----( Declare objects )-----*/
    8. NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
    9. /*-----( Declare Variables )-----*/
    10. int DistanceIn;
    11. int DistanceCm;
    12.  
    13. void setup()   /****** SETUP: RUNS ONCE ******/
    14. {
    15.   Serial.begin(9600);
    16.   Serial.println("UltraSonic Distance Measurement");
    17.   Serial.println("YourDuino.com  terry@yourduino.com");
    18. }//--(end setup )---
    19.  
    20.  
    21. void loop()   /****** LOOP: RUNS CONSTANTLY ******/
    22. {
    23.   delay(100);// Wait 100ms between pings (about 10 pings/sec). 29ms should be the shortest delay between pings.
    24.   DistanceIn = sonar.ping_in();
    25.   Serial.print("Ping: ");
    26.   Serial.print(DistanceIn); // Convert ping time to distance and print result
    27.                             // (0 = outside set distance range, no ping echo)
    28.   Serial.print(" in     ");
    29.  
    30.   delay(100);// Wait 100ms between pings (about 10 pings/sec). 29ms should be the shortest delay between pings.
    31.   DistanceCm = sonar.ping_cm();
    32.   Serial.print("Ping: ");
    33.   Serial.print(DistanceCm);
    34.   Serial.println(" cm");
    35.   if ( (DistanceCm <= 7) && (DistanceCm != 0) )
    36.   {
    37.   Serial.println("3 Inches or closer! ");
    38.   }
    39.  
    40. }//--(end main loop )---
    [​IMG]
     
  2. sirch2

    Well-Known Member

    Jan 21, 2013
    1,008
    351
    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.
     
    encrypted05 likes this.
  3. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    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.
     
  4. encrypted05

    Thread Starter New Member

    Nov 24, 2014
    6
    0
    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. :)
     
  5. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    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). :)
     
Loading...