HC-SR04 New ping.h

Just curious question.
I’m trying to read water level with the HC-SR04 and wanna convert it into % instead off CM.
Do i need to convert/reverse it? Like 0 CM should be like 100% tank is full, 200 CM should be like 0% tank is empty.

#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 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.

void setup() {
  Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
}

void loop() {
  delay(50);                      // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
  unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
  Serial.print("Ping: ");
  Serial.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance in cm and print result (0 = outside set distance range)
  Serial.println("cm");
}

Thanks

Do i need to convert/reverse it? Like 0 CM should be like 100% tank is full, 200 CM should be like 0% tank is empty.

That's up to you but it would seem to be sensible.

So is it possible?, tried maping... but it is just for analog read`?

tried maping...

. . . and?

It seems to me that even map() is overkill, and even simpler arithmetic would do the trick.

Trying my best :) but i can't seem to find a answer. Must be possible to just change define? CM = 0 is like 100% and CM = 200 is 0 %..

http://arduino.cc/en/pmwiki.php?n=Reference/HomePage been searching.

OK, here's a clue - what distance would be 50%? Or 25%?

sry,sir. isee no solution :/

 client.print("Ping: ");
 client.print(uS / US_ROUNDTRIP_CM);
 client.println("cm");

(uS / US_ROUNDTRIP_CM);

This put it in CM. but i need to say that uS / US_ROUNDTRIP_CM - 100 = 50 and 150 = 25

To reverse or "flip" an analog reading which varies from 0-1023, so you get a response that varies from 1023-0, try:

unsigned short flippedReading = 1023 - analogRead(A0);

Doesn't have to be an analog reading ... you could easily "flip" the range of any unsigned variable. For signed variables, I believe you need to use a 2's compliment method.

So, 200cm = 0% 150cm = 25% ?cm = 50% ?cm = 75% 0cm = 100%

Let me guess - 100cm would be 50%?

Hint: This isn't a programming problem, this is arithmetic.

sadsad: I'm trying to read water level with the HC-SR04 and wanna convert it into % instead off CM. Do i need to convert/reverse it? Like 0 CM should be like 100% tank is full, 200 CM should be like 0% tank is empty.

You can't use 0 CM since the minimum measurable distance is 3 CM. You would only use 200 CM if your tank is two meters deep. You can use simple math if your tank has a constant vertical cross-section (like a cylinder). Mount the sensor at least 3 CM above the surface when full. Call that height 'H'. Measure the depth 'D' of the tank when full, in CM. Take the raw reading in CM. Subtract H. Subtract the result from D. Divide that value by D. The result is the fraction of water in the tank.

thanks :)

client.print(uS / US_ROUNDTRIP_CM * 100 / depth);

this will work?

This work on the calculator

client.print(uS / US_ROUNDTRIP_CM - depth * 100 / depth);

but it seems that arduino don't like -95% :)

but it seems that arduino don't like -95% :)

The Arduino has no feelings on the matter.

How are the variables used in the calculation declared, how does the Arduino show its displeasure and why is the answer a negative number anyway ?

I thought that you wanted the number to be the percentage of the tank contents that are available, so that when it is full the answer should be 100% and the answer should never be negative.

Correct :) but i don't now how to convert the % over the other way.

The first calc client.print(uS / US_ROUNDTRIP_CM * 100 / depth); puts it in % based on CM out. like 10cm 5% and 60cm 30% 100cm 50% 150cm 75% this works but it's wrong in a way. If the tank is 10cm from the sensor, the tank is almost full. like 95%.

If the tank is 10cm from the sensor, the tank is almost full. like 95%.

Nonsense. The distance from the tank to the sensor has NOTHING to do with how full the tank is.

The distance from the sensor to the liquid in the tank is a horse of a different color.

If uS / US_ROUNDTRIP_CM * 100 / depth is the inverse of what you want, then adding "emptyDist - " somewhere in that equation will result in the value that you want.

The thing to do is to convert the distance to %-age in steps. emptyDist - measuredDist would define the full part of the tank. Converting that to %age is then trivial.