oh - i forgot to check this thread again...
i hope i m still in time...
it is very unlikely that u check at the right microsecond, because all those things can take several microseconds...
so u should write "if (duration < x*1e6)"...
the while-condition looks suspicious, too...
it should be "while (duration < x*1e6);"...
btw: why do u want to use floating point arithmetics?
u could use "if (duration < x*1000000UL)", too...
"UL" means "unsigned long"... an "
unsigned long" is a "
uint32_t"...
and u should reset "pulseLow" to "0" in the "else" branch...
pulseIn delivers a value in "milli seconds"... so u should devide it by 30e3 (not 30e6)...
the rest looks good...
but just for 1.19hrs... :-) then or about then micros() starts at 0 again...
why dont u do it without the "x"?
void loop() {
// 1. phase: pulseIn for 30 seconds
const uint32_t howlong = 30000UL;
const uint32_t sts = millis(); // start time stamp
uint32_t ls = 0;
while (millis() - sts < howlong)
ls += pulseIn(...);
percentage = ls/(howlong/100);
// 2. phase: interpret the result
... like the else-branch
}
NB: in
unsigned integer arithmetics a single overflow is well tolerated in the "millis() - sts" subtraction above...
e. g.: we use a 3 bit integer and howlong is 2:
sts=6 millis()=7 --> 7-6 = 1 < 2
sts=6 millis()=0 (overflow occurred) -> -6 mod 8 = (-6+8) mod 8 = 2 mod 8
-arne