const unsigned long waterWait = 4000;
unsigned long waterdelay;
const byte trig = 12;
const byte echo = 11;
void setup()
{
Serial.begin(9600);
pinMode(trig, OUTPUT);
digitalWrite(trig, LOW);
pinMode(echo, INPUT);
}
void loop()
{
if (millis() - waterdelay > waterWait) {
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
unsigned long t = pulseIn(echo, HIGH);
unsigned long h = t / 45;
h = h - 5; // offset correction
h = 35 - h; // water height, 0 - 50 cm
unsigned long hp = 2.7 * h; // distance in %, 0-100 %
Serial.print(hp);
Serial.println(F("%"));
waterdelay += waterWait;
}
}
(I hate too much code) This code has not been compiled and it has not been tested.