/*
Forum: https://forum.arduino.cc/t/ky-032-obstacle-avoidance-sensor-not-working/1173727/7
Wokwi: https://wokwi.com/projects/377386960545098753
Simple test environment to count pulses using an interrupt
*/
constexpr byte inPin = 3;
unsigned long lastTime =0;
volatile unsigned long count = 0;
void ISRroutine(){
count++;
};
void setup() {
// put your setup code here, to run once:
Serial .begin(115200);
Serial.println("Start");
pinMode(inPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(inPin),ISRroutine,RISING);
lastTime = millis();
}
void loop() {
// put your main code here, to run repeatedly:
if (millis()-lastTime >= 1000){
noInterrupts();
unsigned long lastCount = count;
lastTime = millis();
count =0;
interrupts();
Serial.println(lastCount);
}
}