I'm trying to count number of HB100 microwave sensor pulses in 200ms
time quanta.
Here is the code:
#include <SoftwareSerial.h>
#include <elapsedMillis.h>
elapsedMillis ElapsedTime;
#define Sensor A0
#define TimeQuanta 200
int Counter = 0;
boolean LastState;
void setup()
{
Serial.begin(250000);
pinMode(Sensor, INPUT);
}
void loop()
{
Counter = 0;
ElapsedTime = 0;
while (ElapsedTime < TimeQuanta ){
LastState = digitalRead(Sensor);
if (LastState == LOW && digitalRead(Sensor) == HIGH ){
Counter += 1; //Compare Last state with current state
}
}
Serial.print(digitalRead(Sensor));
Serial.print("\t");
Serial.println(Counter);
}
I need to know the digital read cycles.
I'm comparing the last state of the sensor with current state and if a change is made (LOW TO HIGH) the counter is incremented. However, my counter is always 0
!
- is the code correct (the
if
condition)? - do I need some delays?
- is it possible to count these pulses?
Here is the Logic Analyzer output of Microwave Sensor:
Edit: if I add delay(1);
before if
then the counter is not 0 anymore.