Help in US 100 Ultrasonic Sonar Codes

Discussion in 'Embedded Systems and Microcontrollers' started by encrypted05, Dec 10, 2014.

  1. encrypted05

    Thread Starter New Member

    Nov 24, 2014
    6
    0
    Hello! The US 100 Ultrasonic will be used to detect the presence of an object in just a range of 3 inches. It should be displaying a 1 if an object is detected and 0 if otherwise. I modified the codes from someone but unfortunately, it doesn't work the way it was supposed to be and I'm having a hard time trying to find out what went wrong. I hope someone can help me. Thank you. Below would be the codes and the printscreen of the output.
    Code (Text):
    1.  
    2.  
    3. /*-----( Import needed libraries )-----*/
    4. #include <NewPing.h>
    5. /*-----( Declare Constants and Pin Numbers )-----*/
    6. #define  TRIGGER_PIN  11
    7. #define  ECHO_PIN     10
    8. #define MAX_DISTANCE  14 // Maximum distance we want to ping for (in centimeters).
    9.                          //Maximum sensor distance is rated at 400-500cm.
    10. /*-----( Declare objects )-----*/
    11. NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
    12. /*-----( Declare Variables )-----*/
    13. int DistanceIn;
    14. int DistanceCm;
    15.  
    16. void setup()   /****** SETUP: RUNS ONCE ******/
    17. {
    18.   Serial.begin(9600);
    19.   Serial.println("UltraSonic Distance Measurement");
    20.   Serial.println("YourDuino.com  terry@yourduino.com");
    21. }//--(end setup )---
    22.  
    23. void loop()   /****** LOOP: RUNS CONSTANTLY ******/
    24. {
    25.   delay(100);// Wait 100ms between pings (about 10 pings/sec). 29ms should be the shortest delay between pings.
    26.   DistanceIn = sonar.ping_in();
    27.   Serial.print("Ping: ");
    28.   Serial.print(DistanceIn); // Convert ping time to distance and print result
    29.                             // (0 = outside set distance range, no ping echo)
    30.   Serial.print(" in     ");
    31.   delay(100);// Wait 100ms between pings (about 10 pings/sec). 29ms should be the shortest delay between pings.
    32.   DistanceCm = sonar.ping_cm();
    33.   Serial.print("Ping: ");
    34.   Serial.print(DistanceCm);
    35.   Serial.println(" cm");  
    36.   if ( (DistanceCm <= 7) && (DistanceCm != 0) )
    37.   {
    38.   Serial.println("1");
    39.   }
    40.   else if ( (DistanceCm > 7) && (DistanceCm !=0) )
    41.   {
    42.   Serial.println("0");
    43.   }
    44.  
    45. }//--(end main loop )---
    46.  
    47. /*-----( Declare User-written Functions )-----*/
    48.  
    49. // None
    50. //*********( THE END )***********
    [​IMG]
     
  2. jpanhalt

    AAC Fanatic!

    Jan 18, 2008
    5,671
    898
    Please post the source of your code.
    John
     
  3. encrypted05

    Thread Starter New Member

    Nov 24, 2014
    6
    0
Loading...