Pulse Counter using Interrupts

Hi,

I have been trying to create a system that will count the number of pulses read on a pin in between pulses of another signal using interrupts.

For example, in between 2 pulses of a signal say Z, The Arduino will be counting number of pulses from another signal “X”. There is no fixed value for the frequency of these signals. I expect their frequencies to stay in a range from 100Hz to 100KHz.

I have attached an image to hopefully clarify what I am trying to accomplish.

In my code I am attempting to do this using 2 interrupts. Once signal Z sends a pulse, a flag will be set and the Arduino will now count pulses in X. Once another pulse is detected from Z the flag is set off and the Arduino will print the value of pulses in between and reset the counter.

The issue I noticed is that will method will skip every other segment of pulses to be counted. This isn’t a huge issue since the number of samples will be large. The next issue is that it seems it does not always detect when to begin printing and reset the counter which will throw off the number of pulses counted completely off.

I appreciate any suggestions regarding this problem…
Please let me know if i need to explain this any better!

bool inPulses = false;
int long counter = 0;

void setup() {
  Serial.begin(115200);
  attachInterrupt(0,changeFlag,FALLING);
  
}

void loop() {
  if (inPulses){
    attachInterrupt(1,pulseCounter,RISING);
  }

  else{
    Serial.println(counter);
    counter = 0;
    delay(100);
  }
}

void pulseCounter(){
  counter++;
}  

void changeFlag(){
  inPulses != inPulses;
}

20190408_172832.jpg

Can you clarify: are there cases where there may be no pulses from one signal between the second signal pulses?

Paul

At 100kHz pulse rate printing might use too much time. Don't print from inside the interrupt routine.

Attach the interrupts once (not every loop)
Use a flag from Z to increment when X arrives - ignore X if flag not set.
Do as little as possible inside interrupt code... less lines != less code !

bool Z_state = false;
int long counter = 0;
unsigned long update_ticker;
unsigned long update_interval = 100L;


void setup() {
  Serial.begin(115200);
  attachInterrupt(0,Z_event,FALLING);
  attachInterrupt(1,X_event,RISING);
}

void loop() {
  if (millis() - update_ticker > update_interval){
		Serial.println(counter);
    counter = 0;
		update_ticker = millis();    // delay(100);  // NO NO NO !!!
  }
}

void X_event(){
	if (Z_state)
		counter++;
}  

void Z_event(){
  Z_state != Z_state;
	/*	you may want to validate the even count of Z-ticks,
			if one is missed for any reason the Z-state will be 'upside' down... !
	*/
}

Paul_KD7HB:
Can you clarify: are there cases where there may be no pulses from one signal between the second signal pulses?

Paul

There will be no case where there are no pulses for either signals. Ultimately, they are just 2 signals with different frequencies that may be changing. There will always at least 1 pulse from X within the 2 pulses of Z in other words.

Railroader:
At 100kHz pulse rate printing might use too much time. Don't print from inside the interrupt routine.

I see, what would you recommend as a method of displaying data? It doesn't have to necessarily be a live reading of numbers. Maybe storing each value into an array and displaying after so many samples?

Let the normal loop display the data at some 10Hz rate, or even down to 2 Hz. That cuts down the the load from displaying. No human eye will pick up KHz updating.

void changeFlag(){
inPulses != inPulses;
}

Why to do you toggle inPulses? Why not make it =true when the interrupt fires, and set it back false when you process the data.

quote:"There will be no case where there are no pulses for either signals. Ultimately, they are just 2 signals with different frequencies that may be changing. There will always at least 1 pulse from X within the 2 pulses of Z in other words.
unquote.

If this is true then your two signals cannot be varying from 100 Hz to 100 kHz. Is there some mechanism to make sure both signals are not at exactly the same frequency?

Paul

There will always at least 1 pulse from X within the 2 pulses of Z in other words.

For example, in between 2 pulses of a signal say Z, The Arduino will be counting number of pulses from another signal "X". There is no fixed value for the frequency of these signals. I expect their frequencies to stay in a range from 100Hz to 100KHz.

There must be some other constraint on the relationship between the frequencies. If Z is at 100Khz and X is at 100Hz there will not be at least one X within 2 Z periods.