Skip to content

Commit

Permalink
ultrasonic-distance: fixed printing, showing the scope in a more user…
Browse files Browse the repository at this point in the history
…-friendly way
  • Loading branch information
giuliomoro committed Mar 18, 2021
1 parent be75017 commit c4c91dc
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions examples/Sensors/ultrasonic-distance/render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,24 +81,23 @@ void render(BelaContext *context, void *userData)
} else {
state = LOW;
}

digitalWrite(context, n, gTrigDigitalOutPin, state); //write the state to the trig pin

int pulseLength = pulseIn.hasPulsed(context, n); // will return the pulse duration(in samples) if a pulse just ended
float duration = 1e6 * pulseLength / context->digitalSampleRate; // pulse duration in microseconds
static float distance = 0;
if(pulseLength >= gMinPulseLength){
static int count = 0;
// rescaling according to the datasheet
distance = duration / gRescale;
if(count > 5000){ // we do not want to print the value every time we read it
rt_printf("pulseLength: %d, distance: %fcm\n", pulseLength, distance);
count -= 5000;
}
++count;
}
static int count = 0;
if(count > 5000){ // we do not want to print the value every time we read it
rt_printf("pulseLength: %d, distance: %fcm\n", pulseLength, distance);
count = 0;
}
++count;
// Logging to the scope the pulse inputs (gEchoDigitalInPin) and the distance
scope.log(digitalRead(context, n, gEchoDigitalInPin), distance/100);
scope.log(digitalRead(context, n, gEchoDigitalInPin) * 0.5, distance/100);
}
}

Expand Down

0 comments on commit c4c91dc

Please sign in to comment.