Skip to content

Instantly share code, notes, and snippets.

@Linkaan
Last active November 12, 2015 22:02
Show Gist options
  • Select an option

  • Save Linkaan/6b2117ea31fa634e77e3 to your computer and use it in GitHub Desktop.

Select an option

Save Linkaan/6b2117ea31fa634e77e3 to your computer and use it in GitHub Desktop.

Revisions

  1. Linkaan revised this gist Nov 12, 2015. 1 changed file with 17 additions and 18 deletions.
    35 changes: 17 additions & 18 deletions ultrasonicsensor_motion.ino
    Original file line number Diff line number Diff line change
    @@ -1,22 +1,22 @@
    #include <NewPing.h>

    #define MAX_TIME 28628
    #define MAX_TIME 17202

    #define TRIGGER_PIN 10 // Arduino pin tied to trigger pin on ping sensor.
    #define ECHO_PIN 9 // Arduino pin tied to echo pin on ping sensor.
    #define MAX_DISTANCE 400 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
    #define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

    NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

    unsigned int pingSpeed = 50; // How frequently are we going to send out a ping (in milliseconds). 50ms would be 20 times a second.
    unsigned long pingTimer; // Holds the next ping time.

    unsigned int iterations = 10;
    float g = 0.75f; // this is a coefficient between 0.0 and 1.0
    unsigned int iterations = 5;
    float g = 0.95f; // this is a coefficient between 0.0 and 1.0
    // the higher it is the more "inert" the filter will be
    float avg_time = MAX_TIME;
    float avg_dt = 0.0f;
    float dt_hysteresis = 0.01f;
    float dt_hysteresis = 1.0f;

    void setup() {
    Serial.begin(9600); // Open serial monitor at 9600 baud to see ping results.
    @@ -45,21 +45,20 @@ void echoCheck() { // Timer2 interrupt calls this function every 24uS where you
    float prev_avg_time = avg_time;
    avg_time = avg_time * g + (1.0f - g) * echo_time;
    avg_dt = avg_dt * g + (1.0f - g) * (avg_time - prev_avg_time);

    if(avg_dt < -dt_hysteresis || avg_dt > +dt_hysteresis) {
    avg_time = MAX_TIME;
    Serial.print("motion event, at ");
    Serial.println(millis());
    Serial.print("avg_dt is ");
    Serial.println(avg_dt);
    }
    }

    if(avg_dt < -dt_hysteresis || avg_dt > +dt_hysteresis) {
    Serial.print("motion event, at ");
    Serial.println(millis());
    }else if(avg_dt != 0) {
    Serial.print("avg_dt is ");
    Serial.println(avg_dt);
    }
    iterations = 10;
    }else if(--iterations == 0) {
    Serial.print("Reset iterations at ");
    Serial.println(millis());

    iterations = 5;
    }else if(rc > 1 && --iterations == 0) {
    avg_time = MAX_TIME;
    iterations = 10;
    iterations = 5;
    }
    // Don't do anything here!
    }
  2. Linkaan created this gist Nov 12, 2015.
    65 changes: 65 additions & 0 deletions ultrasonicsensor_motion.ino
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,65 @@
    #include <NewPing.h>

    #define MAX_TIME 28628

    #define TRIGGER_PIN 10 // Arduino pin tied to trigger pin on ping sensor.
    #define ECHO_PIN 9 // Arduino pin tied to echo pin on ping sensor.
    #define MAX_DISTANCE 400 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

    NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

    unsigned int pingSpeed = 50; // How frequently are we going to send out a ping (in milliseconds). 50ms would be 20 times a second.
    unsigned long pingTimer; // Holds the next ping time.

    unsigned int iterations = 10;
    float g = 0.75f; // this is a coefficient between 0.0 and 1.0
    // the higher it is the more "inert" the filter will be
    float avg_time = MAX_TIME;
    float avg_dt = 0.0f;
    float dt_hysteresis = 0.01f;

    void setup() {
    Serial.begin(9600); // Open serial monitor at 9600 baud to see ping results.
    pingTimer = millis(); // Start now.
    }

    void loop() {
    // Notice how there's no delays in this sketch to allow you to do other processing in-line while doing distance pings.
    if (millis() >= pingTimer) { // pingSpeed milliseconds since last ping, do another ping.
    pingTimer += pingSpeed; // Set the next ping time.
    sonar.ping_timer(echoCheck); // Send out the ping, calls "echoCheck" function every 24uS where you can check the ping status.
    }
    // Do other stuff here, really. Think of it as multi-tasking.
    }

    void echoCheck() { // Timer2 interrupt calls this function every 24uS where you can check the ping status.
    // Don't do anything here!
    uint8_t rc = sonar.check_timer();
    if(rc==0) { // This is how you check to see if the ping was received.
    float echo_time = (float) sonar.ping_result;

    if(avg_time == MAX_TIME) {
    avg_time = echo_time;
    avg_dt = 0.0f;
    }else {
    float prev_avg_time = avg_time;
    avg_time = avg_time * g + (1.0f - g) * echo_time;
    avg_dt = avg_dt * g + (1.0f - g) * (avg_time - prev_avg_time);
    }

    if(avg_dt < -dt_hysteresis || avg_dt > +dt_hysteresis) {
    Serial.print("motion event, at ");
    Serial.println(millis());
    }else if(avg_dt != 0) {
    Serial.print("avg_dt is ");
    Serial.println(avg_dt);
    }
    iterations = 10;
    }else if(--iterations == 0) {
    Serial.print("Reset iterations at ");
    Serial.println(millis());
    avg_time = MAX_TIME;
    iterations = 10;
    }
    // Don't do anything here!
    }