I use a Wemos D1 (ESP8266) to detect pulses of a reed sensor of a water meter. To debug my code I want to know how long the pulses are. This is the relevant part of my code:
boolean pulseStartDetected = false;
boolean inPinLevel = HIGH;
unsigned long pulseStartTime = 0;
unsigned long pulseEndTime = 0;
unsigned long pulseDuration = 0;
- - -
void loop() {
client.loop();
inPinLevel = digitalRead(inPin);
if (inPinLevel == LOW) {
pulseStartDetected = true;
pulseStartTime = millis();
}
if ((inPinLevel == HIGH) && (pulseStartDetected)) {
pulseEndTime = millis();
pulseDuration = (unsigned long)(pulseEndTime - pulseStartTime);
pulseCounter ++;
pulseStartDetected = false;
if (debug) {
Serial.println("Pulse detected");
Serial.print("Pulse Duration [ms]: ");
Serial.println(pulseDuration);
}
}
- - -
}
I have the problem that "Serial.println(pulseDuration)" always gives "0".
If I print "pulseStartTime" and "pulseEndTime" I always get reasonable values.
I also omitted the cast operator "(unsigned long)" (used for millis() overflow) - no difference.