 # How do I add equations/conversions into a sketch?

Hi.

I’m trying to use a ultrasonic ping sensor to determine the amount of water left in a tank.

So if puts out the distance out as 5cm, I want it to send back the signal as 650L, and when it’d read 65cm, it’d come back as 1L, with a sliding scale inbetween.

I’m not using any fancy code at the moment:

``````// ---------------------------------------------------------------------------
// Example NewPing library sketch that does a ping about 20 times per second.
// ---------------------------------------------------------------------------

#include <NewPing.h>

#define TRIGGER_PIN  5  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     7  // 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");
}
``````

I’m happy to do the calculations myself, and figure it out, but I was hoping somebody could point me in the right direction.

Thanks in advance for the help!

I'd calc the volume each time.

What ever shape it is, and assuming it's of constant cross-section, calc the area once based on the shape, call it A.

Then from there on, V = Ad where d is the depth.

d will of course be difference between the max depth say D and the reading from the sensor say h.

``````float A = xxxxxxx; // whatever the value is
float V;
float D = xxxxx // whatever the value is
float h;
``````

Then later in loop() where you need it:

``````V = A * (D-h);
``````

I'd calc the volume each time.

As, JimboZA pointed out, you can calculate the volume at each iteration of the loop() function. That's nice for smaller programs where you just have print out the volume in the Serial monitor.

But if you code grows i.e. you add stuff like an LED meter showing the water level or the reading being displayed on an LCD screen and adding buttons to fill the tank if the volume is low etc etc you might find addition of a function to do the calculation a better alternative than putting in code to calculate it in your loop().

For example, you might declare a function that calculates volume.

``````int calcVolume(int sensorReading)
{
// expressions that calculate the volume based on sensor reading
return volume;
}
``````

and then call it in you main loop() function.

``````void loop()
{
// some code