Go Down

Topic: Aruino Uno and HRLV- MaxSonar- EZ (Ultrasonic rangefinder) (Read 256 times) previous topic - next topic

GNR1993


I am new at using an arduino, and posting for help, so please be easy on me.

I purchased a HRLV Max Sonar- EZ with the goal of creating a water height measurement tool.

Anyways, I am using this code:


void setup() {
  // put your setup code here, to run once:
 Serial.begin (9600);
  pinMode(10,OUTPUT);
  pinMode(13,INPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
float duration, distance;
  digitalWrite(10, LOW);
  delayMicroseconds(10);
 
  digitalWrite(10, HIGH);
  delayMicroseconds(10);
  digitalWrite(10, LOW);
 
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);
}



I believe it is working fine, but my Serial Monitor reads, "Out of range". I have tried increasing the amount of Microseconds in the delay, in case it wasn't receiving a signal fast enough, but this didn't work.

I am very new to this, so I have attached a photo of the set up I have in case this is flawed.

I'm an ecologist, just trying to make a cheap advice to monitor stream height.
Any help is appreciated!



Paul_KD7HB

I see you are comparing floats to integers, which never works as you think it will.

if (distance >= 400 || distance <= 2){

Change to:

if (distance >= 400.0 || distance <= 2.0){

So you compare float to float. See if that helps.

Paul

DaveEvans


GNR1993

@DaveEvans,

I'm very new, how do I include that line of code?

Also, this sketch used to work and now it says there is an error when uploading it.

Any suggestions?

I appreciate your time!

Go Up