TolpuddleSartre:
Is it critical to get rid of the very short blocking delayMicroseconds?
In fact, those delays are almost certainly shorter than pulseIn
i dont know i have alot of other timers in the background this is just a small part of code that im going to integrate into my main sketch. so far i have come up with this,
unsigned long WaterWait = 4000;
unsigned long Waterdelay =0;
unsigned long WaterWait2 = 10;
unsigned long Waterdelay2 =0;
unsigned long Waterdelay3 =0;
int trig = 12;
int echo = 11;
void setup()
{
Serial.begin(9600);
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
}
void loop()
{
if (millis() - Waterdelay > WaterWait) {
long t = 0, h = 0, hp = 0;
digitalWrite(trig, LOW);
if (millis() - Waterdelay2 > WaterWait2) {
digitalWrite(trig, HIGH);
if (millis() - Waterdelay3 > WaterWait2) {
digitalWrite(trig, LOW);
t = pulseIn(echo, HIGH);
h = t / 45;
h = h - 5; // offset correction
h = 35 - h; // water height, 0 - 50 cm
hp = 2.7 * h; // distance in %, 0-100 %
Waterdelay2 = millis();
Waterdelay2 = millis();
Waterdelay = millis();
Serial.print(hp);
Serial.println(F("%"));
}
}
}
}