0
votes

I'm trying to attach interrupts to the rising edge of a signal (PWM). However, the signal is somewhat noisy when it's HIGH which causes the code to register another interrupt when it should not. I obviously tried to fix this in my circuit but that's not quite working so I moved to the software part.

The question is how I can filter out interrupts within a given frequency range? I need to apply a lowpass filter so that the interrupts do not get triggered when the signal is HIGH. My idea was detach the interrupt for a given amount of time or simply ignore the interrupt if it happens within a certain time range.

I'm just not sure how to achieve this.

This is my code:

unsigned long tsend = 0;
unsigned long techo = 0;
const int SEND = 2;
const int ECHO = 3;
unsigned long telapsed = 0;
unsigned long treal = 0;

void setup() {
  Serial.begin(115200);
  Serial.println("Start");

  pinMode(SEND, INPUT);
  pinMode(ECHO, INPUT);

  attachInterrupt(digitalPinToInterrupt(SEND), time_send, RISING);
  attachInterrupt(digitalPinToInterrupt(ECHO), time_echo, RISING);
}

void loop() {
  telapsed = techo - tsend;
  if (telapsed > 100 && telapsed < 10000000) {
    treal = telapsed;
    Serial.println(treal);
  }
}

void time_send() {
  tsend = micros();
}
void time_echo() {
  techo = micros();

}

Below is the signal (yellow) which has a lot of noise. I need to ignore the interrupts when the signal is HIGH. Here is an image of the PWM Signal

2

2 Answers

0
votes

I would try the following:

#define DEBOUNCE_TIME 100

void time_send() {
  static long last = micros() ;
  if (last-tsend > DEBOUNCE_TIME)
     tsend = last;
}
void time_echo() {
  static long last = micros() ;
  if (last-techo > DEBOUNCE_TIME)
     techo = last;

}

And adjust DEBOUNCE_TIME until I get a satisfactory result.

0
votes
const byte intrpt_pin = 18; 
volatile unsigned int count = 0; 

#define DEBOUNCE_TIME 5000

void isr()
{
  cli();
  delayMicroseconds(DEBOUNCE_TIME);
  sei();

  count++;
}

void setup()
{
  pinMode(intrpt_pin, INPUT_PULLUP);

  attachInterrupt(digitalPinToInterrupt(intrpt_pin), isr, FALLING);
}

void loop()
{
}

cli() : Disables all interrupts by clearing the global interrupt mask.

sei() : Enables interrupts by setting the global interrupt mask.

So, basically this program will ignore all the interrupt that occurs between these two lines, that is for DEBOUNCE_TIME. Check your your interrupt bouncing time and adjust DEBOUNCE_TIME accordingly for the best result.