Skip to content

Instantly share code, notes, and snippets.

@nix010
Last active April 6, 2018 10:31
Show Gist options
  • Save nix010/c6b2d425f9962ee79c54956a5d71fc5f to your computer and use it in GitHub Desktop.
Save nix010/c6b2d425f9962ee79c54956a5d71fc5f to your computer and use it in GitHub Desktop.

Revisions

  1. nix010 revised this gist Apr 6, 2018. 1 changed file with 6 additions and 3 deletions.
    9 changes: 6 additions & 3 deletions ultrasonic.ino
    Original file line number Diff line number Diff line change
    @@ -5,8 +5,11 @@
    #define MAX_DISTANCE 100 // Maximum distance we want to ping for (in centimeters). Maximum sensor
    #define US_ROUNDTRIP_CM 58

    NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
    pinMode(11, OUTPUT);// Open serial monitor at 115200 baud to see ping results.
    void setup() {
    NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
    pinMode(ECHO_PIN, OUTPUT);// Open serial monitor at 115200 baud to see ping results.
    }


    void loop() {
    unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
    @@ -18,6 +21,6 @@ void loop() {
    Serial.println("cm");

    if(distance < 30 && distance > 0 ){
    //
    // distance from 0-30 cm
    }
    }
  2. nix010 revised this gist Apr 4, 2018. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions ultrasonic.ino
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    // https://docs.google.com/presentation/d/1rrYLHVR3_Fc7mmvc8I8zGOhtoBDN02RjF_-aecimZdg/edit?usp=sharing
    #include <NewPing.h>
    #define TRIGGER_PIN 12 // Arduino pin tied to trigger pin on the ultrasonic sensor.
    #define ECHO_PIN 11 // Arduino pin tied to echo pin on the ultrasonic sensor.
  3. nix010 revised this gist Apr 4, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ultrasonic.ino
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@
    #define TRIGGER_PIN 12 // Arduino pin tied to trigger pin on the ultrasonic sensor.
    #define ECHO_PIN 11 // Arduino pin tied to echo pin on the ultrasonic sensor.
    #define MAX_DISTANCE 100 // Maximum distance we want to ping for (in centimeters). Maximum sensor
    #define US_ROUNDTRIP_CM 100
    #define US_ROUNDTRIP_CM 58

    NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
    pinMode(11, OUTPUT);// Open serial monitor at 115200 baud to see ping results.
  4. nix010 created this gist Apr 4, 2018.
    22 changes: 22 additions & 0 deletions ultrasonic.ino
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    #include <NewPing.h>
    #define TRIGGER_PIN 12 // Arduino pin tied to trigger pin on the ultrasonic sensor.
    #define ECHO_PIN 11 // Arduino pin tied to echo pin on the ultrasonic sensor.
    #define MAX_DISTANCE 100 // Maximum distance we want to ping for (in centimeters). Maximum sensor
    #define US_ROUNDTRIP_CM 100

    NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
    pinMode(11, OUTPUT);// Open serial monitor at 115200 baud to see ping results.

    void loop() {
    unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).

    unsigned int distance = uS / US_ROUNDTRIP_CM;

    Serial.print("Ping: ");
    Serial.print(distance); // Convert ping time to distance in cm and print result (0 = outside set distance range)
    Serial.println("cm");

    if(distance < 30 && distance > 0 ){
    //
    }
    }