Hey all.
I have an ultrasonic sensor with code that works well but I want it to measure the lowest point of an object and then freeze the measurement result. How do I enter it in the code?
It's just code and serial monitor I'm using.
I would like something else other than serial monitor
Then if you can do it with 2 sensors
All help is thankfully received.
I have an ultrasonic sensor with code that works well but I want it to measure the lowest point of an object and then freeze the measurement result. How do I enter it in the code?
make a variable to store the lowest height. Initialize the variable to some arbitrary height that is way too high. Read the new height. If the new height is lower (less than) than the lower height variable, make the lower height variable equal to the new height. Else if the new height is higher, do nothing to the lower height variable. The lower height variable will end up holding the lowest height.
I would like something else other than serial monitor
Like what? A different terminal program, a custom PC program, ...?
Then if you can do it with 2 sensors
Do what with 2 sensors? Find 2 minimums?
here is my code.
#define trigPin 10
#define echoPin 13
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
float duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) * 0.0344;
if (distance >= 400 || distance <= 2){
Serial.print("Distance = ");
Serial.println("Out of range");
}
else {
Serial.print("Distance = ");
Serial.print(distance);
Serial.println(" cm");
delay(500);
}
delay(500);
}
what kind of code string and where i put that variable.
im new to coding.
A short sketch to illustrate what I said in reply #1.
const byte TRIG_PIN = 10;
const byte ECHO_PIN = 13;
float distanceCm;
float lowestDistance = 1000.0; // variable to record low, init way too high
void setup()
{
Serial.begin(9600);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
}
void loop()
{
float newDistance = getDistanceCM();
if ( newDistance != 0) // if not measurement error
{
if(newDistance < lowestDistance)
{
lowestDistance = newDistance;
}
Serial.print("Newest Distance ");
Serial.print(distanceCm);
Serial.print("cm");
Serial.print(" Lowest Distance ");
Serial.print(lowestDistance);
Serial.print("cm");
Serial.println();
}
delay(500);
}
float getDistanceCM()
{
long duration;
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
duration = pulseIn(ECHO_PIN, HIGH, 34000);
distanceCm = duration / 29.1 / 2 ;
if (distanceCm <= 0 || distanceCm > 500)
{
distanceCm = 0; // measurement error -- make distance zero
Serial.println("Out of range");
}
return distanceCm;
}
Ok late reply.
I Will check it thank you.