I did as you said and my code went as following
const int redled = 3;
const int greenled = 4;
volatile uint32_t risingMicros;
volatile uint32_t fallingMicros;
volatile bool fallDetected;
volatile bool riseDetected;
uint32_t timeBetweenPulses;
uint32_t pulseWidth;
void setup()
{
// put your setup code here, to run once:
pinMode (2, INPUT);
pinMode (redled, OUTPUT);
pinMode (greenled, OUTPUT);
attachInterrupt (0, risingISR, RISING);
attachInterrupt (0, fallingISR, FALLING);
}
void loop() {
// Deal with fallDetected and riseDetected here.
if (riseDetected) {
riseDetected = false;
timeBetweenPulses = risingMicros - fallingMicros;
if (timeBetweenPulses < pulseWidth * 2) {
// Looks like we just got the second of the double pulse.
digitalWrite (greenled, HIGH);
}
else digitalWrite (greenled, LOW);
}
if (fallDetected) {
fallDetected = false;
pulseWidth = fallingMicros - risingMicros;
}
}
void risingISR() {
risingMicros = micros();
riseDetected = true;
}
void fallingISR() {
fallingMicros = micros();
fallDetected = true;
}
But nothing worked, I searched about it and I found what you can find in the attached screenshot. attachInterrupt() - Arduino Reference
