9 years, 3 months ago.

Calculation a height instead of printing centimeters

Hey everyone.

I'm very new to the developer and IoT world, and this is probably a very simple question. Currently, when I print in CoolTerm, all i get is centimeters, and I would like the water height instead. How do I set up an equation, so I get the actual water-height? My container is 50 cm tall :) I hope you guys can help!

My code:

#include "mbed.h"
#include "SRF05.h"

AnalogIn fugtighed(p20);
SRF05 srf(p13, p14);
//DigitalOut diode(p10);
DigitalOut relay(p12);

Serial pc(USBTX, USBRX); // tx, rx
Serial WiFi(p28, p27); //WiFi serial
float moisture = 0.0;
float distance = 0.0;
//water_height = 50-distance
//water_volume = pi*r2*water_height
Ticker tick;
void ticker(){
    
    unsigned short moistureDigital = fugtighed.read_u16(); //0 - 65535
    moisture = ((float)moistureDigital)*(3300.0/65535.0); //0 - 3300mV
    pc.printf("moisture: digital: %d Analog(mV): %.2f\r\n", moistureDigital, moisture);
    
}

int main() {
    
    pc.baud(57600); //Setup BAUD rate to CoolTerm to 56700bps
    WiFi.baud(57600); //Setup BAUD rate to WiFi to 56700bps

    //tick.attach(&ticker, 1.0); //update every 1 sec

    
    //Print welcome message in CoolTerm
    pc.printf("\n* * * * * * * * *\nReady to send and receive!\n* * * * * * * * *\n\n\n");
    
    while(1) {
        
    unsigned short moistureDigital = fugtighed.read_u16(); //0 - 65535
    moisture = ((float)moistureDigital)*(3300.0/65535.0); //0 - 3300mV
    pc.printf("moisture: digital: %d Analog(mV): %.2f\r\n", moistureDigital, moisture);
    
    distance = srf.read();
        
        printf("Distance cm = %.1f\n", distance);              
         
         if (distance <= 50 && moisture >= 2000){
             
             //diode = 1;
             relay = 1;
             
        }else{
                 
              //diode = 0;   
              relay = 0;
              
        }
            
        WiFi.printf("LOOP8,3,moisture,%f,water_height,%f,water_volume,%f", moisture, distance, 11);
        wait(5);

//
    }
}

1 Answer

9 years, 3 months ago.

Hey Christian :),

If understand this correctly, you have a container with an ultrasonic attached to the top of it? And you want to know how much water is in the container. There are two ways of doing this.

  • 1. Physically measure yourself the distance from the front of the ultrasonic sensor to the bottom of inside the container lets call it zero_measurement. Then the the water_height= zero_measurement - distance.
  • 2. You could automatically measure the distance with ultrasonic sensor every tie the platform turns on, first take a measurement when there is nothing in the container, this again is a zero_measurement, just now it is dynamic to the container.

Let me know if that helps.

Best Regards,

Andrea, team mbed